src/HOL/ex/CodeEval.thy
author wenzelm
Fri, 16 Feb 2007 22:46:03 +0100
changeset 22333 652f316ca26a
parent 22319 6f162dd72f60
child 22473 753123c89d72
permissions -rw-r--r--
unified arity parser/arguments;
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 =
22333
652f316ca26a unified arity parser/arguments;
wenzelm
parents: 22319
diff changeset
    41
    (maps (fn (tyco, asorts, _) => [(("", []), TypOf.mk_typ_of_def
20835
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
22333
652f316ca26a unified arity parser/arguments;
wenzelm
parents: 22319
diff changeset
    95
      val (_, asorts, _) :: _ = arities;
652f316ca26a unified arity parser/arguments;
wenzelm
parents: 22319
diff changeset
    96
      val vs = Name.names Name.context "'a" asorts;
20835
haftmann
parents: 20655
diff changeset
    97
      val defs = map (TermOf.mk_terms_of_defs vs) css;
haftmann
parents: 20655
diff changeset
    98
      val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs;
haftmann
parents: 20655
diff changeset
    99
    in
haftmann
parents: 20655
diff changeset
   100
      thy
haftmann
parents: 20655
diff changeset
   101
      |> PrimrecPackage.gen_primrec thy_note thy_def "" defs'
haftmann
parents: 20655
diff changeset
   102
      |> snd
haftmann
parents: 20655
diff changeset
   103
    end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   104
  fun hook specs =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   105
    if (fst o hd) specs = (fst o dest_Type) Embed.typ_typ then I
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   106
    else
21924
fe474e69e603 simplified class_package
haftmann
parents: 21836
diff changeset
   107
      DatatypeCodegen.prove_codetypes_arities (ClassPackage.intro_classes_tac [])
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   108
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
20835
haftmann
parents: 20655
diff changeset
   109
      [TermOf.class_term_of] ((K o K o pair) []) mk
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   110
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   111
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   112
22017
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   113
text {* Disable for characters and ml_strings *}
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   114
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   115
code_const "typ_of \<Colon> char itself \<Rightarrow> typ" and "term_of \<Colon> char \<Rightarrow> term"
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   116
  (SML "!((_); raise Fail \"typ'_of'_char\")"
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   117
    and "!((_); raise Fail \"term'_of'_char\")")
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   118
  (OCaml "!((_); failwith \"typ'_of'_char\")"
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   119
    and "!((_); failwith \"term'_of'_char\")")
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   120
  (Haskell "error/ \"typ'_of'_char\""
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   121
    and "error/ \"term'_of'_char\"")
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   122
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   123
code_const "term_of \<Colon> ml_string \<Rightarrow> term"
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   124
  (SML "!((_); raise Fail \"term'_of'_ml'_string\")")
9b1656a28c88 dealing with ml_string and char instances
haftmann
parents: 22005
diff changeset
   125
  (OCaml "!((_); failwith \"term'_of'_ml'_string\")")
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   126
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   127
subsection {* Evaluation infrastructure *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   128
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   129
ML {*
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   130
signature EVAL =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   131
sig
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   132
  val eval_term: theory -> term -> term
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   133
  val term: string -> unit
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   134
  val eval_ref: term option ref
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   135
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   136
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   137
structure Eval : EVAL =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   138
struct
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   139
20655
8c4d80e8025f circumvented defect in SML/NJ type inference
haftmann
parents: 20597
diff changeset
   140
val eval_ref = ref (NONE : term option);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   141
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   142
fun eval_term thy t =
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   143
  CodegenPackage.eval_term
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   144
    thy (("Eval.eval_ref", eval_ref), TermOf.mk_term_of t);
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   145
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   146
fun term t =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   147
  let
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   148
    val thy = the_context ();
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   149
    val t = eval_term thy (Sign.read_term thy t);
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   150
  in (writeln o Sign.string_of_term thy) t end;
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   151
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   152
end;
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   153
*}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   154
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   155
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   156
subsection {* Small examples *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   157
22319
6f162dd72f60 cleanup
haftmann
parents: 22017
diff changeset
   158
ML {* Eval.term "(Suc 2 + 1) * 4" *}
21117
e8657a20a52f *** empty log message ***
haftmann
parents: 20835
diff changeset
   159
ML {* Eval.term "(Suc 2 + Suc 0) * Suc 3" *}
20427
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   160
ML {* Eval.term "[]::nat list" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   161
ML {* Eval.term "fst ([]::nat list, Suc 0) = []" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   162
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   163
text {* a fancy datatype *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   164
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   165
datatype ('a, 'b) bair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   166
    Bair "'a\<Colon>order" 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   167
  | Shift "('a, 'b) cair"
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   168
  | Dummy unit
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   169
and ('a, 'b) cair =
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   170
    Cair 'a 'b
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   171
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   172
ML {* Eval.term "Shift (Cair (4::nat) [Suc 0])" *}
0b102b4182de added and refined some exmples
haftmann
parents:
diff changeset
   173
22005
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   174
text {* also test evaluation oracle *}
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   175
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   176
lemma "True \<or> False" by eval
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   177
lemma "\<not> (Suc 0 = Suc 1)" by eval
0faa5afd5d69 examples for evaluation oracle
haftmann
parents: 21924
diff changeset
   178
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20453
diff changeset
   179
end