src/HOL/Library/code_lazy.ML
author wenzelm
Fri, 14 Aug 2020 14:40:24 +0200
changeset 72154 2b41b710f6ef
parent 69597 ff784d5a5bfb
child 72450 24bd1316eaae
permissions -rw-r--r--
clarified signature;
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
signature CODE_LAZY =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     5
sig
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     6
  type lazy_info =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     7
    {eagerT: typ,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     8
     lazyT: typ,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
     9
     ctr: term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    10
     destr: term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    11
     lazy_ctrs: term list,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    12
     case_lazy: term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    13
     active: bool,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    14
     activate: theory -> theory,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    15
     deactivate: theory -> theory};
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    16
  val code_lazy_type: string -> theory -> theory
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    17
  val activate_lazy_type: string -> theory -> theory
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    18
  val deactivate_lazy_type: string -> theory -> theory
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    19
  val activate_lazy_types: theory -> theory
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    20
  val deactivate_lazy_types: theory -> theory
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    21
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    22
  val get_lazy_types: theory -> (string * lazy_info) list
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    23
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    24
  val print_lazy_types: theory -> unit
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    25
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    26
  val transform_code_eqs: Proof.context -> (thm * bool) list -> (thm * bool) list option
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    27
end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    28
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    29
structure Code_Lazy : CODE_LAZY =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    30
struct
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    31
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    32
type lazy_info =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    33
  {eagerT: typ,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    34
   lazyT: typ,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    35
   ctr: term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    36
   destr: term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    37
   lazy_ctrs: term list,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    38
   case_lazy: term,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    39
   active: bool,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    40
   activate: theory -> theory,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    41
   deactivate: theory -> theory};
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    42
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    43
fun map_active f {eagerT, lazyT, ctr, destr, lazy_ctrs, case_lazy, active, activate, deactivate} =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    44
  { eagerT = eagerT, 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    45
    lazyT = lazyT,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    46
    ctr = ctr,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    47
    destr = destr,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    48
    lazy_ctrs = lazy_ctrs,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    49
    case_lazy = case_lazy,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    50
    active = f active,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    51
    activate = activate,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    52
    deactivate = deactivate
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    53
  }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    54
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    55
structure Laziness_Data = Theory_Data(
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    56
  type T = lazy_info Symtab.table;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    57
  val empty = Symtab.empty;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    58
  val merge = Symtab.join (fn _ => fn (_, record) => record);
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    59
  val extend = I;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    60
);
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    61
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    62
fun fold_type type' tfree tvar typ =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    63
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    64
    fun go (Type (s, Ts)) = type' (s, map go Ts)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    65
      | go (TFree T) = tfree T
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    66
      | go (TVar T) = tvar T
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    67
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    68
    go typ
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    69
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    70
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    71
fun read_typ lthy name =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    72
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    73
    val (s, Ts) = Proof_Context.read_type_name {proper = true, strict = true} lthy name |> dest_Type
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    74
    val (Ts', _) = Ctr_Sugar_Util.mk_TFrees (length Ts) lthy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    75
  in 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    76
    Type (s, Ts')
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    77
  end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    78
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    79
fun mk_name prefix suffix name ctxt =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    80
  Ctr_Sugar_Util.mk_fresh_names ctxt 1 (prefix ^ name ^ suffix) |>> hd;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    81
fun generate_typedef_name name ctxt = mk_name "" "_lazy" name ctxt;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    82
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    83
fun add_syntax_definition short_type_name eagerT lazyT lazy_ctr lthy = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    84
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    85
    val (name, _) = mk_name "lazy_" "" short_type_name lthy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    86
    val freeT = HOLogic.unitT --> lazyT
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
    87
    val lazyT' = Type (\<^type_name>\<open>lazy\<close>, [lazyT])
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    88
    val def = Logic.all_const freeT $ absdummy freeT (Logic.mk_equals (
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    89
      Free (name, (freeT --> eagerT)) $ Bound 0,
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
    90
      lazy_ctr $ (Const (\<^const_name>\<open>delay\<close>, (freeT --> lazyT')) $ Bound 0)))
72154
2b41b710f6ef clarified signature;
wenzelm
parents: 69597
diff changeset
    91
    val lthy' = Local_Theory.open_target lthy
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    92
    val ((t, (_, thm)), lthy') = Specification.definition NONE [] [] 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    93
      ((Thm.def_binding (Binding.name name), []), def) lthy'
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    94
    val lthy' = Specification.notation true ("", false) [(t, Mixfix.mixfix "_")] lthy'
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    95
    val lthy = Local_Theory.close_target lthy'
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    96
    val def_thm = singleton (Proof_Context.export lthy' lthy) thm
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    97
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    98
    (def_thm, lthy)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
    99
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   100
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   101
fun add_ctr_code raw_ctrs case_thms thy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   102
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   103
    fun mk_case_certificate ctxt raw_thms =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   104
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   105
        val thms = raw_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   106
          |> Conjunction.intr_balanced
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   107
          |> Thm.unvarify_global (Proof_Context.theory_of ctxt)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   108
          |> Conjunction.elim_balanced (length raw_thms)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   109
          |> map Simpdata.mk_meta_eq
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   110
          |> map Drule.zero_var_indexes
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   111
        val thm1 = case thms of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   112
          thm :: _ => thm
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   113
          | _ => raise Empty
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   114
        val params = Term.add_free_names (Thm.prop_of thm1) [];
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   115
        val rhs = thm1
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   116
          |> Thm.prop_of |> Logic.dest_equals |> fst |> strip_comb
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   117
          ||> fst o split_last |> list_comb
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   118
        val lhs = Free (singleton (Name.variant_list params) "case", fastype_of rhs);
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   119
        val assum = Thm.cterm_of ctxt (Logic.mk_equals (lhs, rhs))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   120
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   121
        thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   122
        |> Conjunction.intr_balanced
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   123
        |> rewrite_rule ctxt [Thm.symmetric (Thm.assume assum)]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   124
        |> Thm.implies_intr assum
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   125
        |> Thm.generalize ([], params) 0
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   126
        |> Axclass.unoverload ctxt
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   127
        |> Thm.varifyT_global
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   128
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   129
    val ctrs = map (apsnd (perhaps (try Logic.unvarifyT_global))) raw_ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   130
    val unover_ctrs = map (fn ctr as (_, fcT) => (Axclass.unoverload_const thy ctr, fcT)) ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   131
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   132
    if can (Code.constrset_of_consts thy) unover_ctrs then
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   133
      thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   134
      |> Code.declare_datatype_global ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   135
      |> fold_rev (Code.add_eqn_global o rpair true) case_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   136
      |> Code.declare_case_global (mk_case_certificate (Proof_Context.init_global thy) case_thms)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   137
    else
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   138
      thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   139
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   140
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   141
fun not_found s = error (s ^ " has not been added as lazy type");
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   142
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   143
fun validate_type_name thy type_name =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   144
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   145
    val lthy = Named_Target.theory_init thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   146
    val eager_type = read_typ lthy type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   147
    val type_name = case eager_type of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   148
      Type (s, _) => s
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   149
      | _ => raise Match
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   150
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   151
    type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   152
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   153
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   154
fun set_active_lazy_type value eager_type_string thy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   155
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   156
    val type_name = validate_type_name thy eager_type_string
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   157
    val x =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   158
      case Symtab.lookup (Laziness_Data.get thy) type_name of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   159
        NONE => not_found type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   160
        | SOME x => x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   161
    val new_x = map_active (K value) x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   162
    val thy1 = if value = #active x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   163
      then thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   164
      else if value
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   165
        then #activate x thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   166
        else #deactivate x thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   167
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   168
    Laziness_Data.map (Symtab.update (type_name, new_x)) thy1
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   169
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   170
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   171
fun set_active_lazy_types value thy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   172
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   173
    val lazy_type_names = Symtab.keys (Laziness_Data.get thy)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   174
    fun fold_fun value type_name thy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   175
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   176
        val x =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   177
          case Symtab.lookup (Laziness_Data.get thy) type_name of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   178
            SOME x => x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   179
            | NONE => raise Match
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   180
        val new_x = map_active (K value) x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   181
        val thy1 = if value = #active x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   182
          then thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   183
          else if value
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   184
            then #activate x thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   185
            else #deactivate x thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   186
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   187
        Laziness_Data.map (Symtab.update (type_name, new_x)) thy1
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   188
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   189
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   190
    fold (fold_fun value) lazy_type_names thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   191
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   192
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   193
(* code_lazy_type : string -> theory -> theory *)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   194
fun code_lazy_type eager_type_string thy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   195
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   196
    val lthy = Named_Target.theory_init thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   197
    val eagerT = read_typ lthy eager_type_string
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   198
    val (type_name, type_args) = dest_Type eagerT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   199
    val short_type_name = Long_Name.base_name type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   200
    val _ = if Symtab.defined (Laziness_Data.get thy) type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   201
      then error (type_name ^ " has already been added as lazy type.")
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   202
      else ()
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   203
    val {case_thms, casex, ctrs, ...} = case Ctr_Sugar.ctr_sugar_of lthy type_name of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   204
        SOME x => x
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   205
      | _ => error (type_name ^ " is not registered with free constructors.")
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   206
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   207
    fun substitute_ctr (old_T, new_T) ctr_T lthy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   208
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   209
        val old_ctr_vars = map TVar (Term.add_tvarsT ctr_T [])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   210
        val old_ctr_Ts = map TFree (Term.add_tfreesT ctr_T []) @ old_ctr_vars
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   211
        val (new_ctr_Ts, lthy') = Ctr_Sugar_Util.mk_TFrees (length old_ctr_Ts) lthy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   212
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   213
        fun double_type_fold Ts = case Ts of
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   214
          (Type (_, Ts1), Type (_, Ts2)) => flat (map double_type_fold (Ts1 ~~ Ts2))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   215
          | (Type _, _) => raise Match
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   216
          | (_, Type _) => raise Match
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   217
          | Ts => [Ts]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   218
        fun map_fun1 f = List.foldr
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   219
          (fn ((T1, T2), f) => fn T => if T = T1 then T2 else f T)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   220
          f
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   221
          (double_type_fold (old_T, new_T))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   222
        val map_fun2 = AList.lookup (op =) (old_ctr_Ts ~~ new_ctr_Ts) #> the
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   223
        val map_fun = map_fun1 map_fun2
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   224
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   225
        val new_ctr_type = fold_type Type (map_fun o TFree) (map_fun o TVar) ctr_T
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   226
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   227
        (new_ctr_type, lthy')
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   228
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   229
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   230
    val (short_lazy_type_name, lthy1) = generate_typedef_name short_type_name lthy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   231
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   232
    fun mk_lazy_typedef (name, eager_type) lthy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   233
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   234
        val binding = Binding.name name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   235
        val (result, lthy1) = (Typedef.add_typedef
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   236
            { overloaded=false }
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   237
            (binding, rev (Term.add_tfreesT eager_type []), Mixfix.NoSyn)
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   238
            (Const (\<^const_name>\<open>top\<close>, Type (\<^type_name>\<open>set\<close>, [eager_type])))
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   239
            NONE
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   240
            (fn ctxt => resolve_tac ctxt [@{thm UNIV_witness}] 1)
72154
2b41b710f6ef clarified signature;
wenzelm
parents: 69597
diff changeset
   241
          o Local_Theory.open_target) lthy
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   242
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   243
         (binding, result, Local_Theory.close_target lthy1)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   244
      end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   245
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   246
    val (typedef_binding, (_, info), lthy2) = mk_lazy_typedef (short_lazy_type_name, eagerT) lthy1
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   247
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   248
    val lazy_type = Type (Local_Theory.full_name lthy2 typedef_binding, type_args)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   249
    val (Abs_lazy, Rep_lazy) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   250
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   251
        val info = fst info
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   252
        val Abs_name = #Abs_name info
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   253
        val Rep_name = #Rep_name info
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   254
        val Abs_type = eagerT --> lazy_type
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   255
        val Rep_type = lazy_type --> eagerT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   256
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   257
        (Const (Abs_name, Abs_type), Const (Rep_name, Rep_type))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   258
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   259
    val Abs_inverse = #Abs_inverse (snd info)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   260
    val Rep_inverse = #Rep_inverse (snd info)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   261
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   262
    val (ctrs', lthy3) = List.foldr
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   263
      (fn (Const (s, T), (ctrs, lthy)) => let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   264
            val (T', lthy') = substitute_ctr (body_type T, eagerT) T lthy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   265
          in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   266
            ((Const (s, T')) :: ctrs, lthy')
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   267
          end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   268
      )
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   269
      ([], lthy2)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   270
      ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   271
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   272
    fun to_destr_eagerT typ = case typ of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   273
          Type (\<^type_name>\<open>fun\<close>, [_, Type (\<^type_name>\<open>fun\<close>, Ts)]) => 
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   274
          to_destr_eagerT (Type (\<^type_name>\<open>fun\<close>, Ts))
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   275
        | Type (\<^type_name>\<open>fun\<close>, [T, _]) => T
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   276
        | _ => raise Match
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   277
    val (case', lthy4) = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   278
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   279
        val (eager_case, caseT) = dest_Const casex
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   280
        val (caseT', lthy') = substitute_ctr (to_destr_eagerT caseT, eagerT) caseT lthy3
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   281
      in (Const (eager_case, caseT'), lthy') end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   282
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   283
    val ctr_names = map (Long_Name.base_name o fst o dest_Const) ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   284
    val ((((lazy_ctr_name, lazy_sel_name), lazy_ctrs_name), lazy_case_name), ctxt) = lthy4
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   285
      |> mk_name "Lazy_" "" short_type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   286
      ||>> mk_name "unlazy_" "" short_type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   287
      ||>> fold_map (mk_name "" "_Lazy") ctr_names
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   288
      ||>> mk_name "case_" "_lazy" short_type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   289
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   290
    fun mk_def (name, T, rhs) lthy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   291
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   292
        val binding = Binding.name name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   293
        val ((_, (_, thm)), lthy1) = 
72154
2b41b710f6ef clarified signature;
wenzelm
parents: 69597
diff changeset
   294
          Local_Theory.open_target lthy
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   295
          |> Specification.definition NONE [] [] ((Thm.def_binding binding, []), rhs)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   296
        val lthy2 = Local_Theory.close_target lthy1
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   297
        val def_thm = hd (Proof_Context.export lthy1 lthy2 [thm])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   298
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   299
        ({binding = binding, const = Const (Local_Theory.full_name lthy2 binding, T), thm = def_thm}, lthy2)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   300
      end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   301
    
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   302
    val lazy_datatype = Type (\<^type_name>\<open>lazy\<close>, [lazy_type])
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   303
    val Lazy_type = lazy_datatype --> eagerT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   304
    val unstr_type = eagerT --> lazy_datatype
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   305
    
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   306
    fun apply_bounds i n term =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   307
      if n > i then apply_bounds i (n-1) (term $ Bound (n-1))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   308
      else term
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   309
    fun all_abs Ts t = Logic.list_all (map (pair Name.uu) Ts, t)
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   310
    fun mk_force t = Const (\<^const_name>\<open>force\<close>, lazy_datatype --> lazy_type) $ t
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   311
    fun mk_delay t = Const (\<^const_name>\<open>delay\<close>, (\<^typ>\<open>unit\<close> --> lazy_type) --> lazy_datatype) $ t
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   312
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   313
    val lazy_ctr = all_abs [lazy_datatype]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   314
      (Logic.mk_equals (Free (lazy_ctr_name, Lazy_type) $ Bound 0, Rep_lazy $ mk_force (Bound 0)))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   315
    val (lazy_ctr_def, lthy5) = mk_def (lazy_ctr_name, Lazy_type, lazy_ctr) lthy4
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   316
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   317
    val lazy_sel = all_abs [eagerT]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   318
      (Logic.mk_equals (Free (lazy_sel_name, unstr_type) $ Bound 0, 
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   319
        mk_delay (Abs (Name.uu, \<^typ>\<open>unit\<close>, Abs_lazy $ Bound 1))))
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   320
    val (lazy_sel_def, lthy6) = mk_def (lazy_sel_name, unstr_type, lazy_sel) lthy5
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   321
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   322
    fun mk_lazy_ctr (name, eager_ctr) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   323
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   324
        val (_, ctrT) = dest_Const eager_ctr
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   325
        fun to_lazy_ctrT (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) = T1 --> to_lazy_ctrT T2
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   326
          | to_lazy_ctrT T = if T = eagerT then lazy_type else raise Match
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   327
        val lazy_ctrT = to_lazy_ctrT ctrT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   328
        val argsT = binder_types ctrT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   329
        val lhs = apply_bounds 0 (length argsT) (Free (name, lazy_ctrT))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   330
        val rhs = Abs_lazy $ apply_bounds 0 (length argsT) eager_ctr
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   331
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   332
        mk_def (name, lazy_ctrT, all_abs argsT (Logic.mk_equals (lhs, rhs)))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   333
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   334
    val (lazy_ctrs_def, lthy7) = fold_map mk_lazy_ctr (lazy_ctrs_name ~~ ctrs') lthy6
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   335
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   336
    val (lazy_case_def, lthy8) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   337
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   338
        val (_, caseT) = dest_Const case'
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   339
        fun to_lazy_caseT (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) =
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   340
            if T1 = eagerT then lazy_type --> T2 else T1 --> to_lazy_caseT T2
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   341
        val lazy_caseT = to_lazy_caseT caseT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   342
        val argsT = binder_types lazy_caseT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   343
        val n = length argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   344
        val lhs = apply_bounds 0 n (Free (lazy_case_name, lazy_caseT)) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   345
        val rhs = apply_bounds 1 n case' $ (Rep_lazy $ Bound 0)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   346
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   347
        mk_def (lazy_case_name, lazy_caseT, all_abs argsT (Logic.mk_equals (lhs, rhs))) lthy7
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   348
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   349
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   350
    fun mk_thm ((name, term), thms) lthy =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   351
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   352
        val binding = Binding.name name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   353
        fun tac {context, ...} = Simplifier.simp_tac
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   354
          (put_simpset HOL_basic_ss context addsimps thms)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   355
          1
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   356
        val thm = Goal.prove lthy [] [] term tac
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   357
        val (_, lthy1) = lthy
72154
2b41b710f6ef clarified signature;
wenzelm
parents: 69597
diff changeset
   358
          |> Local_Theory.open_target
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   359
          |> Local_Theory.note ((binding, []), [thm])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   360
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   361
        (thm, Local_Theory.close_target lthy1)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   362
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   363
    fun mk_thms exec_list lthy = fold_map mk_thm exec_list lthy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   364
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   365
    val mk_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   366
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   367
    val lazy_ctrs = map #const lazy_ctrs_def
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   368
    val eager_lazy_ctrs = ctrs' ~~ lazy_ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   369
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   370
    val (((((((Lazy_delay_eq_name, Rep_ctr_names), ctrs_lazy_names), force_sel_name), case_lazy_name),
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   371
      sel_lazy_name), case_ctrs_name), _) = lthy5
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   372
      |> mk_name "Lazy_" "_delay" short_type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   373
      ||>> fold_map (mk_name "Rep_" "_Lazy") ctr_names
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   374
      ||>> fold_map (mk_name "" "_conv_lazy") ctr_names
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   375
      ||>> mk_name "force_unlazy_" "" short_type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   376
      ||>> mk_name "case_" "_conv_lazy" short_type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   377
      ||>> mk_name "Lazy_" "_inverse" short_type_name
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   378
      ||>> fold_map (mk_name ("case_" ^ short_type_name ^ "_lazy_") "") ctr_names
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   379
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   380
    val mk_Lazy_delay_eq =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69593
diff changeset
   381
      (#const lazy_ctr_def $ mk_delay (Bound 0), Rep_lazy $ (Bound 0 $ \<^const>\<open>Unity\<close>))
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   382
      |> mk_eq |> all_abs [\<^typ>\<open>unit\<close> --> lazy_type]
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   383
    val (Lazy_delay_thm, lthy8a) = mk_thm 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   384
      ((Lazy_delay_eq_name, mk_Lazy_delay_eq), [#thm lazy_ctr_def, @{thm force_delay}])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   385
      lthy8
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   386
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   387
    fun mk_lazy_ctr_eq (eager_ctr, lazy_ctr) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   388
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   389
        val (_, ctrT) = dest_Const eager_ctr
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   390
        val argsT = binder_types ctrT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   391
        val args = length argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   392
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   393
        (Rep_lazy $ apply_bounds 0 args lazy_ctr, apply_bounds 0 args eager_ctr)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   394
        |> mk_eq |> all_abs argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   395
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   396
    val Rep_ctr_eqs = map mk_lazy_ctr_eq eager_lazy_ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   397
    val (Rep_ctr_thms, lthy8b) = mk_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   398
        ((Rep_ctr_names ~~ Rep_ctr_eqs) ~~
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   399
          (map (fn def => [#thm def, Abs_inverse, @{thm UNIV_I}]) lazy_ctrs_def)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   400
        )
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   401
        lthy8a
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   402
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   403
    fun mk_ctrs_lazy_eq (eager_ctr, lazy_ctr) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   404
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   405
        val argsT = dest_Const eager_ctr |> snd |> binder_types
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   406
        val n = length argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   407
        val lhs = apply_bounds 0 n eager_ctr
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   408
        val rhs = #const lazy_ctr_def $ 
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   409
          (mk_delay (Abs (Name.uu, \<^typ>\<open>unit\<close>, apply_bounds 1 (n + 1) lazy_ctr)))
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   410
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   411
        (lhs, rhs) |> mk_eq |> all_abs argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   412
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   413
    val ctrs_lazy_eq = map mk_ctrs_lazy_eq eager_lazy_ctrs 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   414
    val (ctrs_lazy_thms, lthy8c) = mk_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   415
      ((ctrs_lazy_names ~~ ctrs_lazy_eq) ~~ map (fn thm => [Lazy_delay_thm, thm]) Rep_ctr_thms)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   416
      lthy8b
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   417
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   418
    val force_sel_eq = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   419
      (mk_force (#const lazy_sel_def $ Bound 0), Abs_lazy $ Bound 0)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   420
      |> mk_eq |> all_abs [eagerT]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   421
    val (force_sel_thm, lthy8d) = mk_thm
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   422
        ((force_sel_name, force_sel_eq), [#thm lazy_sel_def, @{thm force_delay}])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   423
        lthy8c
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   424
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   425
    val case_lazy_eq = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   426
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   427
        val (_, caseT) = case' |> dest_Const
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   428
        val argsT = binder_types caseT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   429
        val n = length argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   430
        val lhs = apply_bounds 0 n case'
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   431
        val rhs = apply_bounds 1 n (#const lazy_case_def) $ (mk_force (#const lazy_sel_def $ Bound 0))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   432
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   433
        (lhs, rhs) |> mk_eq |> all_abs argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   434
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   435
    val (case_lazy_thm, lthy8e) = mk_thm
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   436
        ((case_lazy_name, case_lazy_eq), 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   437
        [#thm lazy_case_def, force_sel_thm, Abs_inverse, @{thm UNIV_I}])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   438
        lthy8d
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   439
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   440
    val sel_lazy_eq =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   441
      (#const lazy_sel_def $ (#const lazy_ctr_def $ Bound 0), Bound 0)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   442
      |> mk_eq |> all_abs [lazy_datatype]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   443
    val (sel_lazy_thm, lthy8f) = mk_thm
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   444
      ((sel_lazy_name, sel_lazy_eq),
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   445
      [#thm lazy_sel_def, #thm lazy_ctr_def, Rep_inverse, @{thm delay_force}])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   446
      lthy8e
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   447
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   448
    fun mk_case_ctrs_eq (i, lazy_ctr) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   449
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   450
        val lazy_case = #const lazy_case_def
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   451
        val (_, ctrT) = dest_Const lazy_ctr
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   452
        val ctr_argsT = binder_types ctrT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   453
        val ctr_args_n = length ctr_argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   454
        val (_, caseT) = dest_Const lazy_case
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   455
        val case_argsT = binder_types caseT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   456
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   457
        fun n_bounds_from m n t =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   458
          if n > 0 then n_bounds_from (m - 1) (n - 1) (t $ Bound (m - 1)) else t
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   459
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   460
        val case_argsT' = take (length case_argsT - 1) case_argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   461
        val Ts = case_argsT' @ ctr_argsT
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   462
        val num_abs_types = length Ts
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   463
        val lhs = n_bounds_from num_abs_types (length case_argsT') lazy_case $
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   464
          apply_bounds 0 ctr_args_n lazy_ctr
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   465
        val rhs = apply_bounds 0 ctr_args_n (Bound (num_abs_types - i - 1))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   466
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   467
        (lhs, rhs) |> mk_eq |> all_abs Ts
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   468
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   469
    val case_ctrs_eq = map_index mk_case_ctrs_eq lazy_ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   470
    val (case_ctrs_thms, lthy9) = mk_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   471
        ((case_ctrs_name ~~ case_ctrs_eq) ~~
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   472
         map2 (fn thm1 => fn thm2 => [#thm lazy_case_def, thm1, thm2]) Rep_ctr_thms case_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   473
        )
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   474
        lthy8f
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   475
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   476
    val (pat_def_thm, lthy10) = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   477
      add_syntax_definition short_type_name eagerT lazy_type (#const lazy_ctr_def) lthy9
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   478
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   479
    val add_lazy_ctrs =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   480
      Code.declare_datatype_global [dest_Const (#const lazy_ctr_def)]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   481
    val eager_ctrs = map (apsnd (perhaps (try Logic.unvarifyT_global)) o dest_Const) ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   482
    val add_eager_ctrs =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   483
      fold Code.del_eqn_global ctrs_lazy_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   484
      #> Code.declare_datatype_global eager_ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   485
    val add_code_eqs = fold (Code.add_eqn_global o rpair true) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   486
      ([case_lazy_thm, sel_lazy_thm])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   487
    val add_lazy_ctr_thms = fold (Code.add_eqn_global o rpair true) ctrs_lazy_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   488
    val add_lazy_case_thms =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   489
      fold Code.del_eqn_global case_thms
68549
bbc742358156 declare case theorems as proper code equations
Andreas Lochbihler
parents: 68155
diff changeset
   490
      #> Code.add_eqn_global (case_lazy_thm, true)
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   491
    val add_eager_case_thms = Code.del_eqn_global case_lazy_thm
68549
bbc742358156 declare case theorems as proper code equations
Andreas Lochbihler
parents: 68155
diff changeset
   492
      #> fold (Code.add_eqn_global o rpair true) case_thms
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   493
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   494
    val delay_dummy_thm = (pat_def_thm RS @{thm symmetric})
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   495
      |> Drule.infer_instantiate' lthy10
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   496
         [SOME (Thm.cterm_of lthy10 (Const (\<^const_name>\<open>Pure.dummy_pattern\<close>, HOLogic.unitT --> lazy_type)))]
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   497
      |> Thm.generalize (map (fst o dest_TFree) type_args, []) (Variable.maxidx_of lthy10 + 1);
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   498
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   499
    val ctr_post = delay_dummy_thm :: map (fn thm => thm RS @{thm sym}) ctrs_lazy_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   500
    val ctr_thms_abs = map (fn thm => Drule.abs_def (thm RS @{thm eq_reflection})) ctrs_lazy_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   501
    val case_thm_abs = Drule.abs_def (case_lazy_thm RS @{thm eq_reflection})
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   502
    val add_simps = Code_Preproc.map_pre
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   503
      (fn ctxt => ctxt addsimps (case_thm_abs :: ctr_thms_abs))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   504
    val del_simps = Code_Preproc.map_pre
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   505
      (fn ctxt => ctxt delsimps (case_thm_abs :: ctr_thms_abs))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   506
    val add_post = Code_Preproc.map_post
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   507
      (fn ctxt => ctxt addsimps ctr_post)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   508
    val del_post = Code_Preproc.map_post
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   509
      (fn ctxt => ctxt delsimps ctr_post)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   510
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   511
    Local_Theory.exit_global lthy10
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   512
    |> Laziness_Data.map (Symtab.update (type_name,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   513
      {eagerT = eagerT, 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   514
       lazyT = lazy_type,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   515
       ctr = #const lazy_ctr_def,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   516
       destr = #const lazy_sel_def,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   517
       lazy_ctrs = map #const lazy_ctrs_def,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   518
       case_lazy = #const lazy_case_def,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   519
       active = true,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   520
       activate = add_lazy_ctrs #> add_lazy_ctr_thms #> add_lazy_case_thms #> add_simps #> add_post,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   521
       deactivate = add_eager_ctrs #> add_eager_case_thms #> del_simps #> del_post}))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   522
    |> add_lazy_ctrs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   523
    |> add_ctr_code (map (dest_Const o #const) lazy_ctrs_def) case_ctrs_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   524
    |> add_code_eqs
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   525
    |> add_lazy_ctr_thms
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   526
    |> add_simps
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   527
    |> add_post
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   528
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   529
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   530
fun transform_code_eqs _ [] = NONE
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   531
  | transform_code_eqs ctxt eqs =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   532
    let
69568
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   533
      fun replace_ctr ctxt =
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   534
        let 
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   535
          val thy = Proof_Context.theory_of ctxt
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   536
          val table = Laziness_Data.get thy
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   537
        in fn (s1, s2) => case Symtab.lookup table s1 of
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   538
            NONE => false
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   539
          | SOME x => #active x andalso s2 <> (#ctr x |> dest_Const |> fst)
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   540
        end
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   541
      val thy = Proof_Context.theory_of ctxt
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   542
      val table = Laziness_Data.get thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   543
      fun num_consts_fun (_, T) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   544
        let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   545
          val s = body_type T |> dest_Type |> fst
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   546
        in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   547
          if Symtab.defined table s
69568
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   548
          then Ctr_Sugar.ctr_sugar_of ctxt s |> the |> #ctrs |> length
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   549
          else Code.get_type thy s |> fst |> snd |> length
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   550
        end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   551
      val eqs = map (apfst (Thm.transfer thy)) eqs;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   552
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   553
      val ((code_eqs, nbe_eqs), pure) =
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   554
        ((case hd eqs |> fst |> Thm.prop_of of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   555
            Const (\<^const_name>\<open>Pure.eq\<close>, _) $ _ $ _ =>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   556
              (map (apfst (fn x => x RS @{thm meta_eq_to_obj_eq})) eqs, true)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   557
         | _ => (eqs, false))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   558
        |> apfst (List.partition snd))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   559
        handle THM _ => (([], eqs), false)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   560
      val to_original_eq = if pure then map (apfst (fn x => x RS @{thm eq_reflection})) else I
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   561
    in
69568
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   562
      case Case_Converter.to_case ctxt (Case_Converter.replace_by_type replace_ctr) num_consts_fun (map fst code_eqs) of
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   563
          NONE => NONE
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   564
        | SOME thms => SOME (nbe_eqs @ map (rpair true) thms |> to_original_eq)
69568
de09a7261120 new implementation for case_of_simps based on Code_Lazy's pattern matching elimination algorithm
Andreas Lochbihler
parents: 68549
diff changeset
   565
    end
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   566
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   567
val activate_lazy_type = set_active_lazy_type true;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   568
val deactivate_lazy_type = set_active_lazy_type false;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   569
val activate_lazy_types = set_active_lazy_types true;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   570
val deactivate_lazy_types = set_active_lazy_types false;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   571
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   572
fun get_lazy_types thy = Symtab.dest (Laziness_Data.get thy) 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   573
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   574
fun print_lazy_type thy (name, info : lazy_info) = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   575
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   576
    val ctxt = Proof_Context.init_global thy
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   577
    fun pretty_ctr ctr = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   578
      let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   579
        val argsT = dest_Const ctr |> snd |> binder_types
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   580
      in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   581
        Pretty.block [
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   582
          Syntax.pretty_term ctxt ctr,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   583
          Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   584
          Pretty.block (Pretty.separate "" (map (Pretty.quote o Syntax.pretty_typ ctxt) argsT))
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   585
        ]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   586
      end
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   587
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   588
    Pretty.block [
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   589
      Pretty.str (name ^ (if #active info then "" else " (inactive)") ^ ":"),
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   590
      Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   591
      Pretty.block [
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   592
        Syntax.pretty_typ ctxt (#eagerT info),
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   593
        Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   594
        Pretty.str "=",
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   595
        Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   596
        Syntax.pretty_term ctxt (#ctr info),
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   597
        Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   598
        Pretty.block [
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   599
          Pretty.str "(",
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   600
          Syntax.pretty_term ctxt (#destr info),
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   601
          Pretty.str ":",
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   602
          Pretty.brk 1,
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   603
          Syntax.pretty_typ ctxt (Type (\<^type_name>\<open>lazy\<close>, [#lazyT info])),
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   604
          Pretty.str ")"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   605
        ]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   606
      ],
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   607
      Pretty.fbrk,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   608
      Pretty.keyword2 "and",
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   609
      Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   610
      Pretty.block ([
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   611
        Syntax.pretty_typ ctxt (#lazyT info),
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   612
        Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   613
        Pretty.str "=",
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   614
        Pretty.brk 1] @
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   615
        Pretty.separate " |" (map pretty_ctr (#lazy_ctrs info)) @ [
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   616
        Pretty.fbrk,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   617
        Pretty.keyword2 "for",
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   618
        Pretty.brk 1, 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   619
        Pretty.str "case:",
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   620
        Pretty.brk 1,
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   621
        Syntax.pretty_term ctxt (#case_lazy info)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   622
      ])
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   623
    ]
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   624
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   625
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   626
fun print_lazy_types thy = 
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   627
  let
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   628
    fun cmp ((name1, _), (name2, _)) = string_ord (name1, name2)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   629
    val infos = Laziness_Data.get thy |> Symtab.dest |> map (apfst Long_Name.base_name) |> sort cmp
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   630
  in
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   631
    Pretty.writeln_chunks (map (print_lazy_type thy) infos)
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   632
  end;
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   633
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   634
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   635
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   636
  Outer_Syntax.command \<^command_keyword>\<open>code_lazy_type\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   637
    "make a lazy copy of the datatype and activate substitution"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   638
    (Parse.binding >> (fn b => Toplevel.theory (Binding.name_of b |> code_lazy_type)));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   639
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   640
  Outer_Syntax.command \<^command_keyword>\<open>activate_lazy_type\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   641
    "activate substitution on a specific (lazy) type"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   642
    (Parse.binding >> (fn b => Toplevel.theory (Binding.name_of b |> activate_lazy_type)));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   643
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   644
  Outer_Syntax.command \<^command_keyword>\<open>deactivate_lazy_type\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   645
    "deactivate substitution on a specific (lazy) type"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   646
    (Parse.binding >> (fn b => Toplevel.theory (Binding.name_of b |> deactivate_lazy_type)));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   647
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   648
  Outer_Syntax.command \<^command_keyword>\<open>activate_lazy_types\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   649
    "activate substitution on all (lazy) types"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   650
    (pair (Toplevel.theory activate_lazy_types));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   651
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   652
  Outer_Syntax.command \<^command_keyword>\<open>deactivate_lazy_types\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   653
    "deactivate substitution on all (lazy) type"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   654
    (pair (Toplevel.theory deactivate_lazy_types));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   655
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69568
diff changeset
   656
  Outer_Syntax.command \<^command_keyword>\<open>print_lazy_types\<close>
68155
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   657
    "print the types that have been declared as lazy and their substitution state"
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   658
    (pair (Toplevel.theory (tap print_lazy_types)));
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   659
8b50f29a1992 new tool Code_Lazy
Andreas Lochbihler
parents:
diff changeset
   660
end