src/HOL/Code_Generator.thy
author haftmann
Fri, 01 Jun 2007 10:44:26 +0200
changeset 23181 f52b555f8141
parent 22922 66baa75eae06
permissions -rw-r--r--
localized
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
22886
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
     9
uses "~~/src/HOL/Tools/recfun_codegen.ML"
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    10
begin
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    11
22886
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    12
ML {*
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    13
structure HOL =
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    14
struct
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    15
  val thy = theory "HOL";
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    16
end;
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    17
*}  -- "belongs to theory HOL"
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    18
22385
cc2be3315e72 syntax for "class attach const"
haftmann
parents: 22099
diff changeset
    19
subsection {* SML code generator setup *}
21046
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
types_code
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    22
  "bool"  ("bool")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    23
attach (term_of) {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    24
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
    25
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    26
attach (test) {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    27
fun gen_bool i = one_of [false, true];
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    28
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    29
  "prop"  ("bool")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    30
attach (term_of) {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    31
fun term_of_prop b =
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    32
  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
    33
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    34
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    35
consts_code
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    36
  "Trueprop" ("(_)")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    37
  "True"    ("true")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    38
  "False"   ("false")
22922
66baa75eae06 Name of ML function "not" is now qualified in order to avoid
berghofe
parents: 22921
diff changeset
    39
  "Not"     ("Bool.not")
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    40
  "op |"    ("(_ orelse/ _)")
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    41
  "op &"    ("(_ andalso/ _)")
22921
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22900
diff changeset
    42
  "If"      ("(if _/ then _/ else _)")
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    43
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    44
setup {*
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    45
let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    46
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    47
fun eq_codegen thy defs gr dep thyname b t =
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    48
    (case strip_comb t of
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    49
       (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    50
     | (Const ("op =", _), [t, u]) =>
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    51
          let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    52
            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
    53
            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
    54
            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
    55
          in
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    56
            SOME (gr''', Codegen.parens
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    57
              (Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu]))
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    58
          end
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    59
     | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    60
         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
    61
     | _ => NONE);
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    62
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    63
in
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    64
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    65
Codegen.add_codegen "eq_codegen" eq_codegen
22886
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
    66
#> RecfunCodegen.setup
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    67
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    68
end
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    69
*}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    70
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    71
text {* Evaluation *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    72
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
    73
method_setup evaluation = {*
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    74
let
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    75
22900
f8a7c10e1bd0 moved conversions to structure Conv;
wenzelm
parents: 22886
diff changeset
    76
fun evaluation_tac i = Tactical.PRIMITIVE (Conv.fconv_rule
f8a7c10e1bd0 moved conversions to structure Conv;
wenzelm
parents: 22886
diff changeset
    77
  (Conv.goals_conv (equal i) Codegen.evaluation_conv));
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    78
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
    79
in Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI)) end
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
    80
*} "solve goal by evaluation"
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    81
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    82
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    83
subsection {* Generic code generator setup *}
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
    84
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    85
text {* operational equality for code generation *}
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    86
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22423
diff changeset
    87
class eq (attach "op =") = type
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    88
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    89
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22758
diff changeset
    90
text {* using built-in Haskell equality *}
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    91
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    92
code_class eq
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    93
  (Haskell "Eq" where "op =" \<equiv> "(==)")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    94
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    95
code_const "op ="
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    96
  (Haskell infixl 4 "==")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    97
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
    98
22423
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
    99
text {* type bool *}
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   100
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   101
code_datatype True False
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   102
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   103
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   104
  shows "(False \<and> x) = False"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   105
    and "(True \<and> x) = x"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   106
    and "(x \<and> False) = False"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   107
    and "(x \<and> True) = x" by simp_all
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
lemma [code func]:
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   110
  shows "(False \<or> x) = x"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   111
    and "(True \<or> x) = True"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   112
    and "(x \<or> False) = x"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   113
    and "(x \<or> True) = True" by simp_all
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
  shows "(\<not> True) = False"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   117
    and "(\<not> False) = True" by (rule HOL.simp_thms)+
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   118
22886
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
   119
lemmas [code] = imp_conv_disj
21904
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
lemmas [code func] = if_True if_False
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   122
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   123
instance bool :: eq ..
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   124
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   125
lemma [code func]:
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22758
diff changeset
   126
  shows "True = P \<longleftrightarrow> P"
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22758
diff changeset
   127
    and "False = P \<longleftrightarrow> \<not> P"
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22758
diff changeset
   128
    and "P = True \<longleftrightarrow> P"
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22758
diff changeset
   129
    and "P = False \<longleftrightarrow> \<not> P" by simp_all
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   130
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   131
code_type bool
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   132
  (SML "bool")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   133
  (OCaml "bool")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   134
  (Haskell "Bool")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   135
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   136
code_instance bool :: eq
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   137
  (Haskell -)
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   138
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   139
code_const "op = \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   140
  (Haskell infixl 4 "==")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   141
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   142
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
   143
  (SML "true" and "false" and "not"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   144
    and infixl 1 "andalso" and infixl 0 "orelse"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   145
    and "!(if (_)/ then (_)/ else (_))")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   146
  (OCaml "true" and "false" and "not"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   147
    and infixl 4 "&&" and infixl 2 "||"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   148
    and "!(if (_)/ then (_)/ else (_))")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   149
  (Haskell "True" and "False" and "not"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   150
    and infixl 3 "&&" and infixl 2 "||"
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   151
    and "!(if (_)/ then (_)/ else (_))")
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   152
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   153
code_reserved SML
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   154
  bool true false not
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   155
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   156
code_reserved OCaml
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   157
  bool true false not
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   158
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   159
22423
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   160
text {* type prop *}
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   161
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   162
code_datatype Trueprop "prop"
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   163
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   164
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   165
text {* type itself *}
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   166
22423
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   167
code_datatype "TYPE('a)"
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   168
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22385
diff changeset
   169
22480
b20bc8029edb switched exception from arbitrary to undefined
haftmann
parents: 22473
diff changeset
   170
text {* code generation for undefined as exception *}
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   171
22480
b20bc8029edb switched exception from arbitrary to undefined
haftmann
parents: 22473
diff changeset
   172
code_const undefined
22499
68c8a8390e16 fixed code generator setup
haftmann
parents: 22487
diff changeset
   173
  (SML "raise/ Fail/ \"undefined\"")
68c8a8390e16 fixed code generator setup
haftmann
parents: 22487
diff changeset
   174
  (OCaml "failwith/ \"undefined\"")
22480
b20bc8029edb switched exception from arbitrary to undefined
haftmann
parents: 22473
diff changeset
   175
  (Haskell "error/ \"undefined\"")
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   176
21079
747d716e98d0 added reserved words for Haskell
haftmann
parents: 21059
diff changeset
   177
code_reserved SML Fail
21904
59fcfa2a77ea moved code generator bool setup here
haftmann
parents: 21869
diff changeset
   178
code_reserved OCaml failwith
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   179
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   180
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   181
subsection {* Evaluation oracle *}
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   182
22487
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   183
oracle eval_oracle ("term") = {* fn thy => fn t => 
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   184
  if CodegenPackage.satisfies thy (HOLogic.dest_Trueprop t) [] 
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   185
  then t
22886
cdff6ef76009 moved recfun_codegen.ML to Code_Generator.thy
haftmann
parents: 22845
diff changeset
   186
  else HOLogic.Trueprop $ HOLogic.true_const (*dummy*)
21869
c5e79547bf54 new-style oracle setup
haftmann
parents: 21587
diff changeset
   187
*}
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   188
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   189
method_setup eval = {*
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   190
let
22487
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   191
  fun eval_tac thy = 
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   192
    SUBGOAL (fn (t, i) => rtac (eval_oracle thy t) i)
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   193
in 
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   194
  Method.ctxt_args (fn ctxt => 
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   195
    Method.SIMPLE_METHOD' (eval_tac (ProofContext.theory_of ctxt)))
8cff8a6cb995 simplified "eval" oracle method
krauss
parents: 22480
diff changeset
   196
end
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   197
*} "solve goal by evaluation"
21378
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   198
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   199
cedfce6fc725 added evaluation oracle
haftmann
parents: 21149
diff changeset
   200
subsection {* Normalization by evaluation *}
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   201
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   202
method_setup normalization = {*
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   203
let
22900
f8a7c10e1bd0 moved conversions to structure Conv;
wenzelm
parents: 22886
diff changeset
   204
  fun normalization_tac i = Tactical.PRIMITIVE (Conv.fconv_rule
f8a7c10e1bd0 moved conversions to structure Conv;
wenzelm
parents: 22886
diff changeset
   205
    (Conv.goals_conv (equal i) (HOLogic.Trueprop_conv
21149
ee207b9b8bf5 dropped equals_conv for nbe
haftmann
parents: 21110
diff changeset
   206
      NBE.normalization_conv)));
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   207
in
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   208
  Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl]))
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   209
end
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   210
*} "solve goal by normalization"
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   211
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   212
21059
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   213
text {* lazy @{const If} *}
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   214
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   215
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21378
diff changeset
   216
  if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22758
diff changeset
   217
  [code func del]: "if_delayed b f g = (if b then f True else g False)"
21059
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   218
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   219
lemma [code func]:
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   220
  shows "if_delayed True f g = f True"
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   221
    and "if_delayed False f g = g False"
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   222
  unfolding if_delayed_def by simp_all
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   223
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   224
lemma [normal pre, symmetric, normal post]:
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   225
  "(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)"
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   226
  unfolding if_delayed_def ..
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   227
21454
a1937c51ed88 dropped eq const
haftmann
parents: 21404
diff changeset
   228
hide (open) const if_delayed
21046
fe1db2f991a7 moved HOL code generator setup to Code_Generator
haftmann
parents:
diff changeset
   229
22099
5dc00ac4bd8e simplified ML setup;
wenzelm
parents: 22004
diff changeset
   230
end