src/HOL/Library/Code_Lazy.thy
author wenzelm
Wed, 31 Oct 2018 15:53:32 +0100
changeset 69216 1a52baa70aed
parent 68390 c558a2202f32
child 69272 15e9ed5b28fb
permissions -rw-r--r--
clarified ML_Context.expression: it is a closed expression, not a let-declaration -- thus source positions are more accurate (amending d8849cfad60f, 162a4c2e97bc);
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
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     7
imports Main
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
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    27
subsection \<open>Eliminating pattern matches\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    28
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    29
definition missing_pattern_match :: "String.literal \<Rightarrow> (unit \<Rightarrow> 'a) \<Rightarrow> 'a" where
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    30
  [code del]: "missing_pattern_match m f = f ()"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    31
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    32
lemma missing_pattern_match_cong [cong]:
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    33
  "m = m' \<Longrightarrow> missing_pattern_match m f = missing_pattern_match m' f"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    34
  by(rule arg_cong)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    35
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    36
lemma missing_pattern_match_code [code_unfold]:
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    37
  "missing_pattern_match = Code.abort"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    38
  unfolding missing_pattern_match_def Code.abort_def ..
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    39
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    40
ML_file "case_converter.ML"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    41
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    42
subsection \<open>The type @{text lazy}\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    43
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    44
typedef 'a lazy = "UNIV :: 'a set" ..
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    45
setup_lifting type_definition_lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    46
lift_definition delay :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a lazy"  is "\<lambda>f. f ()" .
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    47
lift_definition force :: "'a lazy \<Rightarrow> 'a" is "\<lambda>x. x" .
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    48
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    49
code_datatype delay
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    50
lemma force_delay [code]: "force (delay f) = f ()" by transfer(rule refl)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    51
lemma delay_force: "delay (\<lambda>_. force s) = s" by transfer(rule refl)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    52
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    53
text \<open>The implementations of @{typ "_ lazy"} using language primitives cache forced values.\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    54
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    55
code_printing code_module Lazy \<rightharpoonup> (SML)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    56
\<open>signature LAZY =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    57
sig
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    58
  type 'a lazy;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    59
  val lazy : (unit -> 'a) -> 'a lazy;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    60
  val force : 'a lazy -> 'a;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    61
  val peek : 'a lazy -> 'a option
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    62
  val termify_lazy : 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    63
   (string -> 'typerep -> 'term) -> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    64
   ('term -> 'term -> 'term) -> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    65
   (string -> 'typerep -> 'term -> 'term) ->
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    66
   'typerep -> ('typerep -> 'typerep -> 'typerep) -> ('typerep -> 'typerep) ->
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    67
   ('a -> 'term) -> 'typerep -> 'a lazy -> 'term -> 'term;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    68
end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    69
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    70
structure Lazy : LAZY = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    71
struct
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    72
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    73
datatype 'a content =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    74
   Delay of unit -> 'a
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    75
 | Value of 'a 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    76
 | Exn of exn;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    77
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    78
datatype 'a lazy = Lazy of 'a content ref;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    79
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    80
fun lazy f = Lazy (ref (Delay f));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    81
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    82
fun force (Lazy x) = case !x of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    83
   Delay f => (
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    84
     let val res = f (); val _ = x := Value res; in res end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    85
     handle exn => (x := Exn exn; raise exn))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    86
  | Value x => x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    87
  | Exn exn => raise exn;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    88
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    89
fun peek (Lazy x) = case !x of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    90
    Value x => SOME x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    91
  | _ => NONE;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    92
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    93
fun termify_lazy const app abs unitT funT lazyT term_of T x _ =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    94
  app (const "Code_Lazy.delay" (funT (funT unitT T) (lazyT T))) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    95
    (case peek x of SOME y => abs "_" unitT (term_of y)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    96
     | _ => const "Pure.dummy_pattern" (funT unitT T));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    97
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    98
end;\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    99
code_reserved SML Lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   100
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   101
code_printing
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   102
  type_constructor lazy \<rightharpoonup> (SML) "_ Lazy.lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   103
| constant delay \<rightharpoonup> (SML) "Lazy.lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   104
| constant force \<rightharpoonup> (SML) "Lazy.force"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   105
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   106
code_printing \<comment> \<open>For code generation within the Isabelle environment, we reuse the thread-safe
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   107
  implementation of lazy from @{file "~~/src/Pure/Concurrent/lazy.ML"}\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   108
  code_module Lazy \<rightharpoonup> (Eval) \<open>\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   109
| type_constructor lazy \<rightharpoonup> (Eval) "_ Lazy.lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   110
| constant delay \<rightharpoonup> (Eval) "Lazy.lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   111
| constant force \<rightharpoonup> (Eval) "Lazy.force"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   112
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   113
code_printing
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   114
  code_module Lazy \<rightharpoonup> (Haskell) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   115
\<open>newtype Lazy a = Lazy a;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   116
delay f = Lazy (f ());
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   117
force (Lazy x) = x;\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   118
| type_constructor lazy \<rightharpoonup> (Haskell) "Lazy.Lazy _"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   119
| constant delay \<rightharpoonup> (Haskell) "Lazy.delay"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   120
| constant force \<rightharpoonup> (Haskell) "Lazy.force"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   121
code_reserved Haskell Lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   122
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   123
code_printing
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   124
  code_module Lazy \<rightharpoonup> (Scala) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   125
\<open>object Lazy {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   126
  final class Lazy[A] (f: Unit => A) {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   127
    var evaluated = false;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   128
    lazy val x: A = f ()
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   129
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   130
    def get() : A = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   131
      evaluated = true;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   132
      return x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   133
    }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   134
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   135
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   136
  def force[A] (x: Lazy[A]) : A = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   137
    return x.get()
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   138
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   139
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   140
  def delay[A] (f: Unit => A) : Lazy[A] = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   141
    return new Lazy[A] (f)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   142
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   143
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   144
  def termify_lazy[Typerep, Term, A] (
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   145
    const: String => Typerep => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   146
    app: Term => Term => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   147
    abs: String => Typerep => Term => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   148
    unitT: Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   149
    funT: Typerep => Typerep => Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   150
    lazyT: Typerep => Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   151
    term_of: A => Term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   152
    ty: Typerep,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   153
    x: Lazy[A],
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   154
    dummy: Term) : Term = {
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   155
    if (x.evaluated)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   156
      app(const("Code_Lazy.delay")(funT(funT(unitT)(ty))(lazyT(ty))))(abs("_")(unitT)(term_of(x.get)))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   157
    else
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   158
      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
   159
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   160
}\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   161
| type_constructor lazy \<rightharpoonup> (Scala) "Lazy.Lazy[_]"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   162
| constant delay \<rightharpoonup> (Scala) "Lazy.delay"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   163
| constant force \<rightharpoonup> (Scala) "Lazy.force"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   164
code_reserved Scala Lazy termify_lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   165
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   166
code_printing
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   167
  type_constructor lazy \<rightharpoonup> (OCaml) "_ Lazy.t"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   168
| constant delay \<rightharpoonup> (OCaml) "Lazy.from'_fun"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   169
| constant force \<rightharpoonup> (OCaml) "Lazy.force"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   170
code_reserved OCaml Lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   171
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   172
text \<open>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   173
  Term reconstruction for lazy looks into the lazy value and reconstructs it to the depth it has been evaluated.
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   174
  This is not done for Haskell and Scala as we do not know of any portable way to inspect whether a lazy value
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   175
  has been evaluated to or not.
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   176
\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   177
code_printing code_module Termify_Lazy \<rightharpoonup> (Eval) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   178
\<open>structure Termify_Lazy = struct
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   179
fun termify_lazy 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   180
  (_: string -> typ -> term) (_: term -> term -> term)  (_: string -> typ -> term -> term)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   181
  (_: typ) (_: typ -> typ -> typ) (_: typ -> typ)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   182
  (term_of: 'a -> term) (T: typ) (x: 'a Lazy.lazy) (_: term) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   183
  Const ("Code_Lazy.delay", (HOLogic.unitT --> T) --> Type ("Code_Lazy.lazy", [T])) $ 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   184
  (case Lazy.peek x of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   185
    SOME (Exn.Res x) => absdummy HOLogic.unitT (term_of x)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   186
  | _ => Const ("Pure.dummy_pattern", HOLogic.unitT --> T));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   187
end;\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   188
code_reserved Eval Termify_Lazy TERMIFY_LAZY termify_lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   189
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   190
code_printing code_module Termify_Lazy \<rightharpoonup> (OCaml) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   191
\<open>module Termify_Lazy : sig
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   192
  val termify_lazy :
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   193
   (string -> 'typerep -> 'term) -> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   194
   ('term -> 'term -> 'term) -> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   195
   (string -> 'typerep -> 'term -> 'term) ->
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   196
   'typerep -> ('typerep -> 'typerep -> 'typerep) -> ('typerep -> 'typerep) ->
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   197
   ('a -> 'term) -> 'typerep -> 'a Lazy.t -> 'term -> 'term
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   198
end = struct
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   199
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   200
let termify_lazy const app abs unitT funT lazyT term_of ty x _ =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   201
  app (const "Code_Lazy.delay" (funT (funT unitT ty) (lazyT ty))) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   202
    (if Lazy.is_val x then abs "_" unitT (term_of (Lazy.force x))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   203
     else const "Pure.dummy_pattern" (funT unitT ty));;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   204
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   205
end;;\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   206
code_reserved OCaml Termify_Lazy termify_lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   207
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   208
definition termify_lazy2 :: "'a :: typerep lazy \<Rightarrow> term"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   209
where "termify_lazy2 x =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   210
  Code_Evaluation.App (Code_Evaluation.Const (STR ''Code_Lazy.delay'') (TYPEREP((unit \<Rightarrow> 'a) \<Rightarrow> 'a lazy)))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   211
    (Code_Evaluation.Const (STR ''Pure.dummy_pattern'') (TYPEREP((unit \<Rightarrow> 'a))))"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   212
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   213
definition termify_lazy :: 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   214
  "(String.literal \<Rightarrow> 'typerep \<Rightarrow> 'term) \<Rightarrow> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   215
   ('term \<Rightarrow> 'term \<Rightarrow> 'term) \<Rightarrow> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   216
   (String.literal \<Rightarrow> 'typerep \<Rightarrow> 'term \<Rightarrow> 'term) \<Rightarrow>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   217
   'typerep \<Rightarrow> ('typerep \<Rightarrow> 'typerep \<Rightarrow> 'typerep) \<Rightarrow> ('typerep \<Rightarrow> 'typerep) \<Rightarrow>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   218
   ('a \<Rightarrow> 'term) \<Rightarrow> 'typerep \<Rightarrow> 'a :: typerep lazy \<Rightarrow> 'term \<Rightarrow> term"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   219
where "termify_lazy _ _ _ _ _ _ _ _ x _ = termify_lazy2 x"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   220
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   221
declare [[code drop: "Code_Evaluation.term_of :: _ lazy \<Rightarrow> _"]]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   222
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   223
lemma term_of_lazy_code [code]:
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   224
  "Code_Evaluation.term_of x \<equiv> 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   225
   termify_lazy 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   226
     Code_Evaluation.Const Code_Evaluation.App Code_Evaluation.Abs 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   227
     TYPEREP(unit) (\<lambda>T U. typerep.Typerep (STR ''fun'') [T, U]) (\<lambda>T. typerep.Typerep (STR ''Code_Lazy.lazy'') [T])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   228
     Code_Evaluation.term_of TYPEREP('a) x (Code_Evaluation.Const (STR '''') (TYPEREP(unit)))"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   229
  for x :: "'a :: {typerep, term_of} lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   230
by(rule term_of_anything)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   231
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   232
code_printing constant termify_lazy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   233
  \<rightharpoonup> (SML) "Lazy.termify'_lazy" 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   234
  and (Eval) "Termify'_Lazy.termify'_lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   235
  and (OCaml) "Termify'_Lazy.termify'_lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   236
  and (Scala) "Lazy.termify'_lazy"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   237
  
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   238
text \<open>Make evaluation with the simplifier respect @{term delay}s.\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   239
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   240
lemma delay_lazy_cong: "delay f = delay f" by simp
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   241
setup \<open>Code_Simp.map_ss (Simplifier.add_cong @{thm delay_lazy_cong})\<close>        
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   242
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   243
subsection \<open>Implementation\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   244
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   245
ML_file "code_lazy.ML"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   246
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   247
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
   248
  Code_Preproc.add_functrans ("lazy_datatype", Code_Lazy.transform_code_eqs)
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   249
\<close>
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   250
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   251
end