src/HOL/ex/CodeEval.thy
author haftmann
Thu, 04 Jan 2007 20:01:00 +0100
changeset 22005 0faa5afd5d69
parent 21924 fe474e69e603
child 22017 9b1656a28c88
permissions -rw-r--r--
examples for evaluation oracle
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
subsection {* The typ_of class *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    12
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    13
class typ_of =
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    14
  fixes typ_of :: "'a itself \<Rightarrow> typ"
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    15
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    16
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    17
structure TypOf =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    18
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    19
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    20
local
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    21
  val thy = the_context ();
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    22
  val const_typ_of = Sign.intern_const thy "typ_of";
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    23
  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
    24
in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    25
  val class_typ_of = Sign.intern_class thy "typ_of";
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    26
  fun term_typ_of_type ty =
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    27
    term_typ_of ty $ Logic.mk_type ty;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    28
  fun mk_typ_of_def ty =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    29
    let
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    30
      val lhs = term_typ_of ty $ Free ("x", Term.itselfT ty)
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    31
      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
    32
    in Logic.mk_equals (lhs, rhs) end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    33
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    34
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
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    38
setup {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    39
let
20835
haftmann
parents: 20655
diff changeset
    40
  fun mk arities _ thy =
haftmann
parents: 20655
diff changeset
    41
    (maps (fn ((tyco, asorts), _) => [(("", []), TypOf.mk_typ_of_def
haftmann
parents: 20655
diff changeset
    42
      (Type (tyco, map TFree (Name.names Name.context "'a" asorts))))]) arities, thy);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    43
  fun hook specs =
21924
fe474e69e603 simplified class_package
haftmann
parents: 21836
diff changeset
    44
    DatatypeCodegen.prove_codetypes_arities (ClassPackage.intro_classes_tac [])
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    45
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
20835
haftmann
parents: 20655
diff changeset
    46
      [TypOf.class_typ_of] mk ((K o K) I)
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    47
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    48
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    49
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    50
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    51
subsection {* term_of class *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    52
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    53
class term_of = typ_of +
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    54
  constrains typ_of :: "'a itself \<Rightarrow> typ"
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    55
  fixes term_of :: "'a \<Rightarrow> term"
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    56
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    57
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    58
structure TermOf =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    59
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    60
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    61
local
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    62
  val thy = the_context ();
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    63
  val const_term_of = Sign.intern_const thy "term_of";
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    64
  fun term_term_of ty = Const (const_term_of, ty --> Embed.typ_term);
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    65
in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    66
  val class_term_of = Sign.intern_class thy "term_of";
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    67
  fun mk_terms_of_defs vs (tyco, cs) =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    68
    let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    69
      val dty = Type (tyco, map TFree vs);
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    70
      fun mk_eq c =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    71
        let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    72
          val lhs : term = term_term_of dty $ c;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    73
          val rhs : term = Embed.term_term
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    74
            (fn (v, ty) => term_term_of ty $ Free (v, ty))
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
    75
            (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
    76
        in
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    77
          HOLogic.mk_eq (lhs, rhs)
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    78
        end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    79
    in map mk_eq cs end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    80
  fun mk_term_of t =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    81
    term_term_of (Term.fastype_of t) $ t;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    82
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    83
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    84
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    85
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    86
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    87
setup {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
    88
let
20835
haftmann
parents: 20655
diff changeset
    89
  fun thy_note ((name, atts), thms) =
haftmann
parents: 20655
diff changeset
    90
    PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
haftmann
parents: 20655
diff changeset
    91
  fun thy_def ((name, atts), t) =
haftmann
parents: 20655
diff changeset
    92
    PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
haftmann
parents: 20655
diff changeset
    93
  fun mk arities css thy =
20427
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;
20835
haftmann
parents: 20655
diff changeset
    96
      val defs = map (TermOf.mk_terms_of_defs vs) css;
haftmann
parents: 20655
diff changeset
    97
      val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs;
haftmann
parents: 20655
diff changeset
    98
    in
haftmann
parents: 20655
diff changeset
    99
      thy
haftmann
parents: 20655
diff changeset
   100
      |> PrimrecPackage.gen_primrec thy_note thy_def "" defs'
haftmann
parents: 20655
diff changeset
   101
      |> snd
haftmann
parents: 20655
diff changeset
   102
    end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   103
  fun hook specs =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   104
    if (fst o hd) specs = (fst o dest_Type) Embed.typ_typ then I
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   105
    else
21924
fe474e69e603 simplified class_package
haftmann
parents: 21836
diff changeset
   106
      DatatypeCodegen.prove_codetypes_arities (ClassPackage.intro_classes_tac [])
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   107
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
20835
haftmann
parents: 20655
diff changeset
   108
      [TermOf.class_term_of] ((K o K o pair) []) mk
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   109
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   110
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   111
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   112
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   113
subsection {* Evaluation infrastructure *}
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
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   122
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   123
structure Eval : EVAL =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   124
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   125
20655
8c4d80e8025f circumvented defect in SML/NJ type inference
haftmann
parents: 20597
diff changeset
   126
val eval_ref = ref (NONE : term option);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   127
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   128
fun eval_term thy t =
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   129
  CodegenPackage.eval_term
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   130
    thy (("Eval.eval_ref", eval_ref), TermOf.mk_term_of t);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   131
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   132
fun term t =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   133
  let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   134
    val thy = the_context ();
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   135
    val t = eval_term thy (Sign.read_term thy t);
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   136
  in (writeln o Sign.string_of_term thy) t end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   137
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   138
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   139
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   140
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   141
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   142
subsection {* Small examples *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   143
21117
e8657a20a52f *** empty log message ***
haftmann
parents: 20835
diff changeset
   144
ML {* Eval.term "(Suc 2 + Suc 0) * Suc 3" *}
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   145
ML {* Eval.term "[]::nat list" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   146
ML {* Eval.term "fst ([]::nat list, Suc 0) = []" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   147
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   148
text {* a fancy datatype *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   149
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   150
datatype ('a, 'b) bair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   151
    Bair "'a\<Colon>order" 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   152
  | Shift "('a, 'b) cair"
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   153
  | Dummy unit
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   154
and ('a, 'b) cair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   155
    Cair 'a 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   156
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   157
ML {* Eval.term "Shift (Cair (4::nat) [Suc 0])" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   158
22005
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   159
text {* also test evaluation oracle *}
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   160
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   161
lemma "True \<or> False" by eval
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   162
lemma "\<not> (Suc 0 = Suc 1)" by eval
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   163
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   164
end