src/HOL/Library/Eval.thy
author wenzelm
Sat Oct 06 16:50:04 2007 +0200 (2007-10-06)
changeset 24867 e5b55d7be9bb
parent 24835 8c26128f8997
child 24916 dc56dd1b3cda
permissions -rw-r--r--
simplified interfaces for outer syntax;
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@24280
     9
imports Main Pure_term
haftmann@22525
    10
begin
haftmann@22525
    11
haftmann@22527
    12
subsection {* @{text typ_of} class *}
haftmann@22525
    13
haftmann@23062
    14
class typ_of =
haftmann@23062
    15
  fixes typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ"
haftmann@22525
    16
haftmann@22525
    17
ML {*
haftmann@22525
    18
structure TypOf =
haftmann@22525
    19
struct
haftmann@22525
    20
haftmann@22525
    21
val class_typ_of = Sign.intern_class @{theory} "typ_of";
haftmann@22525
    22
haftmann@22525
    23
fun term_typ_of_type ty =
haftmann@22525
    24
  Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ})
haftmann@22525
    25
    $ Logic.mk_type ty;
haftmann@22525
    26
haftmann@22525
    27
fun mk_typ_of_def ty =
haftmann@22525
    28
  let
haftmann@22525
    29
    val lhs = Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ})
haftmann@22525
    30
      $ Free ("x", Term.itselfT ty)
haftmann@22525
    31
    val rhs = Pure_term.mk_typ (fn v => term_typ_of_type (TFree v)) ty
haftmann@22525
    32
  in Logic.mk_equals (lhs, rhs) end;
haftmann@22525
    33
haftmann@22525
    34
end;
haftmann@22525
    35
*}
haftmann@22525
    36
haftmann@24587
    37
instance "prop" :: typ_of
haftmann@24587
    38
  "typ_of T \<equiv> STR ''prop'' {\<struct>} []" ..
haftmann@24587
    39
haftmann@23062
    40
instance itself :: (typ_of) typ_of
haftmann@23062
    41
  "typ_of T \<equiv> STR ''itself'' {\<struct>} [typ_of TYPE('a\<Colon>typ_of)]" ..
haftmann@23062
    42
haftmann@24587
    43
instance set :: (typ_of) typ_of
haftmann@24587
    44
  "typ_of T \<equiv> STR ''set'' {\<struct>} [typ_of TYPE('a\<Colon>typ_of)]" ..
haftmann@23062
    45
haftmann@23020
    46
instance int :: typ_of
haftmann@23020
    47
  "typ_of T \<equiv> STR ''IntDef.int'' {\<struct>} []" ..
haftmann@23020
    48
haftmann@22525
    49
setup {*
haftmann@22525
    50
let
haftmann@22525
    51
  fun mk arities _ thy =
haftmann@22525
    52
    (maps (fn (tyco, asorts, _) => [(("", []), TypOf.mk_typ_of_def
haftmann@22525
    53
      (Type (tyco,
haftmann@22525
    54
        map TFree (Name.names Name.context "'a" asorts))))]) arities, thy);
haftmann@22525
    55
  fun hook specs =
haftmann@24219
    56
    DatatypeCodegen.prove_codetypes_arities (Class.intro_classes_tac [])
haftmann@22525
    57
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
haftmann@24621
    58
      [TypOf.class_typ_of] mk ((K o K) (fold Code.add_default_func))
haftmann@24626
    59
in DatatypeCodegen.add_codetypes_hook hook end
haftmann@22525
    60
*}
haftmann@22525
    61
haftmann@22525
    62
haftmann@22527
    63
subsection {* @{text term_of} class *}
haftmann@22525
    64
haftmann@22525
    65
class term_of = typ_of +
haftmann@23062
    66
  constrains typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ"
haftmann@22525
    67
  fixes term_of :: "'a \<Rightarrow> term"
haftmann@22525
    68
haftmann@22525
    69
