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