src/HOL/Code_Generator.thy
author wenzelm
Fri, 19 Jan 2007 22:08:06 +0100
changeset 22099 5dc00ac4bd8e
parent 22004 a69d21fc6d68
child 22385 cc2be3315e72
permissions -rw-r--r--
simplified ML setup;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     1
(*  ID:         $Id$
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     3
*)
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     4
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     5
header {* Setup of code generator tools *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     6
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     7
theory Code_Generator
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     8
imports HOL
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
     9
begin
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    10
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    11
subsection {* ML code generator *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    12
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    13
types_code
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    14
  "bool"  ("bool")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    15
attach (term_of) {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    16
fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    17
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    18
attach (test) {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    19
fun gen_bool i = one_of [false, true];
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    20
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    21
  "prop"  ("bool")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    22
attach (term_of) {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    23
fun term_of_prop b =
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    24
  HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    25
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    26
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    27
consts_code
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    28
  "Trueprop" ("(_)")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    29
  "True"    ("true")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    30
  "False"   ("false")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    31
  "Not"     ("not")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    32
  "op |"    ("(_ orelse/ _)")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    33
  "op &"    ("(_ andalso/ _)")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    34
  "HOL.If"      ("(if _/ then _/ else _)")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    35
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    36
setup {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    37
let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    38
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    39
fun eq_codegen thy defs gr dep thyname b t =
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    40
    (case strip_comb t of
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    41
       (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    42
     | (Const ("op =", _), [t, u]) =>
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    43
          let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    44
            val (gr', pt) = Codegen.invoke_codegen thy defs dep thyname false (gr, t);
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    45
            val (gr'', pu) = Codegen.invoke_codegen thy defs dep thyname false (gr', u);
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    46
            val (gr''', _) = Codegen.invoke_tycodegen thy defs dep thyname false (gr'', HOLogic.boolT)
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    47
          in
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    48
            SOME (gr''', Codegen.parens
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    49
              (Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu]))
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    50
          end
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    51
     | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    52
         thy defs dep thyname b (gr, Codegen.eta_expand t ts 2))
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    53
     | _ => NONE);
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    54
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    55
in
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    56
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    57
Codegen.add_codegen "eq_codegen" eq_codegen
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    58
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    59
end
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    60
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    61
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    62
text {* Evaluation *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    63
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
    64
method_setup evaluation = {*
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    65
let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    66
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    67
fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    68
  (Drule.goals_conv (equal i) Codegen.evaluation_conv));
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    69
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
    70
in Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI)) end
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
    71
*} "solve goal by evaluation"
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    72
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    73
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    74
subsection {* Generic code generator setup *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    75
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    76
text {* operational equality for code generation *}
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    77
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    78
axclass eq \<subseteq> type
21951
56abe5f3c612 changed syntax for axclass attach
haftmann
parents: 21904
diff changeset
    79
  (attach "op =")
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    80
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    81
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    82
text {* equality for Haskell *}
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    83
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    84
code_class eq
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    85
  (Haskell "Eq" where "op =" \<equiv> "(==)")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    86
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    87
code_const "op ="
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    88
  (Haskell infixl 4 "==")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    89
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    90
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    91
text {* boolean expressions *}
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    92
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    93
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    94
  shows "(False \<and> x) = False"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    95
    and "(True \<and> x) = x"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    96
    and "(x \<and> False) = False"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    97
    and "(x \<and> True) = x" by simp_all
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    98
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    99
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   100
  shows "(False \<or> x) = x"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   101
    and "(True \<or> x) = True"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   102
    and "(x \<or> False) = x"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   103
    and "(x \<or> True) = True" by simp_all
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   104
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   105
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   106
  shows "(\<not> True) = False"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   107
    and "(\<not> False) = True" by (rule HOL.simp_thms)+
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   108
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   109
lemmas [code func] = imp_conv_disj
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   110
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   111
lemmas [code func] = if_True if_False
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   112
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   113
instance bool :: eq ..
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   114
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   115
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   116
  "True = P \<longleftrightarrow> P" by simp
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   117
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   118
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   119
  "False = P \<longleftrightarrow> \<not> P" by simp
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   120
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   121
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   122
  "P = True \<longleftrightarrow> P" by simp
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   123
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   124
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   125
  "P = False \<longleftrightarrow> \<not> P" by simp
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   126
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   127
code_type bool
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   128
  (SML "bool")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   129
  (OCaml "bool")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   130
  (Haskell "Bool")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   131
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   132
code_instance bool :: eq
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   133
  (Haskell -)
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   134
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   135
code_const "op = \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   136
  (Haskell infixl 4 "==")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   137
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   138
code_const True and False and Not and "op &" and "op |" and If
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   139
  (SML "true" and "false" and "not"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   140
    and infixl 1 "andalso" and infixl 0 "orelse"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   141
    and "!(if (_)/ then (_)/ else (_))")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   142
  (OCaml "true" and "false" and "not"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   143
    and infixl 4 "&&" and infixl 2 "||"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   144
    and "!(if (_)/ then (_)/ else (_))")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   145
  (Haskell "True" and "False" and "not"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   146
    and infixl 3 "&&" and infixl 2 "||"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   147
    and "!(if (_)/ then (_)/ else (_))")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   148
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   149
code_reserved SML
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   150
  bool true false not
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   151
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   152
code_reserved OCaml
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   153
  bool true false not
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   154
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   155
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   156
text {* itself as a code generator datatype *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   157
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   158
setup {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   159
  let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   160
    val v = ("'a", []);
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   161
    val t = Logic.mk_type (TFree v);
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   162
    val Const (c, ty) = t;
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   163
    val (_, Type (dtco, _)) = strip_type ty;
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   164
  in
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   165
    CodegenData.add_datatype (dtco, (([v], [(c, [])]), CodegenData.lazy (fn () => [])))
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   166
  end
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   167
*} 
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   168
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   169
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   170
text {* code generation for arbitrary as exception *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   171
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   172
setup {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   173
  CodegenSerializer.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")"
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   174
  #> CodegenSerializer.add_undefined "OCaml" "arbitrary" "(failwith \"arbitrary\")"
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   175
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   176
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   177
code_const arbitrary
21110
fc98cb66c5c3 adaptions to changes in preprocessor
haftmann
parents: 21079
diff changeset
   178
  (Haskell "error/ \"arbitrary\"")
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   179
21079
747d716e98d0 added reserved words for Haskell
haftmann
parents: 21059
diff changeset
   180
code_reserved SML Fail
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   181
code_reserved OCaml failwith
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   182
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   183
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   184
subsection {* Evaluation oracle *}
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   185
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   186
ML {*
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   187
structure HOL_Eval:
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   188
sig
22004
a69d21fc6d68 fixed eval oracle
haftmann
parents: 21951
diff changeset
   189
  val reff: bool option ref
a69d21fc6d68 fixed eval oracle
haftmann
parents: 21951
diff changeset
   190
  val prop: theory -> term -> term
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   191
end =
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   192
struct
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   193
22004
a69d21fc6d68 fixed eval oracle
haftmann
parents: 21951
diff changeset
   194
val reff : bool option ref = ref NONE;
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   195
22004
a69d21fc6d68 fixed eval oracle
haftmann
parents: 21951
diff changeset
   196
fun prop thy t =
21869
c5e79547bf54 new-style oracle setup
haftmann
parents: 21587
diff changeset
   197
  if CodegenPackage.eval_term thy
22004
a69d21fc6d68 fixed eval oracle
haftmann
parents: 21951
diff changeset
   198
    (("HOL_Eval.reff", reff), t)
21869
c5e79547bf54 new-style oracle setup
haftmann
parents: 21587
diff changeset
   199
    then HOLogic.true_const
22004
a69d21fc6d68 fixed eval oracle
haftmann
parents: 21951
diff changeset
   200
    else HOLogic.false_const
a69d21fc6d68 fixed eval oracle
haftmann
parents: 21951
diff changeset
   201
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   202
end
21869
c5e79547bf54 new-style oracle setup
haftmann
parents: 21587
diff changeset
   203
*}
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   204
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   205
oracle eval_oracle ("term") = {*
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   206
  fn thy => fn t => Logic.mk_equals (t, HOL_Eval.prop thy t)
21869
c5e79547bf54 new-style oracle setup
haftmann
parents: 21587
diff changeset
   207
*}
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   208
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   209
method_setup eval = {*
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   210
let
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   211
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   212
fun conv ct =
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   213
  let val {thy, t, ...} = rep_cterm ct
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   214
  in eval_oracle thy t end;
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   215
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   216
fun eval_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
21546
268b6bed0cc8 removed HOL structure
haftmann
parents: 21454
diff changeset
   217
  (Drule.goals_conv (equal i) (HOLogic.Trueprop_conv conv)));
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   218
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   219
in Method.no_args (Method.SIMPLE_METHOD' (eval_tac THEN' rtac TrueI)) end
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   220
*} "solve goal by evaluation"
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   221
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   222
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   223
subsection {* Normalization by evaluation *}
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   224
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   225
method_setup normalization = {*
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   226
let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   227
  fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
21546
268b6bed0cc8 removed HOL structure
haftmann
parents: 21454
diff changeset
   228
    (Drule.goals_conv (equal i) (HOLogic.Trueprop_conv
21149
ee207b9b8bf5 dropped equals_conv for nbe
haftmann
parents: 21110
diff changeset
   229
      NBE.normalization_conv)));
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   230
in
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   231
  Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl]))
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   232
end
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   233
*} "solve goal by normalization"
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   234
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   235
21059
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   236
text {* lazy @{const If} *}
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   237
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   238
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21378
diff changeset
   239
  if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where
21059
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   240
  "if_delayed b f g = (if b then f True else g False)"
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   241
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   242
lemma [code func]:
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   243
  shows "if_delayed True f g = f True"
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   244
    and "if_delayed False f g = g False"
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   245
  unfolding if_delayed_def by simp_all
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   246
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   247
lemma [normal pre, symmetric, normal post]:
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   248
  "(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)"
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   249
  unfolding if_delayed_def ..
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   250
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   251
21454
a1937c51ed88 dropped eq const
haftmann
parents: 21404
diff changeset
   252
hide (open) const if_delayed
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   253
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   254
end