ML {*
haftmann@22525
    70
structure TermOf =
haftmann@22525
    71
struct
haftmann@22525
    72
haftmann@22525
    73
local
haftmann@22525
    74
  fun term_term_of ty =
haftmann@22525
    75
    Const (@{const_name term_of}, ty --> @{typ term});
haftmann@22525
    76
in
haftmann@22525
    77
  val class_term_of = Sign.intern_class @{theory} "term_of";
haftmann@22525
    78
  fun mk_terms_of_defs vs (tyco, cs) =
haftmann@22525
    79
    let
haftmann@22525
    80
      val dty = Type (tyco, map TFree vs);
haftmann@22525
    81
      fun mk_eq c =
haftmann@22525
    82
        let
haftmann@22525
    83
          val lhs : term = term_term_of dty $ c;
haftmann@22525
    84
          val rhs : term = Pure_term.mk_term
haftmann@22525
    85
            (fn (v, ty) => term_term_of ty $ Free (v, ty))
haftmann@22525
    86
            (Pure_term.mk_typ (fn (v, sort) => TypOf.term_typ_of_type (TFree (v, sort)))) c
haftmann@22525
    87
        in
haftmann@22525
    88
          HOLogic.mk_eq (lhs, rhs)
haftmann@22525
    89
        end;
haftmann@22525
    90
    in map mk_eq cs end;
haftmann@22525
    91
  fun mk_term_of t =
haftmann@22525
    92
    term_term_of (Term.fastype_of t) $ t;
haftmann@22525
    93
end;
haftmann@22525
    94
haftmann@22525
    95
end;
haftmann@22525
    96
*}
haftmann@22525
    97
haftmann@22525
    98
setup {*
haftmann@22525
    99
let
haftmann@22525
   100
  fun thy_note ((name, atts), thms) =
haftmann@22525
   101
    PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
haftmann@22525
   102
  fun thy_def ((name, atts), t) =
haftmann@22525
   103
    PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
haftmann@24423
   104
  fun mk arities css _ thy =
haftmann@22525
   105
    let
haftmann@22525
   106
      val (_, asorts, _) :: _ = arities;
haftmann@22525
   107
      val vs = Name.names Name.context "'a" asorts;
haftmann@22525
   108
      val defs = map (TermOf.mk_terms_of_defs vs) css;
haftmann@22525
   109
      val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs;
haftmann@22525
   110
    in
haftmann@22525
   111
      thy
haftmann@22525
   112
      |> PrimrecPackage.gen_primrec thy_note thy_def "" defs'
haftmann@22525
   113
      |> snd
haftmann@22525
   114
    end;
haftmann@22525
   115
  fun hook specs =
haftmann@24659
   116
    if null specs orelse (fst o hd) specs = (fst o dest_Type) @{typ typ} then I
haftmann@22525
   117
    else
haftmann@24219
   118
      DatatypeCodegen.prove_codetypes_arities (Class.intro_classes_tac [])
haftmann@22525
   119
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
haftmann@22525
   120
      [TermOf.class_term_of] ((K o K o pair) []) mk
haftmann@24626
   121
in DatatypeCodegen.add_codetypes_hook hook end
haftmann@22525
   122
*}
haftmann@22525
   123
haftmann@23062
   124
abbreviation
haftmann@23062
   125
  intT :: "typ"
haftmann@23062
   126
where
haftmann@23062
   127
  "intT \<equiv> STR ''IntDef.int'' {\<struct>} []"
haftmann@23062
   128
haftmann@23133
   129
abbreviation
haftmann@23133
   130
  bitT :: "typ"
haftmann@23133
   131
where
haftmann@23133
   132
  "bitT \<equiv> STR ''Numeral.bit'' {\<struct>} []"
haftmann@23133
   133
haftmann@23062
   134
function
haftmann@23062
   135
  mk_int :: "int \<Rightarrow> term"
haftmann@23062
   136
where
haftmann@23062
   137
  "mk_int k = (if k = 0 then STR ''Numeral.Pls'' \<Colon>\<subseteq> intT
haftmann@23062
   138
    else if k = -1 then STR ''Numeral.Min'' \<Colon>\<subseteq> intT
haftmann@23062
   139
    else let (l, m) = divAlg (k, 2)
haftmann@23133
   140
  in STR ''Numeral.Bit'' \<Colon>\<subseteq> intT \<rightarrow> bitT \<rightarrow> intT \<bullet> mk_int l \<bullet>
haftmann@23133
   141
    (if m = 0 then STR ''Numeral.bit.B0'' \<Colon>\<subseteq> bitT else STR ''Numeral.bit.B1'' \<Colon>\<subseteq> bitT))"
haftmann@23062
   142
by pat_completeness auto
haftmann@23062
   143
termination by (relation "measure (nat o abs)") (auto simp add: divAlg_mod_div)
haftmann@23062
   144
haftmann@23062
   145
instance int :: term_of
haftmann@23133
   146
  "term_of k \<equiv> STR ''Numeral.number_class.number_of'' \<Colon>\<subseteq> intT \<rightarrow> intT \<bullet> mk_int k" ..
haftmann@23062
   147
