src/HOL/Code_Setup.thy
changeset 31007 7c871a9cf6f4
parent 31006 644d18da3c77
parent 31003 ed7364584aa7
child 31008 b8f4e351b5bf
     1.1 --- a/src/HOL/Code_Setup.thy	Wed Apr 22 11:00:25 2009 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,253 +0,0 @@
     1.4 -(*  Title:      HOL/Code_Setup.thy
     1.5 -    ID:         $Id$
     1.6 -    Author:     Florian Haftmann
     1.7 -*)
     1.8 -
     1.9 -header {* Setup of code generators and related tools *}
    1.10 -
    1.11 -theory Code_Setup
    1.12 -imports HOL
    1.13 -begin
    1.14 -
    1.15 -subsection {* Generic code generator foundation *}
    1.16 -
    1.17 -text {* Datatypes *}
    1.18 -
    1.19 -code_datatype True False
    1.20 -
    1.21 -code_datatype "TYPE('a\<Colon>{})"
    1.22 -
    1.23 -code_datatype Trueprop "prop"
    1.24 -
    1.25 -text {* Code equations *}
    1.26 -
    1.27 -lemma [code]:
    1.28 -  shows "(True \<Longrightarrow> PROP P) \<equiv> PROP P" 
    1.29 -    and "(False \<Longrightarrow> Q) \<equiv> Trueprop True" 
    1.30 -    and "(PROP P \<Longrightarrow> True) \<equiv> Trueprop True" 
    1.31 -    and "(Q \<Longrightarrow> False) \<equiv> Trueprop (\<not> Q)" by (auto intro!: equal_intr_rule)
    1.32 -
    1.33 -lemma [code]:
    1.34 -  shows "False \<and> x \<longleftrightarrow> False"
    1.35 -    and "True \<and> x \<longleftrightarrow> x"
    1.36 -    and "x \<and> False \<longleftrightarrow> False"
    1.37 -    and "x \<and> True \<longleftrightarrow> x" by simp_all
    1.38 -
    1.39 -lemma [code]:
    1.40 -  shows "False \<or> x \<longleftrightarrow> x"
    1.41 -    and "True \<or> x \<longleftrightarrow> True"
    1.42 -    and "x \<or> False \<longleftrightarrow> x"
    1.43 -    and "x \<or> True \<longleftrightarrow> True" by simp_all
    1.44 -
    1.45 -lemma [code]:
    1.46 -  shows "\<not> True \<longleftrightarrow> False"
    1.47 -    and "\<not> False \<longleftrightarrow> True" by (rule HOL.simp_thms)+
    1.48 -
    1.49 -lemmas [code] = Let_def if_True if_False
    1.50 -
    1.51 -lemmas [code, code unfold, symmetric, code post] = imp_conv_disj
    1.52 -
    1.53 -text {* Equality *}
    1.54 -
    1.55 -context eq
    1.56 -begin
    1.57 -
    1.58 -lemma equals_eq [code inline, code]: "op = \<equiv> eq"
    1.59 -  by (rule eq_reflection) (rule ext, rule ext, rule sym, rule eq_equals)
    1.60 -
    1.61 -declare eq [code unfold, code inline del]
    1.62 -
    1.63 -declare equals_eq [symmetric, code post]
    1.64 -
    1.65 -end
    1.66 -
    1.67 -declare simp_thms(6) [code nbe]
    1.68 -
    1.69 -hide (open) const eq
    1.70 -hide const eq
    1.71 -
    1.72 -setup {*
    1.73 -  Code_Unit.add_const_alias @{thm equals_eq}
    1.74 -*}
    1.75 -
    1.76 -text {* Cases *}
    1.77 -
    1.78 -lemma Let_case_cert:
    1.79 -  assumes "CASE \<equiv> (\<lambda>x. Let x f)"
    1.80 -  shows "CASE x \<equiv> f x"
    1.81 -  using assms by simp_all
    1.82 -
    1.83 -lemma If_case_cert:
    1.84 -  assumes "CASE \<equiv> (\<lambda>b. If b f g)"
    1.85 -  shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
    1.86 -  using assms by simp_all
    1.87 -
    1.88 -setup {*
    1.89 -  Code.add_case @{thm Let_case_cert}
    1.90 -  #> Code.add_case @{thm If_case_cert}
    1.91 -  #> Code.add_undefined @{const_name undefined}
    1.92 -*}
    1.93 -
    1.94 -code_abort undefined
    1.95 -
    1.96 -
    1.97 -subsection {* Generic code generator preprocessor *}
    1.98 -
    1.99 -setup {*
   1.100 -  Code.map_pre (K HOL_basic_ss)
   1.101 -  #> Code.map_post (K HOL_basic_ss)
   1.102 -*}
   1.103 -
   1.104 -
   1.105 -subsection {* Generic code generator target languages *}
   1.106 -
   1.107 -text {* type bool *}
   1.108 -
   1.109 -code_type bool
   1.110 -  (SML "bool")
   1.111 -  (OCaml "bool")
   1.112 -  (Haskell "Bool")
   1.113 -
   1.114 -code_const True and False and Not and "op &" and "op |" and If
   1.115 -  (SML "true" and "false" and "not"
   1.116 -    and infixl 1 "andalso" and infixl 0 "orelse"
   1.117 -    and "!(if (_)/ then (_)/ else (_))")
   1.118 -  (OCaml "true" and "false" and "not"
   1.119 -    and infixl 4 "&&" and infixl 2 "||"
   1.120 -    and "!(if (_)/ then (_)/ else (_))")
   1.121 -  (Haskell "True" and "False" and "not"
   1.122 -    and infixl 3 "&&" and infixl 2 "||"
   1.123 -    and "!(if (_)/ then (_)/ else (_))")
   1.124 -
   1.125 -code_reserved SML
   1.126 -  bool true false not
   1.127 -
   1.128 -code_reserved OCaml
   1.129 -  bool not
   1.130 -
   1.131 -text {* using built-in Haskell equality *}
   1.132 -
   1.133 -code_class eq
   1.134 -  (Haskell "Eq")
   1.135 -
   1.136 -code_const "eq_class.eq"
   1.137 -  (Haskell infixl 4 "==")
   1.138 -
   1.139 -code_const "op ="
   1.140 -  (Haskell infixl 4 "==")
   1.141 -
   1.142 -text {* undefined *}
   1.143 -
   1.144 -code_const undefined
   1.145 -  (SML "!(raise/ Fail/ \"undefined\")")
   1.146 -  (OCaml "failwith/ \"undefined\"")
   1.147 -  (Haskell "error/ \"undefined\"")
   1.148 -
   1.149 -
   1.150 -subsection {* SML code generator setup *}
   1.151 -
   1.152 -types_code
   1.153 -  "bool"  ("bool")
   1.154 -attach (term_of) {*
   1.155 -fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
   1.156 -*}
   1.157 -attach (test) {*
   1.158 -fun gen_bool i =
   1.159 -  let val b = one_of [false, true]
   1.160 -  in (b, fn () => term_of_bool b) end;
   1.161 -*}
   1.162 -  "prop"  ("bool")
   1.163 -attach (term_of) {*
   1.164 -fun term_of_prop b =
   1.165 -  HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
   1.166 -*}
   1.167 -
   1.168 -consts_code
   1.169 -  "Trueprop" ("(_)")
   1.170 -  "True"    ("true")
   1.171 -  "False"   ("false")
   1.172 -  "Not"     ("Bool.not")
   1.173 -  "op |"    ("(_ orelse/ _)")
   1.174 -  "op &"    ("(_ andalso/ _)")
   1.175 -  "If"      ("(if _/ then _/ else _)")
   1.176 -
   1.177 -setup {*
   1.178 -let
   1.179 -
   1.180 -fun eq_codegen thy defs dep thyname b t gr =
   1.181 -    (case strip_comb t of
   1.182 -       (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
   1.183 -     | (Const ("op =", _), [t, u]) =>
   1.184 -          let
   1.185 -            val (pt, gr') = Codegen.invoke_codegen thy defs dep thyname false t gr;
   1.186 -            val (pu, gr'') = Codegen.invoke_codegen thy defs dep thyname false u gr';
   1.187 -            val (_, gr''') = Codegen.invoke_tycodegen thy defs dep thyname false HOLogic.boolT gr'';
   1.188 -          in
   1.189 -            SOME (Codegen.parens
   1.190 -              (Pretty.block [pt, Codegen.str " =", Pretty.brk 1, pu]), gr''')
   1.191 -          end
   1.192 -     | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
   1.193 -         thy defs dep thyname b (Codegen.eta_expand t ts 2) gr)
   1.194 -     | _ => NONE);
   1.195 -
   1.196 -in
   1.197 -  Codegen.add_codegen "eq_codegen" eq_codegen
   1.198 -end
   1.199 -*}
   1.200 -
   1.201 -
   1.202 -subsection {* Evaluation and normalization by evaluation *}
   1.203 -
   1.204 -setup {*
   1.205 -  Value.add_evaluator ("SML", Codegen.eval_term o ProofContext.theory_of)
   1.206 -*}
   1.207 -
   1.208 -ML {*
   1.209 -structure Eval_Method =
   1.210 -struct
   1.211 -
   1.212 -val eval_ref : (unit -> bool) option ref = ref NONE;
   1.213 -
   1.214 -end;
   1.215 -*}
   1.216 -
   1.217 -oracle eval_oracle = {* fn ct =>
   1.218 -  let
   1.219 -    val thy = Thm.theory_of_cterm ct;
   1.220 -    val t = Thm.term_of ct;
   1.221 -    val dummy = @{cprop True};
   1.222 -  in case try HOLogic.dest_Trueprop t
   1.223 -   of SOME t' => if Code_ML.eval_term
   1.224 -         ("Eval_Method.eval_ref", Eval_Method.eval_ref) thy t' [] 
   1.225 -       then Thm.capply (Thm.capply @{cterm "op \<equiv> \<Colon> prop \<Rightarrow> prop \<Rightarrow> prop"} ct) dummy
   1.226 -       else dummy
   1.227 -    | NONE => dummy
   1.228 -  end
   1.229 -*}
   1.230 -
   1.231 -ML {*
   1.232 -fun gen_eval_method conv ctxt = SIMPLE_METHOD'
   1.233 -  (CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
   1.234 -    THEN' rtac TrueI)
   1.235 -*}
   1.236 -
   1.237 -method_setup eval = {* Scan.succeed (gen_eval_method eval_oracle) *}
   1.238 -  "solve goal by evaluation"
   1.239 -
   1.240 -method_setup evaluation = {* Scan.succeed (gen_eval_method Codegen.evaluation_conv) *}
   1.241 -  "solve goal by evaluation"
   1.242 -
   1.243 -method_setup normalization = {*
   1.244 -  Scan.succeed (K (SIMPLE_METHOD' (CONVERSION Nbe.norm_conv THEN' (fn k => TRY (rtac TrueI k)))))
   1.245 -*} "solve goal by normalization"
   1.246 -
   1.247 -
   1.248 -subsection {* Quickcheck *}
   1.249 -
   1.250 -setup {*
   1.251 -  Quickcheck.add_generator ("SML", Codegen.test_term)
   1.252 -*}
   1.253 -
   1.254 -quickcheck_params [size = 5, iterations = 50]
   1.255 -
   1.256 -end