src/HOL/ex/CodeEval.thy
author haftmann
Fri, 03 Nov 2006 14:22:40 +0100
changeset 21154 c8cc6b68759f
parent 21117 e8657a20a52f
child 21386 a80a35d67cf1
permissions -rw-r--r--
improved evaluation setup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     1
(*  ID:         $Id$
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     3
*)
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     4
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     5
header {* A simple embedded term evaluation mechanism *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     6
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     7
theory CodeEval
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     8
imports CodeEmbed
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
     9
begin
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    10
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    11
section {* A simple embedded term evaluation mechanism *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    12
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    13
subsection {* The typ_of class *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    14
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    15
class typ_of =
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    16
  fixes typ_of :: "'a itself \<Rightarrow> typ"
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    17
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    18
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    19
structure TypOf =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    20
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    21
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    22
local
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    23
  val thy = the_context ();
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    24
  val const_typ_of = Sign.intern_const thy "typ_of";
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    25
  fun term_typ_of ty = Const (const_typ_of, Term.itselfT ty --> Embed.typ_typ);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    26
in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    27
  val class_typ_of = Sign.intern_class thy "typ_of";
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    28
  fun term_typ_of_type ty =
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    29
    term_typ_of ty $ Logic.mk_type ty;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    30
  fun mk_typ_of_def ty =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    31
    let
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    32
      val lhs = term_typ_of ty $ Free ("x", Term.itselfT ty)
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    33
      val rhs = Embed.term_typ (fn v => term_typ_of_type (TFree v)) ty
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    34
    in Logic.mk_equals (lhs, rhs) end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    35
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    36
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    37
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    38
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    39
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    40
setup {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    41
let
20835
haftmann
parents: 20655
diff changeset
    42
  fun mk arities _ thy =
haftmann
parents: 20655
diff changeset
    43
    (maps (fn ((tyco, asorts), _) => [(("", []), TypOf.mk_typ_of_def
haftmann
parents: 20655
diff changeset
    44
      (Type (tyco, map TFree (Name.names Name.context "'a" asorts))))]) arities, thy);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    45
  fun tac _ = ClassPackage.intro_classes_tac [];
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    46
  fun hook specs =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    47
    DatatypeCodegen.prove_codetypes_arities tac
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    48
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
20835
haftmann
parents: 20655
diff changeset
    49
      [TypOf.class_typ_of] mk ((K o K) I)
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    50
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    51
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    52
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    53
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    54
subsection {* term_of class *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    55
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    56
class term_of = typ_of +
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    57
  constrains typ_of :: "'a itself \<Rightarrow> typ"
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    58
  fixes term_of :: "'a \<Rightarrow> term"
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    59
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    60
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    61
structure TermOf =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    62
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    63
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    64
local
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    65
  val thy = the_context ();
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    66
  val const_term_of = Sign.intern_const thy "term_of";
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    67
  fun term_term_of ty = Const (const_term_of, ty --> Embed.typ_term);
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    68
in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    69
  val class_term_of = Sign.intern_class thy "term_of";
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    70
  fun mk_terms_of_defs vs (tyco, cs) =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    71
    let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    72
      val dty = Type (tyco, map TFree vs);
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    73
      fun mk_eq c =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    74
        let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    75
          val lhs : term = term_term_of dty $ c;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    76
          val rhs : term = Embed.term_term
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    77
            (fn (v, ty) => term_term_of ty $ Free (v, ty))
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    78
            (Embed.term_typ (fn (v, sort) => TypOf.term_typ_of_type (TFree (v, sort)))) c
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    79
        in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    80
          HOLogic.mk_eq (lhs, rhs)
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    81
        end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    82
    in map mk_eq cs end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    83
  fun mk_term_of t =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    84
    term_term_of (Term.fastype_of t) $ t;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    85
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    86
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    87
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    88
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    89
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    90
setup {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    91
let
20835
haftmann
parents: 20655
diff changeset
    92
  fun thy_note ((name, atts), thms) =
haftmann
parents: 20655
diff changeset
    93
    PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
haftmann
parents: 20655
diff changeset
    94
  fun thy_def ((name, atts), t) =
haftmann
parents: 20655
diff changeset
    95
    PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
haftmann
parents: 20655
diff changeset
    96
  fun mk arities css thy =
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    97
    let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    98
      val vs = (Name.names Name.context "'a" o snd o fst o hd) arities;
20835
haftmann
parents: 20655
diff changeset
    99
      val defs = map (TermOf.mk_terms_of_defs vs) css;
haftmann
parents: 20655
diff changeset
   100
      val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs;
haftmann
parents: 20655
diff changeset
   101
    in
haftmann
parents: 20655
diff changeset
   102
      thy
haftmann
parents: 20655
diff changeset
   103
      |> PrimrecPackage.gen_primrec thy_note thy_def "" defs'
haftmann
parents: 20655
diff changeset
   104
      |> snd
haftmann
parents: 20655
diff changeset
   105
    end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   106
  fun tac _ = ClassPackage.intro_classes_tac [];
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   107
  fun hook specs =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   108
    if (fst o hd) specs = (fst o dest_Type) Embed.typ_typ then I
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   109
    else
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   110
      DatatypeCodegen.prove_codetypes_arities tac
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   111
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
20835
haftmann
parents: 20655
diff changeset
   112
      [TermOf.class_term_of] ((K o K o pair) []) mk
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   113
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   114
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   115
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   116
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   117
subsection {* Evaluation infrastructure *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   118
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   119
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   120
signature EVAL =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   121
sig
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   122
  val eval_term: theory -> term -> term
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   123
  val term: string -> unit
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   124
  val eval_ref: term option ref
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   125
  val oracle: string * (theory * exn -> term)
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   126
  val method: Method.src -> Proof.context -> Method.method
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   127
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   128
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   129
structure Eval : EVAL =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   130
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   131
20655
8c4d80e8025f circumvented defect in SML/NJ type inference
haftmann
parents: 20597
diff changeset
   132
val eval_ref = ref (NONE : term option);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   133
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   134
fun eval_term thy t =
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   135
  CodegenPackage.eval_term
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   136
    thy (("Eval.eval_ref", eval_ref), TermOf.mk_term_of t);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   137
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   138
fun term t =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   139
  let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   140
    val thy = the_context ();
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   141
    val t = eval_term thy (Sign.read_term thy t);
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   142
  in (writeln o Sign.string_of_term thy) t end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   143
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   144
exception Eval of term;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   145
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   146
val oracle = ("Eval", fn (thy, Eval t) =>
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   147
  Logic.mk_equals (t, eval_term thy t));
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   148
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   149
val oracle_name = NameSpace.pack [Context.theory_name (the_context ()), fst oracle];
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   150
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   151
fun conv ct =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   152
  let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   153
    val {thy, t, ...} = rep_cterm ct;
21154
c8cc6b68759f improved evaluation setup
haftmann
parents: 21117
diff changeset
   154
  in Thm.invoke_oracle_i thy oracle_name (thy, Eval t) end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   155
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   156
fun tac i = Tactical.PRIMITIVE (Drule.fconv_rule
21154
c8cc6b68759f improved evaluation setup
haftmann
parents: 21117
diff changeset
   157
  (Drule.goals_conv (equal i) (HOL.Trueprop_conv conv)));
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   158
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   159
val method =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   160
  Method.no_args (Method.METHOD (fn _ =>
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   161
    tac 1 THEN rtac TrueI 1));
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   162
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   163
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   164
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   165
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   166
setup {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   167
  Theory.add_oracle Eval.oracle
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   168
  #> Method.add_method ("eval", Eval.method, "solve goal by evaluation")
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   169
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   170
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   171
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   172
subsection {* Small examples *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   173
21117
e8657a20a52f *** empty log message ***
haftmann
parents: 20835
diff changeset
   174
ML {* Eval.term "(Suc 2 + Suc 0) * Suc 3" *}
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   175
ML {* Eval.term "[]::nat list" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   176
ML {* Eval.term "fst ([]::nat list, Suc 0) = []" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   177
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   178
text {* a fancy datatype *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   179
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   180
datatype ('a, 'b) bair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   181
    Bair "'a\<Colon>order" 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   182
  | Shift "('a, 'b) cair"
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   183
  | Dummy unit
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   184
and ('a, 'b) cair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   185
    Cair 'a 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   186
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   187
ML {* Eval.term "Shift (Cair (4::nat) [Suc 0])" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   188
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   189
lemma
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   190
  "Suc 0 = 1" by eval
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   191
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   192
lemma
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   193
  "rev [0, Suc 0, Suc 0] = [Suc 0, Suc 0, 0]" by eval
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   194
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   195
lemma
20435
haftmann
parents: 20427
diff changeset
   196
  "fst (snd (fst ( ((Some (2::nat), (Suc 0, ())), [0::nat]) ))) = Suc (Suc 0) - 1" by eval
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   197
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   198
end