src/HOL/Library/Code_Lazy.thy
author wenzelm
Mon, 17 May 2021 13:37:47 +0200
changeset 73711 5833b556b3b5
parent 69690 1fb204399d8d
child 76987 4c275405faae
permissions -rw-r--r--
proper syntax of Scala 3;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     1
(* Author: Pascal Stoop, ETH Zurich
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     2
   Author: Andreas Lochbihler, Digital Asset *)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     3
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     4
section \<open>Lazy types in generated code\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     5
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     6
theory Code_Lazy
69567
6b4c41037649 separate case converter into a separate theory
Andreas Lochbihler
parents: 69528
diff changeset
     7
imports Case_Converter
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     8
keywords
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     9
  "code_lazy_type"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    10
  "activate_lazy_type"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    11
  "deactivate_lazy_type"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    12
  "activate_lazy_types"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    13
  "deactivate_lazy_types"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    14
  "print_lazy_types" :: thy_decl
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    15
begin
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    16
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    17
text \<open>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    18
  This theory and the CodeLazy tool described in @{cite "LochbihlerStoop2018"}.
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    19
68390
c558a2202f32 eliminated suspicious Unicode;
wenzelm
parents: 68155
diff changeset
    20
  It hooks into Isabelle's code generator such that the generated code evaluates a user-specified
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    21
  set of type constructors lazily, even in target languages with eager evaluation.
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    22
  The lazy type must be algebraic, i.e., values must be built from constructors and a
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    23
  corresponding case operator decomposes them. Every datatype and codatatype is algebraic
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    24
  and thus eligible for lazification.
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    25
\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    26
69272
15e9ed5b28fb isabelle update_cartouches -t;
wenzelm
parents: 69216
diff changeset
    27
