
haftmann@32657: (*  Title:      HOL/Code_Evaluation.thy
haftmann@28228:     Author:     Florian Haftmann, TU Muenchen
haftmann@28228: *)
haftmann@28228: 
haftmann@28228: header {* Term evaluation using the generic code generator *}
haftmann@28228: 
haftmann@32657: theory Code_Evaluation
haftmann@51126: imports Typerep Limited_Sequence
haftmann@56925: keywords "value" :: diag
haftmann@28228: begin
haftmann@28228: 
haftmann@28228: subsection {* Term representation *}
haftmann@28228: 
haftmann@28228: subsubsection {* Terms and class @{text term_of} *}
haftmann@28228: 
blanchet@58152: datatype_new "term" = dummy_term
haftmann@28228: 
haftmann@31205: definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
haftmann@28228:   "Const _ _ = dummy_term"
haftmann@28228: 
haftmann@28661: definition App :: "term \<Rightarrow> term \<Rightarrow> term" where
haftmann@28228:   "App _ _ = dummy_term"
haftmann@28228: 
bulwahn@40638: definition Abs :: "String.literal \<Rightarrow> typerep \<Rightarrow> term \<Rightarrow> term" where
bulwahn@40638:   "Abs _ _ _ = dummy_term"
bulwahn@40638: 
bulwahn@42979: definition Free :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
bulwahn@42979:   "Free _ _ = dummy_term"
bulwahn@42979: 
bulwahn@42979: code_datatype Const App Abs Free
haftmann@28228: 
haftmann@28335: class term_of = typerep +
haftmann@31031:   fixes term_of :: "'a \<Rightarrow> term"
haftmann@28228: 
haftmann@28228: lemma term_of_anything: "term_of x \<equiv> t"
haftmann@28228:   by (rule eq_reflection) (cases "term_of x", cases t, simp)
haftmann@28228: 
haftmann@31191: definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
haftmann@31178:   \<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where
haftmann@31191:   "valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"
haftmann@31178: 
haftmann@32069: lemma valapp_code [code, code_unfold]:
haftmann@31191:   "valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"
haftmann@31191:   by (simp only: valapp_def fst_conv snd_conv)
haftmann@31178: 
haftmann@28228: 
haftmann@31178: subsubsection {* Syntax *}
haftmann@31178: 
haftmann@31191: definition termify :: "'a \<Rightarrow> term" where
haftmann@31191:   [code del]: "termify x = dummy_term"
haftmann@31191: 
haftmann@31191: abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
haftmann@31191:   "valtermify x \<equiv> (x, \<lambda>u. termify x)"
haftmann@31178: 
haftmann@31178: locale term_syntax
haftmann@31178: begin
haftmann@31178: 
haftmann@31191: notation App (infixl "<\<cdot>>" 70)
haftmann@31191:   and valapp (infixl "{\<cdot>}" 70)
haftmann@31178: 
haftmann@31178: end
haftmann@31178: 
haftmann@31178: interpretation term_syntax .
haftmann@31178: 
haftmann@31191: no_notation App (infixl "<\<cdot>>" 70)
haftmann@31191:   and valapp (infixl "{\<cdot>}" 70)
haftmann@31191: 
haftmann@31191: 
haftmann@39564: subsection {* Tools setup and evaluation *}
haftmann@39564: 
haftmann@39567: lemma eq_eq_TrueD:
haftmann@39567:   assumes "(x \<equiv> y) \<equiv> Trueprop True"
haftmann@39567:   shows "x \<equiv> y"
haftmann@39567:   using assms by simp
haftmann@39567: 
haftmann@56928: code_printing
haftmann@56928:   type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
haftmann@56928: | constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
haftmann@56928: | constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
haftmann@56928: | constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
haftmann@56928: | constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
haftmann@56928: 
wenzelm@48891: ML_file "Tools/code_evaluation.ML"
haftmann@39564: 
haftmann@39564: code_reserved Eval Code_Evaluation
haftmann@39564: 
haftmann@56928: ML_file "~~/src/HOL/Tools/value.ML"
haftmann@56928: 
haftmann@39564: 
haftmann@39564: subsection {* @{text term_of} instances *}
haftmann@39564: 
haftmann@39564: instantiation "fun" :: (typerep, typerep) term_of
haftmann@39564: begin
haftmann@39564: 
haftmann@39564: definition
wenzelm@56241:   "term_of (f \<Colon> 'a \<Rightarrow> 'b) =
wenzelm@56241:     Const (STR ''Pure.dummy_pattern'')
wenzelm@56241:       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
haftmann@39564: 
haftmann@39564: instance ..
haftmann@39564: 
haftmann@39564: end
haftmann@39564: 
haftmann@39564: instantiation String.literal :: term_of
haftmann@39564: begin
haftmann@39564: 
haftmann@39564: definition
haftmann@39564:   "term_of s = App (Const (STR ''STR'')
haftmann@39564:     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
haftmann@39564:       Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
haftmann@39564: 
haftmann@39564: instance ..
haftmann@39564: 
haftmann@39564: end
haftmann@39564: 
haftmann@56926: declare [[code drop: rec_term case_term size_term "size :: term \<Rightarrow> _" "HOL.equal :: term \<Rightarrow> _"
haftmann@56926:   "term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"
haftmann@56926:   "term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]
haftmann@39564: 
haftmann@39564: lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:
haftmann@51160:   "Code_Evaluation.term_of c = (case c of Char x y \<Rightarrow>
haftmann@51160:    Code_Evaluation.App (Code_Evaluation.App
haftmann@39564:     (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
haftmann@51160:       (Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"
haftmann@39564:   by (subst term_of_anything) rule 
haftmann@39564: 
haftmann@52435: code_printing
haftmann@56928:   constant "term_of \<Colon> integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
haftmann@52435: | constant "term_of \<Colon> String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
haftmann@39564: 
Andreas@58036: declare [[code drop: "term_of :: integer \<Rightarrow> _"]]
Andreas@58036: 
Andreas@58036: lemma term_of_integer [unfolded typerep_fun_def typerep_num_def typerep_integer_def, code]:
Andreas@58038:   "term_of (i :: integer) =
Andreas@58038:   (if i > 0 then 
Andreas@58038:      App (Const (STR ''Num.numeral_class.numeral'') (TYPEREP(num \<Rightarrow> integer)))
Andreas@58038:       (term_of (num_of_integer i))
Andreas@58038:    else if i = 0 then Const (STR ''Groups.zero_class.zero'') TYPEREP(integer)
Andreas@58038:    else
Andreas@58038:      App (Const (STR ''Groups.uminus_class.uminus'') TYPEREP(integer \<Rightarrow> integer))
Andreas@58038:        (term_of (- i)))"
Andreas@58036: by(rule term_of_anything[THEN meta_eq_to_obj_eq])
Andreas@58036: 
haftmann@39564: code_reserved Eval HOLogic
haftmann@39564: 
haftmann@39564: 
haftmann@56928: subsection {* Generic reification *}
haftmann@31178: 
haftmann@56928: ML_file "~~/src/HOL/Tools/reification.ML"
haftmann@31178: 
haftmann@31178: 
haftmann@39564: subsection {* Diagnostic *}
haftmann@39387: 
haftmann@39387: definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@39387:   [code del]: "tracing s x = x"
haftmann@39387: 
haftmann@52435: code_printing
haftmann@52435:   constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
bulwahn@33473: 
haftmann@31178: 
bulwahn@40638: hide_const dummy_term valapp
haftmann@51144: hide_const (open) Const App Abs Free termify valtermify term_of tracing
haftmann@28228: 
haftmann@28228: end
haftmann@51143: 




