src/HOL/Code_Generator.thy
changeset 21110 fc98cb66c5c3
parent 21079 747d716e98d0
child 21149 ee207b9b8bf5
equal deleted inserted replaced
21109:f8f89be75e81 21110:fc98cb66c5c3
   102 setup {*
   102 setup {*
   103   CodegenSerializer.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")"
   103   CodegenSerializer.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")"
   104 *}
   104 *}
   105 
   105 
   106 code_const arbitrary
   106 code_const arbitrary
   107   (Haskell target_atom "(error \"arbitrary\")")
   107   (Haskell "error/ \"arbitrary\"")
   108 
   108 
   109 code_reserved SML Fail
   109 code_reserved SML Fail
   110 code_reserved Haskell error
   110 code_reserved Haskell error
   111 
   111 
   112 subsection {* Operational equality for code generation *}
       
   113 
   112 
   114 subsubsection {* eq class *}
   113 subsection {* normalization by evaluation *}
   115 
       
   116 class eq =
       
   117   fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
       
   118 
       
   119 defs
       
   120   eq_def [normal post]: "eq \<equiv> (op =)"
       
   121 
       
   122 lemmas [symmetric, code inline] = eq_def
       
   123 
       
   124 
       
   125 subsubsection {* bool type *}
       
   126 
       
   127 instance bool :: eq ..
       
   128 
       
   129 lemma [code func]:
       
   130   "eq True p = p" unfolding eq_def by auto
       
   131 
       
   132 lemma [code func]:
       
   133   "eq False p = (\<not> p)" unfolding eq_def by auto
       
   134 
       
   135 lemma [code func]:
       
   136   "eq p True = p" unfolding eq_def by auto
       
   137 
       
   138 lemma [code func]:
       
   139   "eq p False = (\<not> p)" unfolding eq_def by auto
       
   140 
       
   141 
       
   142 subsubsection {* preprocessors *}
       
   143 
   114 
   144 setup {*
   115 setup {*
   145 let
   116 let
   146   fun constrain_op_eq thy ts =
       
   147     let
       
   148       fun add_eq (Const ("op =", ty)) =
       
   149             fold (insert (eq_fst (op = : indexname * indexname -> bool)))
       
   150               (Term.add_tvarsT ty [])
       
   151         | add_eq _ =
       
   152             I
       
   153       val eqs = (fold o fold_aterms) add_eq ts [];
       
   154       val inst = map (fn (v_i, _) => (v_i, [HOLogic.class_eq])) eqs;
       
   155     in inst end;
       
   156 in CodegenData.add_constrains constrain_op_eq end
       
   157 *}
       
   158 
       
   159 
       
   160 subsubsection {* Haskell *}
       
   161 
       
   162 code_class eq
       
   163   (Haskell "Eq" where eq \<equiv> "(==)")
       
   164 
       
   165 code_const eq
       
   166   (Haskell infixl 4 "==")
       
   167 
       
   168 code_instance bool :: eq
       
   169   (Haskell -)
       
   170 
       
   171 code_const "eq \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
       
   172   (Haskell infixl 4 "==")
       
   173 
       
   174 code_reserved Haskell
       
   175   Eq eq
       
   176 
       
   177 subsection {* normalization by evaluation *}
       
   178 
       
   179 lemma eq_refl: "eq x x"
       
   180   unfolding eq_def ..
       
   181 
       
   182 setup {*
       
   183 let
       
   184   val eq_refl = thm "eq_refl";
       
   185   fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
   117   fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
   186     (Drule.goals_conv (equal i) (HOL.Trueprop_conv NBE.normalization_conv)));
   118     (Drule.goals_conv (equal i) (HOL.Trueprop_conv
       
   119       (HOL.Equals_conv NBE.normalization_conv))));
   187   val normalization_meth =
   120   val normalization_meth =
   188     Method.no_args (Method.METHOD (fn _ => normalization_tac 1 THEN resolve_tac [TrueI, refl, eq_refl] 1));
   121     Method.no_args (Method.METHOD (fn _ => normalization_tac 1 THEN resolve_tac [TrueI, refl] 1));
   189 in
   122 in
   190   Method.add_method ("normalization", normalization_meth, "solve goal by normalization")
   123   Method.add_method ("normalization", normalization_meth, "solve goal by normalization")
   191 end;
   124 end;
   192 *}
   125 *}
   193 
   126 
   205 lemma [normal pre, symmetric, normal post]:
   138 lemma [normal pre, symmetric, normal post]:
   206   "(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)"
   139   "(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)"
   207   unfolding if_delayed_def ..
   140   unfolding if_delayed_def ..
   208 
   141 
   209 
   142 
       
   143 subsection {* Operational equality for code generation *}
       
   144 
       
   145 subsubsection {* eq class *}
       
   146 
       
   147 class eq =
       
   148   fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
       
   149 
       
   150 defs
       
   151   eq_def [normal post]: "eq \<equiv> (op =)"
       
   152 
       
   153 lemmas [symmetric, code inline, code func] = eq_def
       
   154 
       
   155 
       
   156 subsubsection {* bool type *}
       
   157 
       
   158 instance bool :: eq ..
       
   159 
       
   160 lemma [code func]:
       
   161   "eq True p = p" unfolding eq_def by auto
       
   162 
       
   163 lemma [code func]:
       
   164   "eq False p = (\<not> p)" unfolding eq_def by auto
       
   165 
       
   166 lemma [code func]:
       
   167   "eq p True = p" unfolding eq_def by auto
       
   168 
       
   169 lemma [code func]:
       
   170   "eq p False = (\<not> p)" unfolding eq_def by auto
       
   171 
       
   172 
       
   173 subsubsection {* Haskell *}
       
   174 
       
   175 code_class eq
       
   176   (Haskell "Eq" where eq \<equiv> "(==)")
       
   177 
       
   178 code_const eq
       
   179   (Haskell infixl 4 "==")
       
   180 
       
   181 code_instance bool :: eq
       
   182   (Haskell -)
       
   183 
       
   184 code_const "eq \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
       
   185   (Haskell infixl 4 "==")
       
   186 
       
   187 code_reserved Haskell
       
   188   Eq eq
       
   189 
       
   190 
   210 hide (open) const eq if_delayed
   191 hide (open) const eq if_delayed
   211 
   192 
   212 end
   193 end