subsection \<open>The type \<open>lazy\<close>\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    28
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    29
typedef 'a lazy = "UNIV :: 'a set" ..
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    30
setup_lifting type_definition_lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    31
lift_definition delay :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a lazy"  is "\<lambda>f. f ()" .
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    32
lift_definition force :: "'a lazy \<Rightarrow> 'a" is "\<lambda>x. x" .
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    33
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    34
code_datatype delay
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    35
lemma force_delay [code]: "force (delay f) = f ()" by transfer (rule refl)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    36
lemma delay_force: "delay (\<lambda>_. force s) = s" by transfer (rule refl)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    37
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    38
definition termify_lazy2 :: "'a :: typerep lazy \<Rightarrow> term"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    39
  where "termify_lazy2 x =
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    40
  Code_Evaluation.App (Code_Evaluation.Const (STR ''Code_Lazy.delay'') (TYPEREP((unit \<Rightarrow> 'a) \<Rightarrow> 'a lazy)))
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    41
    (Code_Evaluation.Const (STR ''Pure.dummy_pattern'') (TYPEREP((unit \<Rightarrow> 'a))))"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    42
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    43
definition termify_lazy ::
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    44
  "(String.literal \<Rightarrow> 'typerep \<Rightarrow> 'term) \<Rightarrow>
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    45
   ('term \<Rightarrow> 'term \<Rightarrow> 'term) \<Rightarrow>
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    46
   (String.literal \<Rightarrow> 'typerep \<Rightarrow> 'term \<Rightarrow> 'term) \<Rightarrow>
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    47
   'typerep \<Rightarrow> ('typerep \<Rightarrow> 'typerep \<Rightarrow> 'typerep) \<Rightarrow> ('typerep \<Rightarrow> 'typerep) \<Rightarrow>
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    48
   ('a \<Rightarrow> 'term) \<Rightarrow> 'typerep \<Rightarrow> 'a :: typerep lazy \<Rightarrow> 'term \<Rightarrow> term"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    49
  where "termify_lazy _ _ _ _ _ _ _ _ x _ = termify_lazy2 x"
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    50
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    51
declare [[code drop: "Code_Evaluation.term_of :: _ lazy \<Rightarrow> _"]]
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    52
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    53
lemma term_of_lazy_code [code]:
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    54
  "Code_Evaluation.term_of x \<equiv>
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    55
   termify_lazy
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    56
     Code_Evaluation.Const Code_Evaluation.App Code_Evaluation.Abs
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    57
     TYPEREP(unit) (\<lambda>T U. typerep.Typerep (STR ''fun'') [T, U]) (\<lambda>T. typerep.Typerep (STR ''Code_Lazy.lazy'') [T])
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    58
     Code_Evaluation.term_of TYPEREP('a) x (Code_Evaluation.Const (STR '''') (TYPEREP(unit)))"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    59
  for x :: "'a :: {typerep, term_of} lazy"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    60
  by (rule term_of_anything)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    61
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    62
text \<open>
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69567
diff changeset
    63
  The implementations of \<^typ>\<open>_ lazy\<close> using language primitives cache forced values.
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    64
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    65
  Term reconstruction for lazy looks into the lazy value and reconstructs it to the depth it has been evaluated.
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    66
  This is not done for Haskell as we do not know of any portable way to inspect whether a lazy value
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    67
  has been evaluated to or not.
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
    68
\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    69
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    70
code_printing code_module Lazy \<rightharpoonup> (SML)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    71
\<open>signature LAZY =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    72
sig
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    73
  type 'a lazy;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    74
  val lazy : (unit -> 'a) -> 'a lazy;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    75
  val force : 'a lazy -> 'a;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    76
  val peek : 'a lazy -> 'a option
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    77
  val termify_lazy : 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    78
   (string -> 'typerep -> 'term) -> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    79
   ('term -> 'term -> 'term) -> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    80
   (string -> 'typerep -> 'term -> 'term) ->
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    81
   'typerep -> ('typerep -> 'typerep -> 'typerep) -> ('typerep -> 'typerep) ->
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    82
   ('a -> 'term) -> 'typerep -> 'a lazy -> 'term -> 'term;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    83
end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    84
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    85
structure Lazy : LAZY = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    86
struct
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    87
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    88
datatype 'a content =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    89
   Delay of unit -> 'a
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    90
 | Value of 'a 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    91
 | Exn of exn;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    92
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    93
datatype 'a lazy = Lazy of 'a content ref;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    94
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    95
fun lazy f = Lazy (ref (Delay f));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    96
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    97
fun force (Lazy x) = case !x of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    98
   Delay f => (
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    99
     let val res = f (); val _ = x := Value res; in res end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   100
     handle exn => (x := Exn exn; raise exn))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   101
  | Value x => x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   102
  | Exn exn => raise exn;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   103
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   104
fun peek (Lazy x) = case !x of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   105
    Value x => SOME x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   106
  | _ => NONE;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   107
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   108
fun termify_lazy const app abs unitT funT lazyT term_of T x _ =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   109
  app (const "Code_Lazy.delay" (funT (funT unitT T) (lazyT T))) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   110
    (case peek x of SOME y => abs "_" unitT (term_of y)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   111
     | _ => const "Pure.dummy_pattern" (funT unitT T));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   112
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   113
end;\<close> for type_constructor lazy constant delay force termify_lazy
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   114
| type_constructor lazy \<rightharpoonup> (SML) "_ Lazy.lazy"
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   115
| constant delay \<rightharpoonup> (SML) "Lazy.lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   116
| constant force \<rightharpoonup> (SML) "Lazy.force"
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   117
| constant termify_lazy \<rightharpoonup> (SML) "Lazy.termify'_lazy"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   118
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   119
code_reserved SML Lazy
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   120
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   121
code_printing \<comment> \<open>For code generation within the Isabelle environment, we reuse the thread-safe
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69567
diff changeset
   122
  implementation of lazy from \<^file>\<open>~~/src/Pure/Concurrent/lazy.ML\<close>\<close>
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   123
  code_module Lazy \<rightharpoonup> (Eval) \<open>\<close> for constant undefined
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   124
| type_constructor lazy \<rightharpoonup> (Eval) "_ Lazy.lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   125
| constant delay \<rightharpoonup> (Eval) "Lazy.lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   126
| constant force \<rightharpoonup> (Eval) "Lazy.force"
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   127
| code_module Termify_Lazy \<rightharpoonup> (Eval)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   128
\<open>structure Termify_Lazy = struct
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   129
fun termify_lazy
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   130
  (_: string -> typ -> term) (_: term -> term -> term)  (_: string -> typ -> term -> term)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   131
  (_: typ) (_: typ -> typ -> typ) (_: typ -> typ)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   132
  (term_of: 'a -> term) (T: typ) (x: 'a Lazy.lazy) (_: term) =
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   133
  Const ("Code_Lazy.delay", (HOLogic.unitT --> T) --> Type ("Code_Lazy.lazy", [T])) $
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   134
  (case Lazy.peek x of
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   135
    SOME (Exn.Res x) => absdummy HOLogic.unitT (term_of x)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   136
  | _ => Const ("Pure.dummy_pattern", HOLogic.unitT --> T));
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   137
end;\<close> for constant termify_lazy
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   138
| constant termify_lazy \<rightharpoonup> (Eval) "Termify'_Lazy.termify'_lazy"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   139
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   140
code_reserved Eval Termify_Lazy
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   141
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   142
code_printing
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   143
  type_constructor lazy \<rightharpoonup> (OCaml) "_ Lazy.t"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   144
| constant delay \<rightharpoonup> (OCaml) "Lazy.from'_fun"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   145
| constant force \<rightharpoonup> (OCaml) "Lazy.force"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   146
| code_module Termify_Lazy \<rightharpoonup> (OCaml)
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   147
\<open>module Termify_Lazy : sig
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   148
  val termify_lazy :
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   149
   (string -> 'typerep -> 'term) ->
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   150
   ('term -> 'term -> 'term) ->
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   151
   (string -> 'typerep -> 'term -> 'term) ->
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   152
   'typerep -> ('typerep -> 'typerep -> 'typerep) -> ('typerep -> 'typerep) ->
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   153
   ('a -> 'term) -> 'typerep -> 'a Lazy.t -> 'term -> 'term
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   154
end = struct
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   155
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   156
let termify_lazy const app abs unitT funT lazyT term_of ty x _ =
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   157
  app (const "Code_Lazy.delay" (funT (funT unitT ty) (lazyT ty)))
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   158
    (if Lazy.is_val x then abs "_" unitT (term_of (Lazy.force x))
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   159
     else const "Pure.dummy_pattern" (funT unitT ty));;
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   160
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   161
end;;\<close> for constant termify_lazy
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   162
| constant termify_lazy \<rightharpoonup> (OCaml) "Termify'_Lazy.termify'_lazy"
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   163
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   164
code_reserved OCaml Lazy Termify_Lazy
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   165
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   166
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   167
code_printing
69690
1fb204399d8d self-contained code modules for Haskell
haftmann
parents: 69605
diff changeset
   168
  code_module Lazy \<rightharpoonup> (Haskell) \<open>
1fb204399d8d self-contained code modules for Haskell
haftmann
parents: 69605
diff changeset
   169
module Lazy(Lazy, delay, force) where
1fb204399d8d self-contained code modules for Haskell
haftmann
parents: 69605
diff changeset
   170
1fb204399d8d self-contained code modules for Haskell
haftmann
parents: 69605
diff changeset
   171
newtype Lazy a = Lazy a
1fb204399d8d self-contained code modules for Haskell
haftmann
parents: 69605
diff changeset
   172
delay f = Lazy (f ())
1fb204399d8d self-contained code modules for Haskell
haftmann
parents: 69605
diff changeset
   173
force (Lazy x) = x\<close> for type_constructor lazy constant delay force
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   174
| type_constructor lazy \<rightharpoonup> (Haskell) "Lazy.Lazy _"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   175
| constant delay \<rightharpoonup> (Haskell) "Lazy.delay"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   176
| constant force \<rightharpoonup> (Haskell) "Lazy.force"
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   177
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   178
code_reserved Haskell Lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   179
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   180
code_printing
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   181
  code_module Lazy \<rightharpoonup> (Scala) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   182
\<open>object Lazy {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   183
  final class Lazy[A] (f: Unit => A) {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   184
    var evaluated = false;
73711
5833b556b3b5 proper syntax of Scala 3;
wenzelm
parents: 69690
diff changeset
   185
    lazy val x: A = f(())
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   186
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   187
    def get() : A = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   188
      evaluated = true;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   189
      return x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   190
    }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   191
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   192
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   193
  def force[A] (x: Lazy[A]) : A = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   194
    return x.get()
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   195
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   196
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   197
  def delay[A] (f: Unit => A) : Lazy[A] = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   198
    return new Lazy[A] (f)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   199
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   200
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   201
  def termify_lazy[Typerep, Term, A] (
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   202
    const: String => Typerep => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   203
    app: Term => Term => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   204
    abs: String => Typerep => Term => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   205
    unitT: Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   206
    funT: Typerep => Typerep => Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   207
    lazyT: Typerep => Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   208
    term_of: A => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   209
    ty: Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   210
    x: Lazy[A],
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   211
    dummy: Term) : Term = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   212
    if (x.evaluated)
73711
5833b556b3b5 proper syntax of Scala 3;
wenzelm
parents: 69690
diff changeset
   213
      app(const("Code_Lazy.delay")(funT(funT(unitT)(ty))(lazyT(ty))))(abs("_")(unitT)(term_of(x.get())))
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   214
    else
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   215
      app(const("Code_Lazy.delay")(funT(funT(unitT)(ty))(lazyT(ty))))(const("Pure.dummy_pattern")(funT(unitT)(ty)))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   216
  }
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   217
}\<close> for type_constructor lazy constant delay force termify_lazy
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   218
| type_constructor lazy \<rightharpoonup> (Scala) "Lazy.Lazy[_]"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   219
| constant delay \<rightharpoonup> (Scala) "Lazy.delay"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   220
| constant force \<rightharpoonup> (Scala) "Lazy.force"
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   221
| constant termify_lazy \<rightharpoonup> (Scala) "Lazy.termify'_lazy"
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   222
69528
9d0e492e3229 explicit dependencies for includes
haftmann
parents: 69272
diff changeset
   223
code_reserved Scala Lazy
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   224
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69567
diff changeset
   225
text \<open>Make evaluation with the simplifier respect \<^term>\<open>delay\<close>s.\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   226
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   227
lemma delay_lazy_cong: "delay f = delay f" by simp
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   228
setup \<open>Code_Simp.map_ss (Simplifier.add_cong @{thm delay_lazy_cong})\<close>        
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   229
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   230
subsection \<open>Implementation\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   231
69605
a96320074298 isabelle update -u path_cartouches;
wenzelm
parents: 69593
diff changeset
   232
ML_file \<open>code_lazy.ML\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   233
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   234
setup \<open>
69216
1a52baa70aed clarified ML_Context.expression: it is a closed expression, not a let-declaration -- thus source positions are more accurate (amending d8849cfad60f, 162a4c2e97bc);
wenzelm
parents: 68390
diff changeset
   235
  Code_Preproc.add_functrans ("lazy_datatype", Code_Lazy.transform_code_eqs)
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   236
\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   237
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   238
end