src/HOL/Tools/code_evaluation.ML
author wenzelm
Mon, 27 Jul 2015 17:44:55 +0200
changeset 60801 7664e0916eec
parent 59621 291934bac95e
child 61667 4b53042d7a40
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Tools/code_evaluation.ML
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     3
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     4
Evaluation and reconstruction of terms in ML.
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     5
*)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     6
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     7
signature CODE_EVALUATION =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     8
sig
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
     9
  val dynamic_value: Proof.context -> term -> term option
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
    10
  val dynamic_value_strict: Proof.context -> term -> term
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
    11
  val dynamic_value_exn: Proof.context -> term -> term Exn.result
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    12
  val static_value: { ctxt: Proof.context, consts: string list, Ts: typ list }
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    13
    -> Proof.context -> term -> term option
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    14
  val static_value_strict: { ctxt: Proof.context, consts: string list, Ts: typ list }
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    15
    -> Proof.context -> term -> term
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    16
  val static_value_exn: { ctxt: Proof.context, consts: string list, Ts: typ list }
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    17
    -> Proof.context -> term -> term Exn.result
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
    18
  val dynamic_conv: Proof.context -> conv
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    19
  val static_conv: { ctxt: Proof.context, consts: string list, Ts: typ list }
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
    20
    -> Proof.context -> conv
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    21
  val put_term: (unit -> term) -> Proof.context -> Proof.context
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    22
  val tracing: string -> 'a -> 'a
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    23
end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    24
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    25
structure Code_Evaluation : CODE_EVALUATION =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    26
struct
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    27
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    28
(** term_of instances **)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    29
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    30
(* formal definition *)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    31
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    32
fun add_term_of tyco raw_vs thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    33
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    34
    val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    35
    val ty = Type (tyco, map TFree vs);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    36
    val lhs = Const (@{const_name term_of}, ty --> @{typ term})
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    37
      $ Free ("x", ty);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    38
    val rhs = @{term "undefined :: term"};
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    39
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    40
    fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    41
      o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    42
  in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    43
    thy
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    44
    |> Class.instantiation ([tyco], vs, @{sort term_of})
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    45
    |> `(fn lthy => Syntax.check_term lthy eq)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    46
    |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    47
    |> snd
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59323
diff changeset
    48
    |> Class.prove_instantiation_exit (fn ctxt => Class.intro_classes_tac ctxt [])
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    49
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    50
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    51
fun ensure_term_of (tyco, (raw_vs, _)) thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    52
  let
48272
db75b4005d9a more direct Sorts.has_instance;
wenzelm
parents: 45429
diff changeset
    53
    val need_inst = not (Sorts.has_instance (Sign.classes_of thy) tyco @{sort term_of})
db75b4005d9a more direct Sorts.has_instance;
wenzelm
parents: 45429
diff changeset
    54
      andalso Sorts.has_instance (Sign.classes_of thy) tyco @{sort typerep};
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    55
  in if need_inst then add_term_of tyco raw_vs thy else thy end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    56
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    57
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    58
(* code equations for datatypes *)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    59
40726
16dcfedc4eb7 keep type variable arguments of datatype constructors in bookkeeping
haftmann
parents: 39567
diff changeset
    60
fun mk_term_of_eq thy ty (c, (_, tys)) =
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    61
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    62
    val t = list_comb (Const (c, tys ---> ty),
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 42402
diff changeset
    63
      map Free (Name.invent_names Name.context "a" tys));
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    64
    val (arg, rhs) =
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59617
diff changeset
    65
      apply2 (Thm.global_cterm_of thy o Logic.unvarify_types_global o Logic.varify_global)
45344
e209da839ff4 added Logic.varify_types_global/unvarify_types_global, which avoids somewhat expensive Term.map_types;
wenzelm
parents: 43329
diff changeset
    66
        (t,
e209da839ff4 added Logic.varify_types_global/unvarify_types_global, which avoids somewhat expensive Term.map_types;
wenzelm
parents: 43329
diff changeset
    67
          map_aterms (fn t as Free (_, ty) => HOLogic.mk_term_of ty t | t => t)
e209da839ff4 added Logic.varify_types_global/unvarify_types_global, which avoids somewhat expensive Term.map_types;
wenzelm
parents: 43329
diff changeset
    68
            (HOLogic.reflect_term t));
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59617
diff changeset
    69
    val cty = Thm.global_ctyp_of thy ty;
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    70
  in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    71
    @{thm term_of_anything}
60801
7664e0916eec tuned signature;
wenzelm
parents: 59621
diff changeset
    72
    |> Thm.instantiate' [SOME cty] [SOME arg, SOME rhs]
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    73
    |> Thm.varifyT_global
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    74
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    75
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    76
fun add_term_of_code tyco raw_vs raw_cs thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    77
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    78
    val algebra = Sign.classes_of thy;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    79
    val vs = map (fn (v, sort) =>
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    80
      (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    81
    val ty = Type (tyco, map TFree vs);
40726
16dcfedc4eb7 keep type variable arguments of datatype constructors in bookkeeping
haftmann
parents: 39567
diff changeset
    82
    val cs = (map o apsnd o apsnd o map o map_atyps)
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    83
      (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 48272
diff changeset
    84
    val const = Axclass.param_of_inst thy (@{const_name term_of}, tyco);
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
    85
    val eqs = map (mk_term_of_eq thy ty) cs;
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    86
 in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    87
    thy
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    88
    |> Code.del_eqns const
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    89
    |> fold Code.add_eqn eqs
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    90
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    91
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    92
fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    93
  let
48272
db75b4005d9a more direct Sorts.has_instance;
wenzelm
parents: 45429
diff changeset
    94
    val has_inst = Sorts.has_instance (Sign.classes_of thy) tyco @{sort term_of};
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    95
  in if has_inst then add_term_of_code tyco raw_vs cs thy else thy end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    96
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    97
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    98
(* code equations for abstypes *)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    99
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   100
fun mk_abs_term_of_eq thy ty abs ty_rep proj =
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   101
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   102
    val arg = Var (("x", 0), ty);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   103
    val rhs = Abs ("y", @{typ term}, HOLogic.reflect_term (Const (abs, ty_rep --> ty) $ Bound 0)) $
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   104
      (HOLogic.mk_term_of ty_rep (Const (proj, ty --> ty_rep) $ arg))
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59617
diff changeset
   105
      |> Thm.global_cterm_of thy;
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59617
diff changeset
   106
    val cty = Thm.global_ctyp_of thy ty;
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   107
  in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   108
    @{thm term_of_anything}
60801
7664e0916eec tuned signature;
wenzelm
parents: 59621
diff changeset
   109
    |> Thm.instantiate' [SOME cty] [SOME (Thm.global_cterm_of thy arg), SOME rhs]
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   110
    |> Thm.varifyT_global
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   111
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   112
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   113
fun add_abs_term_of_code tyco raw_vs abs raw_ty_rep proj thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   114
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   115
    val algebra = Sign.classes_of thy;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   116
    val vs = map (fn (v, sort) =>
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   117
      (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   118
    val ty = Type (tyco, map TFree vs);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   119
    val ty_rep = map_atyps
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   120
      (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_ty_rep;
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 48272
diff changeset
   121
    val const = Axclass.param_of_inst thy (@{const_name term_of}, tyco);
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   122
    val eq = mk_abs_term_of_eq thy ty abs ty_rep proj;
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   123
 in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   124
    thy
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   125
    |> Code.del_eqns const
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   126
    |> Code.add_eqn eq
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   127
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   128
40726
16dcfedc4eb7 keep type variable arguments of datatype constructors in bookkeeping
haftmann
parents: 39567
diff changeset
   129
fun ensure_abs_term_of_code (tyco, (raw_vs, ((abs, (_, ty)), (proj, _)))) thy =
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   130
  let
48272
db75b4005d9a more direct Sorts.has_instance;
wenzelm
parents: 45429
diff changeset
   131
    val has_inst = Sorts.has_instance (Sign.classes_of thy) tyco @{sort term_of};
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   132
  in if has_inst then add_abs_term_of_code tyco raw_vs abs ty proj thy else thy end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   133
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   134
56926
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   135
(* setup *)
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   136
59323
468bd3aedfa1 modernized and more uniform style
haftmann
parents: 59153
diff changeset
   137
val _ = Theory.setup
56926
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   138
  (Code.datatype_interpretation ensure_term_of
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   139
  #> Code.abstype_interpretation ensure_term_of
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   140
  #> Code.datatype_interpretation ensure_term_of_code
59323
468bd3aedfa1 modernized and more uniform style
haftmann
parents: 59153
diff changeset
   141
  #> Code.abstype_interpretation ensure_abs_term_of_code);
56926
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   142
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   143
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   144
(** termifying syntax **)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   145
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   146
fun map_default f xs =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   147
  let val ys = map f xs
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   148
  in if exists is_some ys
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   149
    then SOME (map2 the_default xs ys)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   150
    else NONE
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   151
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   152
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   153
fun subst_termify_app (Const (@{const_name termify}, _), [t]) =
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   154
      if not (Term.has_abs t)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   155
      then if fold_aterms (fn Const _ => I | _ => K false) t true
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   156
        then SOME (HOLogic.reflect_term t)
51714
88f7f38d5cb9 spelling
haftmann
parents: 51685
diff changeset
   157
        else error "Cannot termify expression containing variable"
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   158
      else error "Cannot termify expression containing abstraction"
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   159
  | subst_termify_app (t, ts) = case map_default subst_termify ts
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   160
     of SOME ts' => SOME (list_comb (t, ts'))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   161
      | NONE => NONE
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   162
and subst_termify (Abs (v, T, t)) = (case subst_termify t
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   163
     of SOME t' => SOME (Abs (v, T, t'))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   164
      | NONE => NONE)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   165
  | subst_termify t = subst_termify_app (strip_comb t) 
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   166
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   167
fun check_termify _ ts = the_default ts (map_default subst_termify ts);
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   168
56926
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   169
val _ = Context.>> (Syntax_Phases.term_check 0 "termify" check_termify);
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
   170
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   171
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   172
(** evaluation **)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   173
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 41247
diff changeset
   174
structure Evaluation = Proof_Data
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 41247
diff changeset
   175
(
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   176
  type T = unit -> term
59153
wenzelm
parents: 59151
diff changeset
   177
  val empty: T = fn () => raise Fail "Evaluation"
wenzelm
parents: 59151
diff changeset
   178
  fun init _ = empty
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   179
);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   180
val put_term = Evaluation.put;
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   181
val cookie = (Evaluation.get, put_term, "Code_Evaluation.put_term");
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   182
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   183
fun mk_term_of t = HOLogic.mk_term_of (fastype_of t) t;
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   184
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 48272
diff changeset
   185
fun term_of_const_for thy = Axclass.unoverload_const thy o dest_Const o HOLogic.term_of_const;
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   186
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   187
fun gen_dynamic_value dynamic_value ctxt t =
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   188
  dynamic_value cookie ctxt NONE I (mk_term_of t) [];
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   189
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   190
val dynamic_value = gen_dynamic_value Code_Runtime.dynamic_value;
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   191
val dynamic_value_strict = gen_dynamic_value Code_Runtime.dynamic_value_strict;
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   192
val dynamic_value_exn = gen_dynamic_value Code_Runtime.dynamic_value_exn;
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   193
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   194
fun gen_static_value static_value { ctxt, consts, Ts } =
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   195
  let
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   196
    val static_value' = static_value cookie
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   197
      { ctxt = ctxt, target = NONE, lift_postproc = I, consts =
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   198
        union (op =) (map (term_of_const_for (Proof_Context.theory_of ctxt)) Ts) consts }
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   199
  in fn ctxt' => fn t => static_value' ctxt' (mk_term_of t) end;
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   200
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   201
val static_value = gen_static_value Code_Runtime.static_value;
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   202
val static_value_strict = gen_static_value Code_Runtime.static_value_strict;
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   203
val static_value_exn = gen_static_value Code_Runtime.static_value_exn;
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   204
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   205
fun certify_eval ctxt value conv ct =
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   206
  let
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   207
    val t = Thm.term_of ct;
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   208
    val T = fastype_of t;
59617
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59580
diff changeset
   209
    val mk_eq =
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59617
diff changeset
   210
      Thm.mk_binop (Thm.cterm_of ctxt (Const (@{const_name Pure.eq}, T --> T --> propT)));
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   211
  in case value ctxt t
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   212
   of NONE => Thm.reflexive ct
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59617
diff changeset
   213
    | SOME t' => conv ctxt (mk_eq ct (Thm.cterm_of ctxt t')) RS @{thm eq_eq_TrueD}
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   214
        handle THM _ =>
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   215
          error ("Failed to certify evaluation result of " ^ Syntax.string_of_term ctxt t)
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   216
  end;
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   217
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   218
fun dynamic_conv ctxt = certify_eval ctxt dynamic_value
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   219
  Code_Runtime.dynamic_holds_conv;
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   220
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   221
fun static_conv { ctxt, consts, Ts }  =
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   222
  let
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55757
diff changeset
   223
    val eqs = @{const_name Pure.eq} :: @{const_name HOL.eq} ::
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   224
      map (fn T => Axclass.unoverload_const (Proof_Context.theory_of ctxt)
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55757
diff changeset
   225
        (@{const_name HOL.equal}, T)) Ts; (*assumes particular code equations for Pure.eq etc.*)
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   226
    val value = static_value { ctxt = ctxt, consts = consts, Ts = Ts };
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56926
diff changeset
   227
    val holds = Code_Runtime.static_holds_conv { ctxt = ctxt, consts = union (op =) eqs consts };
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   228
  in
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51714
diff changeset
   229
    fn ctxt' => certify_eval ctxt' value holds
39567
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   230
  end;
5ee997fbe5cc dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
haftmann
parents: 39565
diff changeset
   231
39565
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   232
f4f87c6e2fad full palette of dynamic/static value(_strict/exn)
haftmann
parents: 39564
diff changeset
   233
(** diagnostic **)
39564
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   234
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   235
fun tracing s x = (Output.tracing s; x);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   236
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   237
end;