haftmann@23062
   148
haftmann@22804
   149
text {* Adaption for @{typ ml_string}s *}
haftmann@22525
   150
haftmann@22845
   151
lemmas [code func, code func del] = term_of_ml_string_def
haftmann@22525
   152
haftmann@22525
   153
haftmann@22525
   154
subsection {* Evaluation infrastructure *}
haftmann@22525
   155
haftmann@22525
   156
ML {*
haftmann@22525
   157
signature EVAL =
haftmann@22525
   158
sig
haftmann@24587
   159
  val eval_ref: (unit -> term) option ref
haftmann@24835
   160
  val eval_term: theory -> term -> term
haftmann@24835
   161
  val evaluate: Proof.context -> term -> unit
haftmann@24835
   162
  val evaluate': string -> Proof.context -> term -> unit
haftmann@24835
   163
  val evaluate_cmd: string option -> Toplevel.state -> unit
haftmann@22525
   164
end;
haftmann@22525
   165
haftmann@24280
   166
structure Eval =
haftmann@22525
   167
struct
haftmann@22525
   168
haftmann@24587
   169
val eval_ref = ref (NONE : (unit -> term) option);
haftmann@22525
   170
haftmann@24835
   171
fun eval_invoke thy code ((_, ty), t) deps _ =
haftmann@24835
   172
  CodePackage.eval_invoke thy ("Eval.eval_ref", eval_ref) code (t, ty) [];
haftmann@24280
   173
haftmann@24835
   174
fun eval_term thy =
haftmann@24835
   175
  TermOf.mk_term_of
haftmann@24835
   176
  #> CodePackage.eval_term thy (eval_invoke thy)
haftmann@24835
   177
  #> Code.postprocess_term thy;
haftmann@24280
   178
haftmann@24835
   179
val evaluators = [
haftmann@24835
   180
  ("code", eval_term),
haftmann@24835
   181
  ("SML", Codegen.eval_term),
haftmann@24835
   182
  ("normal_form", Nbe.norm_term)
haftmann@24835
   183
];
haftmann@22525
   184
haftmann@24835
   185
fun gen_evaluate evaluators ctxt t =
haftmann@24280
   186
  let
haftmann@24280
   187
    val thy = ProofContext.theory_of ctxt;
haftmann@24835
   188
    val (evls, evl) = split_last evaluators;
haftmann@24835
   189
    val t' = case get_first (fn f => try (f thy) t) evls
haftmann@24835
   190
     of SOME t' => t'
haftmann@24835
   191
      | NONE => evl thy t;
haftmann@24835
   192
    val ty' = Term.type_of t';
haftmann@24835
   193
    val p = Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'),
haftmann@24835
   194
      Pretty.fbrk, Pretty.str "::", Pretty.brk 1,
haftmann@24835
   195
      Pretty.quote (ProofContext.pretty_typ ctxt ty')];
haftmann@24280
   196
  in Pretty.writeln p end;
haftmann@24280
   197
haftmann@24835
   198
val evaluate = gen_evaluate (map snd evaluators);
haftmann@24835
   199
haftmann@24835
   200
fun evaluate' name = gen_evaluate
haftmann@24835
   201
  [(the o AList.lookup (op =) evaluators) name];
haftmann@24835
   202
haftmann@24835
   203
fun evaluate_cmd some_name raw_t state =
haftmann@22525
   204
  let
haftmann@22804
   205
    val ctxt = Toplevel.context_of state;
wenzelm@24508
   206
    val t = Syntax.read_term ctxt raw_t;
haftmann@24835
   207
  in case some_name
haftmann@24835
   208
   of NONE => evaluate ctxt t
haftmann@24835
   209
    | SOME name => evaluate' name ctxt t
haftmann@24835
   210
  end;
haftmann@22525
   211
haftmann@22525
   212
end;
haftmann@22525
   213
*}
haftmann@22525
   214
haftmann@22804
   215
ML {*
haftmann@22804
   216
  OuterSyntax.improper_command "value" "read, evaluate and print term" OuterKeyword.diag
haftmann@24835
   217
    (Scan.option (OuterParse.$$$ "(" |-- OuterParse.name --| OuterParse.$$$ ")")
haftmann@24835
   218
    -- OuterParse.term
haftmann@24835
   219
      >> (fn (some_name, t) => Toplevel.no_timing o Toplevel.keep
haftmann@24835
   220
           (Eval.evaluate_cmd some_name t)));
haftmann@22804
   221
*}
haftmann@22804
   222
wenzelm@22665
   223
end