src/HOL/Code_Setup.thy
author haftmann
Thu Nov 13 15:58:37 2008 +0100 (2008-11-13)
changeset 28740 22a8125d66fa
parent 28699 32b6a8f12c1c
child 28856 5e009a80fe6d
permissions -rw-r--r--
improved handling of !!/==> for eval and normalization
     1 (*  Title:      HOL/Code_Setup.thy
     2     ID:         $Id$
     3     Author:     Florian Haftmann
     4 *)
     5 
     6 header {* Setup of code generators and related tools *}
     7 
     8 theory Code_Setup
     9 imports HOL
    10 begin
    11 
    12 subsection {* Generic code generator foundation *}
    13 
    14 text {* Datatypes *}
    15 
    16 code_datatype True False
    17 
    18 code_datatype "TYPE('a\<Colon>{})"
    19 
    20 code_datatype Trueprop "prop"
    21 
    22 text {* Code equations *}
    23 
    24 lemma [code]:
    25   shows "(True \<Longrightarrow> PROP P) \<equiv> PROP P" 
    26     and "(False \<Longrightarrow> Q) \<equiv> Trueprop True" 
    27     and "(PROP P \<Longrightarrow> True) \<equiv> Trueprop True" 
    28     and "(Q \<Longrightarrow> False) \<equiv> Trueprop (\<not> Q)" by (auto intro!: equal_intr_rule)
    29 
    30 lemma [code]:
    31   shows "False \<and> x \<longleftrightarrow> False"
    32     and "True \<and> x \<longleftrightarrow> x"
    33     and "x \<and> False \<longleftrightarrow> False"
    34     and "x \<and> True \<longleftrightarrow> x" by simp_all
    35 
    36 lemma [code]:
    37   shows "False \<or> x \<longleftrightarrow> x"
    38     and "True \<or> x \<longleftrightarrow> True"
    39     and "x \<or> False \<longleftrightarrow> x"
    40     and "x \<or> True \<longleftrightarrow> True" by simp_all
    41 
    42 lemma [code]:
    43   shows "\<not> True \<longleftrightarrow> False"
    44     and "\<not> False \<longleftrightarrow> True" by (rule HOL.simp_thms)+
    45 
    46 lemmas [code] = Let_def if_True if_False
    47 
    48 lemmas [code, code unfold, symmetric, code post] = imp_conv_disj
    49 
    50 text {* Equality *}
    51 
    52 context eq
    53 begin
    54 
    55 lemma equals_eq [code inline, code]: "op = \<equiv> eq"
    56   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule eq_equals)
    57 
    58 declare eq [code unfold, code inline del]
    59 
    60 declare equals_eq [symmetric, code post]
    61 
    62 end
    63 
    64 declare simp_thms(6) [code nbe]
    65 
    66 hide (open) const eq
    67 hide const eq
    68 
    69 setup {*
    70   Code_Unit.add_const_alias @{thm equals_eq}
    71 *}
    72 
    73 text {* Cases *}
    74 
    75 lemma Let_case_cert:
    76   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
    77   shows "CASE x \<equiv> f x"
    78   using assms by simp_all
    79 
    80 lemma If_case_cert:
    81   fixes meta_conjunction :: "prop => prop => prop"  (infixr "&&" 2)
    82   assumes "CASE \<equiv> (\<lambda>b. If b f g)"
    83   shows "(CASE True \<equiv> f) && (CASE False \<equiv> g)"
    84   using assms by simp_all
    85 
    86 setup {*
    87   Code.add_case @{thm Let_case_cert}
    88   #> Code.add_case @{thm If_case_cert}
    89   #> Code.add_undefined @{const_name undefined}
    90 *}
    91 
    92 code_abort undefined
    93 
    94 
    95 subsection {* Generic code generator preprocessor *}
    96 
    97 setup {*
    98   Code.map_pre (K HOL_basic_ss)
    99   #> Code.map_post (K HOL_basic_ss)
   100 *}
   101 
   102 
   103 subsection {* Generic code generator target languages *}
   104 
   105 text {* type bool *}
   106 
   107 code_type bool
   108   (SML "bool")
   109   (OCaml "bool")
   110   (Haskell "Bool")
   111 
   112 code_const True and False and Not and "op &" and "op |" and If
   113   (SML "true" and "false" and "not"
   114     and infixl 1 "andalso" and infixl 0 "orelse"
   115     and "!(if (_)/ then (_)/ else (_))")
   116   (OCaml "true" and "false" and "not"
   117     and infixl 4 "&&" and infixl 2 "||"
   118     and "!(if (_)/ then (_)/ else (_))")
   119   (Haskell "True" and "False" and "not"
   120     and infixl 3 "&&" and infixl 2 "||"
   121     and "!(if (_)/ then (_)/ else (_))")
   122 
   123 code_reserved SML
   124   bool true false not
   125 
   126 code_reserved OCaml
   127   bool not
   128 
   129 text {* using built-in Haskell equality *}
   130 
   131 code_class eq
   132   (Haskell "Eq")
   133 
   134 code_const "eq_class.eq"
   135   (Haskell infixl 4 "==")
   136 
   137 code_const "op ="
   138   (Haskell infixl 4 "==")
   139 
   140 text {* undefined *}
   141 
   142 code_const undefined
   143   (SML "!(raise/ Fail/ \"undefined\")")
   144   (OCaml "failwith/ \"undefined\"")
   145   (Haskell "error/ \"undefined\"")
   146 
   147 
   148 subsection {* SML code generator setup *}
   149 
   150 types_code
   151   "bool"  ("bool")
   152 attach (term_of) {*
   153 fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
   154 *}
   155 attach (test) {*
   156 fun gen_bool i =
   157   let val b = one_of [false, true]
   158   in (b, fn () => term_of_bool b) end;
   159 *}
   160   "prop"  ("bool")
   161 attach (term_of) {*
   162 fun term_of_prop b =
   163   HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
   164 *}
   165 
   166 consts_code
   167   "Trueprop" ("(_)")
   168   "True"    ("true")
   169   "False"   ("false")
   170   "Not"     ("Bool.not")
   171   "op |"    ("(_ orelse/ _)")
   172   "op &"    ("(_ andalso/ _)")
   173   "If"      ("(if _/ then _/ else _)")
   174 
   175 setup {*
   176 let
   177 
   178 fun eq_codegen thy defs dep thyname b t gr =
   179     (case strip_comb t of
   180        (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
   181      | (Const ("op =", _), [t, u]) =>
   182           let
   183             val (pt, gr') = Codegen.invoke_codegen thy defs dep thyname false t gr;
   184             val (pu, gr'') = Codegen.invoke_codegen thy defs dep thyname false u gr';
   185             val (_, gr''') = Codegen.invoke_tycodegen thy defs dep thyname false HOLogic.boolT gr'';
   186           in
   187             SOME (Codegen.parens
   188               (Pretty.block [pt, Codegen.str " =", Pretty.brk 1, pu]), gr''')
   189           end
   190      | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
   191          thy defs dep thyname b (Codegen.eta_expand t ts 2) gr)
   192      | _ => NONE);
   193 
   194 in
   195   Codegen.add_codegen "eq_codegen" eq_codegen
   196 end
   197 *}
   198 
   199 
   200 subsection {* Evaluation and normalization by evaluation *}
   201 
   202 ML {*
   203 structure Eval_Method =
   204 struct
   205 
   206 val eval_ref : (unit -> bool) option ref = ref NONE;
   207 
   208 end;
   209 *}
   210 
   211 oracle eval_oracle = {* fn ct =>
   212   let
   213     val thy = Thm.theory_of_cterm ct;
   214     val t = Thm.term_of ct;
   215     val dummy = @{cprop True};
   216   in case try HOLogic.dest_Trueprop t
   217    of SOME t' => if Code_ML.eval_term
   218          ("Eval_Method.eval_ref", Eval_Method.eval_ref) thy t' [] 
   219        then Thm.capply (Thm.capply @{cterm "op \<equiv> \<Colon> prop \<Rightarrow> prop \<Rightarrow> prop"} ct) dummy
   220        else dummy
   221     | NONE => dummy
   222   end
   223 *}
   224 
   225 ML {*
   226 fun gen_eval_method conv = Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD'
   227   (CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
   228     THEN' rtac TrueI))
   229 *}
   230 
   231 method_setup eval = {* gen_eval_method eval_oracle *}
   232   "solve goal by evaluation"
   233 
   234 method_setup evaluation = {* gen_eval_method Codegen.evaluation_conv *}
   235   "solve goal by evaluation"
   236 
   237 method_setup normalization = {* (Method.no_args o Method.SIMPLE_METHOD')
   238   (CONVERSION Nbe.norm_conv THEN' (fn k => TRY (rtac TrueI k)))
   239 *} "solve goal by normalization"
   240 
   241 
   242 subsection {* Quickcheck *}
   243 
   244 quickcheck_params [size = 5, iterations = 50]
   245 
   246 end