src/HOL/Library/Tools/lazy.scala
changeset 82379 3f875966c3e1
equal deleted inserted replaced
82378:23df00d48d6f 82379:3f875966c3e1
       
     1 /* Author: Pascal Stoop, ETH Zurich
       
     2    Author: Andreas Lochbihler, Digital Asset */
       
     3 
       
     4 object Lazy {
       
     5   final class Lazy[A] (f: Unit => A) {
       
     6     var evaluated = false;
       
     7     lazy val x: A = f(())
       
     8 
       
     9     def get() : A = {
       
    10       evaluated = true;
       
    11       return x
       
    12     }
       
    13   }
       
    14 
       
    15   def force[A] (x: Lazy[A]) : A = {
       
    16     return x.get()
       
    17   }
       
    18 
       
    19   def delay[A] (f: Unit => A) : Lazy[A] = {
       
    20     return new Lazy[A] (f)
       
    21   }
       
    22 
       
    23   def termify_lazy[Typerep, Term, A] (
       
    24     const: String => Typerep => Term,
       
    25     app: Term => Term => Term,
       
    26     abs: String => Typerep => Term => Term,
       
    27     unitT: Typerep,
       
    28     funT: Typerep => Typerep => Typerep,
       
    29     lazyT: Typerep => Typerep,
       
    30     term_of: A => Term,
       
    31     ty: Typerep,
       
    32     x: Lazy[A],
       
    33     dummy: Term) : Term = {
       
    34     x.evaluated match {
       
    35       case true => app(const("Code_Lazy.delay")(funT(funT(unitT)(ty))(lazyT(ty))))(abs("_")(unitT)(term_of(x.get())))
       
    36       case false => app(const("Code_Lazy.delay")(funT(funT(unitT)(ty))(lazyT(ty))))(const("Pure.dummy_pattern")(funT(unitT)(ty)))
       
    37     }
       
    38   }
       
    39 }