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