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