src/HOL/Tools/code_evaluation.ML
author haftmann
Mon, 20 Sep 2010 15:10:21 +0200
changeset 39564 acfd10e38e80
child 39565 f4f87c6e2fad
permissions -rw-r--r--
Factored out ML into separate file
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
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
     9
  val dynamic_value_strict: theory -> term -> term
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    10
  val put_term: (unit -> term) -> Proof.context -> Proof.context
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    11
  val tracing: string -> 'a -> 'a
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    12
  val setup: theory -> theory
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    13
end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    14
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    15
structure Code_Evaluation : CODE_EVALUATION =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    16
struct
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    17
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    18
(** term_of instances **)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    19
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    20
(* formal definition *)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    21
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    22
fun add_term_of tyco raw_vs thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    23
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    24
    val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    25
    val ty = Type (tyco, map TFree vs);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    26
    val lhs = Const (@{const_name term_of}, ty --> @{typ term})
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    27
      $ Free ("x", ty);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    28
    val rhs = @{term "undefined :: term"};
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    29
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    30
    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
    31
      o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    32
  in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    33
    thy
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    34
    |> Class.instantiation ([tyco], vs, @{sort term_of})
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    35
    |> `(fn lthy => Syntax.check_term lthy eq)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    36
    |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    37
    |> snd
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    38
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    39
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    40
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    41
fun ensure_term_of (tyco, (raw_vs, _)) thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    42
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    43
    val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of})
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    44
      andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    45
  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
    46
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    47
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    48
(* code equations for datatypes *)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    49
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    50
fun mk_term_of_eq thy ty vs tyco (c, tys) =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    51
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    52
    val t = list_comb (Const (c, tys ---> ty),
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    53
      map Free (Name.names Name.context "a" tys));
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    54
    val (arg, rhs) =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    55
      pairself (Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    56
        (t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    57
    val cty = Thm.ctyp_of thy ty;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    58
  in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    59
    @{thm term_of_anything}
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    60
    |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    61
    |> Thm.varifyT_global
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    62
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    63
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    64
fun add_term_of_code tyco raw_vs raw_cs thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    65
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    66
    val algebra = Sign.classes_of thy;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    67
    val vs = map (fn (v, sort) =>
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    68
      (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    69
    val ty = Type (tyco, map TFree vs);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    70
    val cs = (map o apsnd o map o map_atyps)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    71
      (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    72
    val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    73
    val eqs = map (mk_term_of_eq thy ty vs tyco) cs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    74
 in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    75
    thy
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    76
    |> Code.del_eqns const
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    77
    |> fold Code.add_eqn eqs
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    78
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    79
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    80
fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    81
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    82
    val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    83
  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
    84
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    85
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    86
(* code equations for abstypes *)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    87
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    88
fun mk_abs_term_of_eq thy ty vs tyco abs ty_rep proj =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    89
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    90
    val arg = Var (("x", 0), ty);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    91
    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
    92
      (HOLogic.mk_term_of ty_rep (Const (proj, ty --> ty_rep) $ arg))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    93
      |> Thm.cterm_of thy;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    94
    val cty = Thm.ctyp_of thy ty;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    95
  in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    96
    @{thm term_of_anything}
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    97
    |> Drule.instantiate' [SOME cty] [SOME (Thm.cterm_of thy arg), SOME rhs]
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    98
    |> Thm.varifyT_global
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
    99
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   100
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   101
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
   102
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   103
    val algebra = Sign.classes_of thy;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   104
    val vs = map (fn (v, sort) =>
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   105
      (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   106
    val ty = Type (tyco, map TFree vs);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   107
    val ty_rep = map_atyps
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   108
      (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_ty_rep;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   109
    val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   110
    val eq = mk_abs_term_of_eq thy ty vs tyco abs ty_rep proj;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   111
 in
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   112
    thy
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   113
    |> Code.del_eqns const
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   114
    |> Code.add_eqn eq
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   115
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   116
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   117
fun ensure_abs_term_of_code (tyco, (raw_vs, ((abs, ty), (proj, _)))) thy =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   118
  let
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   119
    val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   120
  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
   121
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   122
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   123
(** termifying syntax **)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   124
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   125
fun map_default f xs =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   126
  let val ys = map f xs
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   127
  in if exists is_some ys
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   128
    then SOME (map2 the_default xs ys)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   129
    else NONE
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   130
  end;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   131
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   132
fun subst_termify_app (Const (@{const_name termify}, T), [t]) =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   133
      if not (Term.has_abs t)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   134
      then if fold_aterms (fn Const _ => I | _ => K false) t true
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   135
        then SOME (HOLogic.reflect_term t)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   136
        else error "Cannot termify expression containing variables"
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   137
      else error "Cannot termify expression containing abstraction"
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   138
  | subst_termify_app (t, ts) = case map_default subst_termify ts
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   139
     of SOME ts' => SOME (list_comb (t, ts'))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   140
      | NONE => NONE
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   141
and subst_termify (Abs (v, T, t)) = (case subst_termify t
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   142
     of SOME t' => SOME (Abs (v, T, t'))
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   143
      | NONE => NONE)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   144
  | subst_termify t = subst_termify_app (strip_comb t) 
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 check_termify ts ctxt = map_default subst_termify ts
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   147
  |> Option.map (rpair ctxt)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   148
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   149
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   150
(** evaluation **)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   151
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   152
structure Evaluation = Proof_Data (
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   153
  type T = unit -> term
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   154
  fun init _ () = error "Evaluation"
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   155
);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   156
val put_term = Evaluation.put;
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   157
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   158
fun tracing s x = (Output.tracing s; x);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   159
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   160
fun dynamic_value_strict thy t = Code_Runtime.dynamic_value_strict (Evaluation.get, put_term, "Code_Evaluation.put_term")
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   161
  thy NONE I (HOLogic.mk_term_of (fastype_of t) t) [];
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   162
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   163
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   164
(** setup **)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   165
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   166
val setup =
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   167
  Code.datatype_interpretation ensure_term_of
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   168
  #> Code.abstype_interpretation ensure_term_of
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   169
  #> Code.datatype_interpretation ensure_term_of_code
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   170
  #> Code.abstype_interpretation ensure_abs_term_of_code
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   171
  #> Context.theory_map (Syntax.add_term_check 0 "termify" check_termify)
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   172
  #> Value.add_evaluator ("code", dynamic_value_strict o ProofContext.theory_of);
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   173
acfd10e38e80 Factored out ML into separate file
haftmann
parents:
diff changeset
   174
end;