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