src/HOL/Code_Setup.thy
author huffman
Mon, 20 Aug 2007 19:52:52 +0200
changeset 24355 93d78fdeb55a
parent 24293 7e67b9706211
child 24463 841c2e24761f
permissions -rw-r--r--
remove int_of_nat
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24285
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Code_Setup.thy
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     2
    ID:         $Id$
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     4
*)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     5
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     6
header {* Setup of code generators and derived tools *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     7
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     8
theory Code_Setup
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
     9
imports HOL
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    10
uses "~~/src/HOL/Tools/recfun_codegen.ML"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    11
begin
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    12
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    13
subsection {* SML code generator setup *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    14
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    15
setup RecfunCodegen.setup
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    16
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    17
types_code
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    18
  "bool"  ("bool")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    19
attach (term_of) {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    20
fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    21
*}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    22
attach (test) {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    23
fun gen_bool i = one_of [false, true];
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    24
*}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    25
  "prop"  ("bool")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    26
attach (term_of) {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    27
fun term_of_prop b =
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    28
  HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    29
*}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    30
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    31
consts_code
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    32
  "Trueprop" ("(_)")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    33
  "True"    ("true")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    34
  "False"   ("false")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    35
  "Not"     ("Bool.not")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    36
  "op |"    ("(_ orelse/ _)")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    37
  "op &"    ("(_ andalso/ _)")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    38
  "If"      ("(if _/ then _/ else _)")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    39
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    40
setup {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    41
let
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    42
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    43
fun eq_codegen thy defs gr dep thyname b t =
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    44
    (case strip_comb t of
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    45
       (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    46
     | (Const ("op =", _), [t, u]) =>
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    47
          let
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    48
            val (gr', pt) = Codegen.invoke_codegen thy defs dep thyname false (gr, t);
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    49
            val (gr'', pu) = Codegen.invoke_codegen thy defs dep thyname false (gr', u);
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    50
            val (gr''', _) = Codegen.invoke_tycodegen thy defs dep thyname false (gr'', HOLogic.boolT)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    51
          in
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    52
            SOME (gr''', Codegen.parens
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    53
              (Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu]))
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    54
          end
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    55
     | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    56
         thy defs dep thyname b (gr, Codegen.eta_expand t ts 2))
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    57
     | _ => NONE);
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    58
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    59
in
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    60
  Codegen.add_codegen "eq_codegen" eq_codegen
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    61
end
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    62
*}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    63
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    64
text {* Evaluation *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    65
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    66
method_setup evaluation = {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    67
  Method.no_args (Method.SIMPLE_METHOD' (CONVERSION Codegen.evaluation_conv THEN' rtac TrueI))
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    68
*} "solve goal by evaluation"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    69
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    70
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    71
subsection {* Generic code generator setup *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    72
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    73
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    74
text {* using built-in Haskell equality *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    75
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    76
code_class eq
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    77
  (Haskell "Eq" where "op =" \<equiv> "(==)")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    78
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    79
code_const "op ="
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    80
  (Haskell infixl 4 "==")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    81
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    82
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    83
text {* type bool *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    84
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    85
lemmas [code] = imp_conv_disj
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    86
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    87
code_type bool
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    88
  (SML "bool")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    89
  (OCaml "bool")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    90
  (Haskell "Bool")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    91
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    92
code_instance bool :: eq
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    93
  (Haskell -)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    94
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    95
code_const "op = \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    96
  (Haskell infixl 4 "==")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    97
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    98
code_const True and False and Not and "op &" and "op |" and If
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
    99
  (SML "true" and "false" and "not"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   100
    and infixl 1 "andalso" and infixl 0 "orelse"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   101
    and "!(if (_)/ then (_)/ else (_))")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   102
  (OCaml "true" and "false" and "not"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   103
    and infixl 4 "&&" and infixl 2 "||"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   104
    and "!(if (_)/ then (_)/ else (_))")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   105
  (Haskell "True" and "False" and "not"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   106
    and infixl 3 "&&" and infixl 2 "||"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   107
    and "!(if (_)/ then (_)/ else (_))")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   108
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   109
code_reserved SML
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   110
  bool true false not
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   111
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   112
code_reserved OCaml
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   113
  bool not
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   114
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   115
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   116
text {* code generation for undefined as exception *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   117
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   118
code_const undefined
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   119
  (SML "raise/ Fail/ \"undefined\"")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   120
  (OCaml "failwith/ \"undefined\"")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   121
  (Haskell "error/ \"undefined\"")
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   122
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   123
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   124
text {* Let and If *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   125
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   126
lemmas [code func] = Let_def if_True if_False
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   127
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   128
setup {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   129
  CodePackage.add_appconst (@{const_name Let}, CodePackage.appgen_let)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   130
  #> CodePackage.add_appconst (@{const_name If}, CodePackage.appgen_if)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   131
*}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   132
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   133
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   134
subsection {* Evaluation oracle *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   135
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   136
oracle eval_oracle ("term") = {* fn thy => fn t => 
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   137
  if CodePackage.satisfies thy (Thm.cterm_of thy (HOLogic.dest_Trueprop t)) [] 
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   138
  then t
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   139
  else HOLogic.Trueprop $ HOLogic.true_const (*dummy*)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   140
*}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   141
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   142
method_setup eval = {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   143
let
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   144
  fun eval_tac thy = 
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   145
    SUBGOAL (fn (t, i) => rtac (eval_oracle thy t) i)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   146
in 
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   147
  Method.ctxt_args (fn ctxt => 
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   148
    Method.SIMPLE_METHOD' (eval_tac (ProofContext.theory_of ctxt)))
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   149
end
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   150
*} "solve goal by evaluation"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   151
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   152
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   153
subsection {* Normalization by evaluation *}
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   154
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   155
method_setup normalization = {*
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   156
  Method.no_args (Method.SIMPLE_METHOD'
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   157
    (CONVERSION (ObjectLogic.judgment_conv Nbe.normalization_conv)
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   158
      THEN' resolve_tac [TrueI, refl]))
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   159
*} "solve goal by normalization"
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   160
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   161
066bb557570f added Code_Setup
haftmann
parents:
diff changeset
   162
end