src/HOL/ex/CodeEval.thy
author haftmann
Tue, 19 Sep 2006 15:22:05 +0200
changeset 20597 65fe827aa595
parent 20453 855f07fabd76
child 20655 8c4d80e8025f
permissions -rw-r--r--
code generation 2 adjustments
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
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    42
  fun mk thy arities _ =
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    43
    maps (fn ((tyco, asorts), _) => [(("", []), TypOf.mk_typ_of_def
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    44
      (Type (tyco, map TFree (Name.names Name.context "'a" asorts)))
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    45
      |> tap (writeln o Sign.string_of_term thy))]) arities;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    46
  fun tac _ = ClassPackage.intro_classes_tac [];
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    47
  fun hook specs =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    48
    DatatypeCodegen.prove_codetypes_arities tac
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    49
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    50
      [TypOf.class_typ_of] mk I
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    51
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
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
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    55
subsection {* term_of class *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    56
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    57
class term_of = typ_of +
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    58
  constrains typ_of :: "'a itself \<Rightarrow> typ"
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    59
  fixes term_of :: "'a \<Rightarrow> term"
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    60
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    61
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    62
structure TermOf =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    63
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    64
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    65
local
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    66
  val thy = the_context ();
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    67
  val const_term_of = Sign.intern_const thy "term_of";
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    68
  fun term_term_of ty = Const (const_term_of, ty --> Embed.typ_term);
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    69
in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    70
  val class_term_of = Sign.intern_class thy "term_of";
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    71
  fun mk_terms_of_defs vs (tyco, cs) =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    72
    let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    73
      val dty = Type (tyco, map TFree vs);
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    74
      fun mk_eq c =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    75
        let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    76
          val lhs : term = term_term_of dty $ c;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    77
          val rhs : term = Embed.term_term
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    78
            (fn (v, ty) => term_term_of ty $ Free (v, ty))
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    79
            (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
    80
        in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    81
          HOLogic.mk_eq (lhs, rhs)
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    82
        end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    83
    in map mk_eq cs end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    84
  fun mk_term_of t =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    85
    term_term_of (Term.fastype_of t) $ t;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    86
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    87
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    88
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    89
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    90
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    91
setup {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    92
let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    93
  fun mk thy arities css =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    94
    let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    95
      val vs = (Name.names Name.context "'a" o snd o fst o hd) arities;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    96
      val raw_defs = map (TermOf.mk_terms_of_defs vs) css;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    97
      fun mk' thy' = map (apfst (rpair [])) ((PrimrecPackage.mk_combdefs thy' o flat) raw_defs)
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    98
    in ClassPackage.assume_arities_thy thy arities mk' end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    99
  fun tac _ = ClassPackage.intro_classes_tac [];
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   100
  fun hook specs =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   101
    if (fst o hd) specs = (fst o dest_Type) Embed.typ_typ then I
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   102
    else
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   103
      DatatypeCodegen.prove_codetypes_arities tac
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   104
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   105
      [TermOf.class_term_of] mk I
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   106
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   107
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   108
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   109
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   110
subsection {* Evaluation infrastructure *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   111
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   112
lemma lift_eq_Trueprop:
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   113
  "p == q \<Longrightarrow> Trueprop p == Trueprop q" by auto
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   114
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   115
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   116
signature EVAL =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   117
sig
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   118
  val eval_term: theory -> term -> term
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   119
  val term: string -> unit
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   120
  val eval_ref: term option ref
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   121
  val oracle: string * (theory * exn -> term)
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   122
  val method: Method.src -> Proof.context -> Method.method
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   123
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   124
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   125
structure Eval : EVAL =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   126
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   127
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   128
val eval_ref = ref NONE;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   129
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   130
fun eval_term thy t =
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   131
  CodegenPackage.eval_term
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   132
    thy (("Eval.eval_ref", eval_ref), TermOf.mk_term_of t);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   133
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   134
fun term t =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   135
  let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   136
    val thy = the_context ();
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   137
    val t = eval_term thy (Sign.read_term thy t);
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   138
  in (writeln o Sign.string_of_term thy) t end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   139
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   140
val lift_eq_Trueprop = thm "lift_eq_Trueprop";
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   141
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   142
exception Eval of term;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   143
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   144
val oracle = ("Eval", fn (thy, Eval t) =>
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   145
  Logic.mk_equals (t, eval_term thy t));
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   146
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   147
val oracle_name = NameSpace.pack [Context.theory_name (the_context ()), fst oracle];
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   148
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   149
fun conv ct =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   150
  let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   151
    val {thy, t, ...} = rep_cterm ct;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   152
    val t' = HOLogic.dest_Trueprop t;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   153
    val thm' = Thm.invoke_oracle_i thy oracle_name (thy, Eval t');
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   154
  in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   155
    lift_eq_Trueprop OF [thm']
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   156
  end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   157
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   158
fun tac i = Tactical.PRIMITIVE (Drule.fconv_rule
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   159
  (Drule.goals_conv (equal i) conv));
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   160
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   161
val method =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   162
  Method.no_args (Method.METHOD (fn _ =>
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   163
    tac 1 THEN rtac TrueI 1));
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   164
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   165
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   166
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   167
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   168
setup {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   169
  Theory.add_oracle Eval.oracle
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   170
  #> Method.add_method ("eval", Eval.method, "solve goal by evaluation")
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   171
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   172
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   173
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   174
subsection {* Small examples *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   175
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   176
ML {* Eval.term "[]::nat list" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   177
ML {* Eval.term "(Suc 2 + Suc 0) * Suc 3" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   178
ML {* Eval.term "fst ([]::nat list, Suc 0) = []" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   179
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   180
text {* a fancy datatype *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   181
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   182
datatype ('a, 'b) bair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   183
    Bair "'a\<Colon>order" 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   184
  | Shift "('a, 'b) cair"
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   185
  | Dummy unit
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   186
and ('a, 'b) cair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   187
    Cair 'a 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   188
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   189
ML {* Eval.term "Shift (Cair (4::nat) [Suc 0])" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   190
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   191
lemma
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   192
  "Suc 0 = 1" by eval
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   193
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   194
lemma
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   195
  "rev [0, Suc 0, Suc 0] = [Suc 0, Suc 0, 0]" by eval
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   196
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   197
lemma
20435
haftmann
parents: 20427
diff changeset
   198
  "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
   199
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   200
end