src/HOL/Library/Eval.thy
author haftmann
Mon Jul 07 08:47:17 2008 +0200 (2008-07-07)
changeset 27487 c8a6ce181805
parent 27368 9f90ac19e32b
child 27681 8cedebf55539
permissions -rw-r--r--
absolute imports of HOL/*.thy theories
haftmann@22525
     1
(*  Title:      HOL/Library/Eval.thy
haftmann@22525
     2
    ID:         $Id$
haftmann@22525
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@22525
     4
*)
haftmann@22525
     5
haftmann@22525
     6
header {* A simple term evaluation mechanism *}
haftmann@22525
     7
haftmann@22525
     8
theory Eval
haftmann@26037
     9
imports
haftmann@27368
    10
  Plain
haftmann@26168
    11
  RType
haftmann@26037
    12
  Code_Index (* this theory is just imported for a term_of setup *)
haftmann@22525
    13
begin
haftmann@22525
    14
haftmann@25763
    15
subsection {* Term representation *}
haftmann@25763
    16
haftmann@26587
    17
subsubsection {* Terms and class @{text term_of} *}
haftmann@26020
    18
haftmann@26020
    19
datatype "term" = dummy_term
haftmann@25763
    20
haftmann@26020
    21
definition
haftmann@26168
    22
  Const :: "message_string \<Rightarrow> rtype \<Rightarrow> term"
haftmann@26020
    23
where
haftmann@26020
    24
  "Const _ _ = dummy_term"
haftmann@25763
    25
haftmann@26020
    26
definition
haftmann@26020
    27
  App :: "term \<Rightarrow> term \<Rightarrow> term"
haftmann@26020
    28
where
haftmann@26020
    29
  "App _ _ = dummy_term"
haftmann@26020
    30
haftmann@26020
    31
code_datatype Const App
haftmann@25763
    32
haftmann@26168
    33
class term_of = rtype +
haftmann@26020
    34
  fixes term_of :: "'a \<Rightarrow> term"
haftmann@26020
    35
haftmann@26020
    36
lemma term_of_anything: "term_of x \<equiv> t"
haftmann@26020
    37
  by (rule eq_reflection) (cases "term_of x", cases t, simp)
haftmann@25763
    38
haftmann@25763
    39
ML {*
haftmann@26020
    40
structure Eval =
haftmann@25763
    41
struct
haftmann@25763
    42
haftmann@25763
    43
fun mk_term f g (Const (c, ty)) =
haftmann@25763
    44
      @{term Const} $ Message_String.mk c $ g ty
haftmann@25763
    45
  | mk_term f g (t1 $ t2) =
haftmann@25763
    46
      @{term App} $ mk_term f g t1 $ mk_term f g t2
haftmann@26267
    47
  | mk_term f g (Free v) = f v
haftmann@26587
    48
  | mk_term f g (Bound i) = Bound i
haftmann@26587
    49
  | mk_term f g (Abs (v, _, t)) = Abs (v, @{typ term}, mk_term f g t);
haftmann@25763
    50
haftmann@26020
    51
fun mk_term_of ty t = Const (@{const_name term_of}, ty --> @{typ term}) $ t;
haftmann@22525
    52
haftmann@22525
    53
end;
haftmann@22525
    54
*}
haftmann@22525
    55
haftmann@26587
    56
haftmann@26587
    57
subsubsection {* @{text term_of} instances *}
haftmann@26587
    58
haftmann@22525
    59
setup {*
haftmann@22525
    60
let
haftmann@26020
    61
  fun add_term_of_def ty vs tyco thy =
haftmann@22525
    62
    let
haftmann@26020
    63
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
haftmann@26020
    64
        $ Free ("x", ty);
haftmann@26020
    65
      val rhs = @{term "undefined \<Colon> term"};
haftmann@26020
    66
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@26020
    67
    in
haftmann@25536
    68
      thy
haftmann@26020
    69
      |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
haftmann@26020
    70
      |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@26020
    71
      |-> (fn eq => Specification.definition (NONE, (("", []), eq)))
haftmann@25569
    72
      |> snd
haftmann@25569
    73
      |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
haftmann@25569
    74
      |> LocalTheory.exit
haftmann@25569
    75
      |> ProofContext.theory_of
haftmann@26020
    76
    end;
haftmann@26020
    77
  fun interpretator (tyco, (raw_vs, _)) thy =
haftmann@26020
    78
    let
haftmann@26168
    79
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
haftmann@26020
    80
      val constrain_sort =
haftmann@26020
    81
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
haftmann@26020
    82
      val vs = (map o apsnd) constrain_sort raw_vs;
haftmann@26020
    83
      val ty = Type (tyco, map TFree vs);
haftmann@26020
    84
    in
haftmann@26020
    85
      thy
haftmann@26168
    86
      |> RType.perhaps_add_def tyco
haftmann@26168
    87
      |> not has_inst ? add_term_of_def ty vs tyco
haftmann@26020
    88
    end;
haftmann@26020
    89
in
haftmann@26168
    90
  Code.type_interpretation interpretator
haftmann@26020
    91
end
haftmann@22525
    92
*}
haftmann@22525
    93
haftmann@26020
    94
setup {*
haftmann@26020
    95
let
haftmann@26020
    96
  fun mk_term_of_eq ty vs tyco (c, tys) =
haftmann@26020
    97
    let
haftmann@26020
    98
      val t = list_comb (Const (c, tys ---> ty),
haftmann@26020
    99
        map Free (Name.names Name.context "a" tys));
haftmann@26020
   100
    in (map_aterms (fn Free (v, ty) => Var ((v, 0), ty) | t => t) t, Eval.mk_term
haftmann@26020
   101
      (fn (v, ty) => Eval.mk_term_of ty (Var ((v, 0), ty)))
haftmann@26168
   102
      (RType.mk (fn (v, sort) => RType.rtype (TFree (v, sort)))) t)
haftmann@26020
   103
    end;
haftmann@26020
   104
  fun prove_term_of_eq ty eq thy =
haftmann@26020
   105
    let
haftmann@26020
   106
      val cty = Thm.ctyp_of thy ty;
haftmann@26020
   107
      val (arg, rhs) = pairself (Thm.cterm_of thy) eq;
haftmann@26020
   108
      val thm = @{thm term_of_anything}
haftmann@26020
   109
        |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
haftmann@26020
   110
        |> Thm.varifyT;
haftmann@26020
   111
    in
haftmann@26020
   112
      thy
haftmann@26020
   113
      |> Code.add_func thm
haftmann@26020
   114
    end;
haftmann@26020
   115
  fun interpretator (tyco, (raw_vs, raw_cs)) thy =
haftmann@26020
   116
    let
haftmann@26020
   117
      val constrain_sort =
haftmann@26020
   118
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
haftmann@26020
   119
      val vs = (map o apsnd) constrain_sort raw_vs;
haftmann@26020
   120
      val cs = (map o apsnd o map o map_atyps)
haftmann@26020
   121
        (fn TFree (v, sort) => TFree (v, constrain_sort sort)) raw_cs;
haftmann@26020
   122
      val ty = Type (tyco, map TFree vs);
haftmann@26020
   123
      val eqs = map (mk_term_of_eq ty vs tyco) cs;
haftmann@26020
   124
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
haftmann@26020
   125
    in
haftmann@26020
   126
      thy
haftmann@26020
   127
      |> Code.del_funcs const
haftmann@26020
   128
      |> fold (prove_term_of_eq ty) eqs
haftmann@26020
   129
    end;
haftmann@26020
   130
in
haftmann@26020
   131
  Code.type_interpretation interpretator
haftmann@25569
   132
end
haftmann@26020
   133
*}
haftmann@23062
   134
haftmann@26587
   135
haftmann@25763
   136
subsubsection {* Code generator setup *}
haftmann@25763
   137
haftmann@25763
   138
lemmas [code func del] = term.recs term.cases term.size
haftmann@25763
   139
lemma [code func, code func del]: "(t1\<Colon>term) = t2 \<longleftrightarrow> t1 = t2" ..
haftmann@26037
   140
haftmann@26168
   141
lemma [code func, code func del]: "(term_of \<Colon> rtype \<Rightarrow> term) = term_of" ..
haftmann@26037
   142
lemma [code func, code func del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
haftmann@26037
   143
lemma [code func, code func del]: "(term_of \<Colon> index \<Rightarrow> term) = term_of" ..
haftmann@26020
   144
lemma [code func, code func del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" ..
haftmann@22525
   145
haftmann@25763
   146
code_type "term"
haftmann@25763
   147
  (SML "Term.term")
haftmann@25763
   148
haftmann@26020
   149
code_const Const and App
haftmann@26020
   150
  (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)")
haftmann@22525
   151
haftmann@26037
   152
code_const "term_of \<Colon> index \<Rightarrow> term"
haftmann@26037
   153
  (SML "HOLogic.mk'_number/ HOLogic.indexT")
haftmann@26037
   154
haftmann@26037
   155
code_const "term_of \<Colon> message_string \<Rightarrow> term"
haftmann@26037
   156
  (SML "Message'_String.mk")
haftmann@26037
   157
haftmann@26037
   158
haftmann@26587
   159
subsubsection {* Syntax *}
haftmann@26587
   160
haftmann@26587
   161
print_translation {*
haftmann@26587
   162
let
haftmann@26587
   163
  val term = Const ("<TERM>", dummyT);
haftmann@26587
   164
  fun tr1' [_, _] = term;
haftmann@26587
   165
  fun tr2' [] = term;
haftmann@26587
   166
in
haftmann@26587
   167
  [(@{const_syntax Const}, tr1'),
haftmann@26587
   168
    (@{const_syntax App}, tr1'),
haftmann@26587
   169
    (@{const_syntax dummy_term}, tr2')]
haftmann@26587
   170
end
haftmann@26587
   171
*}
haftmann@26587
   172
setup {*
haftmann@26587
   173
  Sign.declare_const [] ("rterm_of", @{typ "'a \<Rightarrow> 'b"}, NoSyn)
haftmann@26587
   174
  #> snd
haftmann@26587
   175
*}
haftmann@26587
   176
haftmann@26587
   177
notation (output)
haftmann@26587
   178
  rterm_of ("\<guillemotleft>_\<guillemotright>")
haftmann@26587
   179
haftmann@26587
   180
locale (open) rterm_syntax =
haftmann@26587
   181
  fixes rterm_of_syntax :: "'a \<Rightarrow> 'b" ("\<guillemotleft>_\<guillemotright>")
haftmann@26587
   182
haftmann@26587
   183
parse_translation {*
haftmann@26587
   184
let
haftmann@26587
   185
  fun rterm_of_tr [t] = Lexicon.const @{const_name rterm_of} $ t
haftmann@26587
   186
    | rterm_of_tr ts = raise TERM ("rterm_of_tr", ts);
haftmann@26587
   187
in
wenzelm@26591
   188
  [(Syntax.fixedN ^ "rterm_of_syntax", rterm_of_tr)]
haftmann@26587
   189
end
haftmann@26587
   190
*}
haftmann@26587
   191
haftmann@26587
   192
setup {*
haftmann@26587
   193
let
haftmann@26587
   194
  val subst_rterm_of = Eval.mk_term
haftmann@26587
   195
    (fn (v, _) => error ("illegal free variable in term quotation: " ^ quote v))
haftmann@26587
   196
    (RType.mk (fn (v, sort) => RType.rtype (TFree (v, sort))));
haftmann@26587
   197
  fun subst_rterm_of' (Const (@{const_name rterm_of}, _), [t]) = subst_rterm_of t
haftmann@26587
   198
    | subst_rterm_of' (Const (@{const_name rterm_of}, _), _) =
haftmann@26587
   199
        error ("illegal number of arguments for " ^ quote @{const_name rterm_of})
haftmann@26587
   200
    | subst_rterm_of' (t, ts) = list_comb (t, map (subst_rterm_of' o strip_comb) ts);
haftmann@26587
   201
  fun subst_rterm_of'' t = 
haftmann@26587
   202
    let
haftmann@26587
   203
      val t' = subst_rterm_of' (strip_comb t);
haftmann@26587
   204
    in if t aconv t'
haftmann@26587
   205
      then NONE
haftmann@26587
   206
      else SOME t'
haftmann@26587
   207
    end;
haftmann@26587
   208
  fun check_rterm_of ts ctxt =
haftmann@26587
   209
    let
haftmann@26587
   210
      val ts' = map subst_rterm_of'' ts;
haftmann@26587
   211
    in if exists is_some ts'
haftmann@26587
   212
      then SOME (map2 the_default ts ts', ctxt)
haftmann@26587
   213
      else NONE
haftmann@26587
   214
    end;
haftmann@26587
   215
in
haftmann@26587
   216
  Context.theory_map (Syntax.add_term_check 0 "rterm_of" check_rterm_of)
haftmann@26587
   217
end;
haftmann@26587
   218
*}
haftmann@26587
   219
haftmann@26587
   220
hide const dummy_term
haftmann@26587
   221
hide (open) const Const App
haftmann@26587
   222
hide (open) const term_of
haftmann@26587
   223
haftmann@26587
   224
haftmann@25763
   225
subsection {* Evaluation setup *}
haftmann@22525
   226
haftmann@22525
   227
ML {*
haftmann@22525
   228
signature EVAL =
haftmann@22525
   229
sig
haftmann@26267
   230
  val mk_term: ((string * typ) -> term) -> (typ -> term) -> term -> term
haftmann@24587
   231
  val eval_ref: (unit -> term) option ref
haftmann@24835
   232
  val eval_term: theory -> term -> term
haftmann@24835
   233
  val evaluate: Proof.context -> term -> unit
haftmann@24835
   234
  val evaluate': string -> Proof.context -> term -> unit
haftmann@26020
   235
  val evaluate_cmd: string option -> string -> Toplevel.state -> unit
haftmann@22525
   236
end;
haftmann@22525
   237
haftmann@26020
   238
structure Eval : EVAL =
haftmann@22525
   239
struct
haftmann@22525
   240
haftmann@26020
   241
open Eval;
haftmann@26020
   242
haftmann@24587
   243
val eval_ref = ref (NONE : (unit -> term) option);
haftmann@22525
   244
haftmann@26020
   245
fun eval_term thy t =
haftmann@26020
   246
  t 
haftmann@26020
   247
  |> Eval.mk_term_of (fastype_of t)
haftmann@27103
   248
  |> (fn t => CodeTarget.eval_term ("Eval.eval_ref", eval_ref) thy t [])
haftmann@26020
   249
  |> Code.postprocess_term thy;
haftmann@24280
   250
haftmann@24835
   251
val evaluators = [
haftmann@24835
   252
  ("code", eval_term),
haftmann@24835
   253
  ("SML", Codegen.eval_term),
haftmann@24835
   254
  ("normal_form", Nbe.norm_term)
haftmann@24835
   255
];
haftmann@22525
   256
haftmann@24835
   257
fun gen_evaluate evaluators ctxt t =
haftmann@24280
   258
  let
haftmann@24280
   259
    val thy = ProofContext.theory_of ctxt;
haftmann@24835
   260
    val (evls, evl) = split_last evaluators;
haftmann@24835
   261
    val t' = case get_first (fn f => try (f thy) t) evls
haftmann@24835
   262
     of SOME t' => t'
haftmann@24835
   263
      | NONE => evl thy t;
haftmann@24835
   264
    val ty' = Term.type_of t';
wenzelm@24920
   265
    val p = Pretty.block [Pretty.quote (Syntax.pretty_term ctxt t'),
haftmann@24835
   266
      Pretty.fbrk, Pretty.str "::", Pretty.brk 1,
wenzelm@24920
   267
      Pretty.quote (Syntax.pretty_typ ctxt ty')];
haftmann@24280
   268
  in Pretty.writeln p end;
haftmann@24280
   269
haftmann@24835
   270
val evaluate = gen_evaluate (map snd evaluators);
haftmann@24835
   271
haftmann@24835
   272
fun evaluate' name = gen_evaluate
haftmann@24835
   273
  [(the o AList.lookup (op =) evaluators) name];
haftmann@24835
   274
haftmann@24835
   275
fun evaluate_cmd some_name raw_t state =
haftmann@22525
   276
  let
haftmann@22804
   277
    val ctxt = Toplevel.context_of state;
wenzelm@24508
   278
    val t = Syntax.read_term ctxt raw_t;
haftmann@24835
   279
  in case some_name
haftmann@24835
   280
   of NONE => evaluate ctxt t
haftmann@24835
   281
    | SOME name => evaluate' name ctxt t
haftmann@24835
   282
  end;
haftmann@22525
   283
haftmann@22525
   284
end;
haftmann@22525
   285
*}
haftmann@22525
   286
haftmann@22804
   287
ML {*
haftmann@22804
   288
  OuterSyntax.improper_command "value" "read, evaluate and print term" OuterKeyword.diag
haftmann@24835
   289
    (Scan.option (OuterParse.$$$ "(" |-- OuterParse.name --| OuterParse.$$$ ")")
haftmann@24835
   290
    -- OuterParse.term
haftmann@24835
   291
      >> (fn (some_name, t) => Toplevel.no_timing o Toplevel.keep
haftmann@24835
   292
           (Eval.evaluate_cmd some_name t)));
haftmann@22804
   293
*}
haftmann@22804
   294
haftmann@26032
   295
end