src/HOL/Code_Evaluation.thy
author haftmann
Thu Sep 16 16:51:33 2010 +0200 (2010-09-16)
changeset 39471 55e0ff582fa4
parent 39403 aad9f3cfa1d9
child 39564 acfd10e38e80
permissions -rw-r--r--
adjusted to changes in Code_Runtime
     1 (*  Title:      HOL/Code_Evaluation.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Term evaluation using the generic code generator *}
     6 
     7 theory Code_Evaluation
     8 imports Plain Typerep Code_Numeral
     9 begin
    10 
    11 subsection {* Term representation *}
    12 
    13 subsubsection {* Terms and class @{text term_of} *}
    14 
    15 datatype "term" = dummy_term
    16 
    17 definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
    18   "Const _ _ = dummy_term"
    19 
    20 definition App :: "term \<Rightarrow> term \<Rightarrow> term" where
    21   "App _ _ = dummy_term"
    22 
    23 code_datatype Const App
    24 
    25 class term_of = typerep +
    26   fixes term_of :: "'a \<Rightarrow> term"
    27 
    28 lemma term_of_anything: "term_of x \<equiv> t"
    29   by (rule eq_reflection) (cases "term_of x", cases t, simp)
    30 
    31 definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
    32   \<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where
    33   "valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"
    34 
    35 lemma valapp_code [code, code_unfold]:
    36   "valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"
    37   by (simp only: valapp_def fst_conv snd_conv)
    38 
    39 
    40 subsubsection {* @{text term_of} instances *}
    41 
    42 instantiation "fun" :: (typerep, typerep) term_of
    43 begin
    44 
    45 definition
    46   "term_of (f \<Colon> 'a \<Rightarrow> 'b) = Const (STR ''dummy_pattern'') (Typerep.Typerep (STR ''fun'')
    47      [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
    48 
    49 instance ..
    50 
    51 end
    52 
    53 setup {*
    54 let
    55   fun add_term_of tyco raw_vs thy =
    56     let
    57       val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
    58       val ty = Type (tyco, map TFree vs);
    59       val lhs = Const (@{const_name term_of}, ty --> @{typ term})
    60         $ Free ("x", ty);
    61       val rhs = @{term "undefined \<Colon> term"};
    62       val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
    63       fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
    64         o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
    65     in
    66       thy
    67       |> Class.instantiation ([tyco], vs, @{sort term_of})
    68       |> `(fn lthy => Syntax.check_term lthy eq)
    69       |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
    70       |> snd
    71       |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
    72     end;
    73   fun ensure_term_of (tyco, (raw_vs, _)) thy =
    74     let
    75       val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of})
    76         andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
    77     in if need_inst then add_term_of tyco raw_vs thy else thy end;
    78 in
    79   Code.datatype_interpretation ensure_term_of
    80   #> Code.abstype_interpretation ensure_term_of
    81 end
    82 *}
    83 
    84 setup {*
    85 let
    86   fun mk_term_of_eq thy ty vs tyco (c, tys) =
    87     let
    88       val t = list_comb (Const (c, tys ---> ty),
    89         map Free (Name.names Name.context "a" tys));
    90       val (arg, rhs) =
    91         pairself (Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
    92           (t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t)
    93       val cty = Thm.ctyp_of thy ty;
    94     in
    95       @{thm term_of_anything}
    96       |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
    97       |> Thm.varifyT_global
    98     end;
    99   fun add_term_of_code tyco raw_vs raw_cs thy =
   100     let
   101       val algebra = Sign.classes_of thy;
   102       val vs = map (fn (v, sort) =>
   103         (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
   104       val ty = Type (tyco, map TFree vs);
   105       val cs = (map o apsnd o map o map_atyps)
   106         (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
   107       val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
   108       val eqs = map (mk_term_of_eq thy ty vs tyco) cs;
   109    in
   110       thy
   111       |> Code.del_eqns const
   112       |> fold Code.add_eqn eqs
   113     end;
   114   fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
   115     let
   116       val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
   117     in if has_inst then add_term_of_code tyco raw_vs cs thy else thy end;
   118 in
   119   Code.datatype_interpretation ensure_term_of_code
   120 end
   121 *}
   122 
   123 setup {*
   124 let
   125   fun mk_term_of_eq thy ty vs tyco abs ty_rep proj =
   126     let
   127       val arg = Var (("x", 0), ty);
   128       val rhs = Abs ("y", @{typ term}, HOLogic.reflect_term (Const (abs, ty_rep --> ty) $ Bound 0)) $
   129         (HOLogic.mk_term_of ty_rep (Const (proj, ty --> ty_rep) $ arg))
   130         |> Thm.cterm_of thy;
   131       val cty = Thm.ctyp_of thy ty;
   132     in
   133       @{thm term_of_anything}
   134       |> Drule.instantiate' [SOME cty] [SOME (Thm.cterm_of thy arg), SOME rhs]
   135       |> Thm.varifyT_global
   136     end;
   137   fun add_term_of_code tyco raw_vs abs raw_ty_rep proj thy =
   138     let
   139       val algebra = Sign.classes_of thy;
   140       val vs = map (fn (v, sort) =>
   141         (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
   142       val ty = Type (tyco, map TFree vs);
   143       val ty_rep = map_atyps
   144         (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_ty_rep;
   145       val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
   146       val eq = mk_term_of_eq thy ty vs tyco abs ty_rep proj;
   147    in
   148       thy
   149       |> Code.del_eqns const
   150       |> Code.add_eqn eq
   151     end;
   152   fun ensure_term_of_code (tyco, (raw_vs, ((abs, ty), (proj, _)))) thy =
   153     let
   154       val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
   155     in if has_inst then add_term_of_code tyco raw_vs abs ty proj thy else thy end;
   156 in
   157   Code.abstype_interpretation ensure_term_of_code
   158 end
   159 *}
   160 
   161 
   162 instantiation String.literal :: term_of
   163 begin
   164 
   165 definition
   166   "term_of s = App (Const (STR ''STR'')
   167     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
   168       Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
   169 
   170 instance ..
   171 
   172 end
   173 
   174 subsubsection {* Code generator setup *}
   175 
   176 lemmas [code del] = term.recs term.cases term.size
   177 lemma [code, code del]: "HOL.equal (t1\<Colon>term) t2 \<longleftrightarrow> HOL.equal t1 t2" ..
   178 
   179 lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
   180 lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
   181 lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..
   182 lemma [code, code del]:
   183   "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term) = Code_Evaluation.term_of" ..
   184 lemma [code, code del]:
   185   "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term) = Code_Evaluation.term_of" ..
   186 
   187 lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]: "Code_Evaluation.term_of c =
   188     (let (n, m) = nibble_pair_of_char c
   189   in Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
   190     (Code_Evaluation.term_of n)) (Code_Evaluation.term_of m))"
   191   by (subst term_of_anything) rule 
   192 
   193 code_type "term"
   194   (Eval "Term.term")
   195 
   196 code_const Const and App
   197   (Eval "Term.Const/ ((_), (_))" and "Term.$/ ((_), (_))")
   198 
   199 code_const "term_of \<Colon> String.literal \<Rightarrow> term"
   200   (Eval "HOLogic.mk'_literal")
   201 
   202 code_reserved Eval HOLogic
   203 
   204 
   205 subsubsection {* Syntax *}
   206 
   207 definition termify :: "'a \<Rightarrow> term" where
   208   [code del]: "termify x = dummy_term"
   209 
   210 abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
   211   "valtermify x \<equiv> (x, \<lambda>u. termify x)"
   212 
   213 setup {*
   214 let
   215   fun map_default f xs =
   216     let val ys = map f xs
   217     in if exists is_some ys
   218       then SOME (map2 the_default xs ys)
   219       else NONE
   220     end;
   221   fun subst_termify_app (Const (@{const_name termify}, T), [t]) =
   222         if not (Term.has_abs t)
   223         then if fold_aterms (fn Const _ => I | _ => K false) t true
   224           then SOME (HOLogic.reflect_term t)
   225           else error "Cannot termify expression containing variables"
   226         else error "Cannot termify expression containing abstraction"
   227     | subst_termify_app (t, ts) = case map_default subst_termify ts
   228        of SOME ts' => SOME (list_comb (t, ts'))
   229         | NONE => NONE
   230   and subst_termify (Abs (v, T, t)) = (case subst_termify t
   231        of SOME t' => SOME (Abs (v, T, t'))
   232         | NONE => NONE)
   233     | subst_termify t = subst_termify_app (strip_comb t) 
   234   fun check_termify ts ctxt = map_default subst_termify ts
   235     |> Option.map (rpair ctxt)
   236 in
   237   Context.theory_map (Syntax.add_term_check 0 "termify" check_termify)
   238 end;
   239 *}
   240 
   241 locale term_syntax
   242 begin
   243 
   244 notation App (infixl "<\<cdot>>" 70)
   245   and valapp (infixl "{\<cdot>}" 70)
   246 
   247 end
   248 
   249 interpretation term_syntax .
   250 
   251 no_notation App (infixl "<\<cdot>>" 70)
   252   and valapp (infixl "{\<cdot>}" 70)
   253 
   254 
   255 subsection {* Numeric types *}
   256 
   257 definition term_of_num :: "'a\<Colon>{semiring_div} \<Rightarrow> 'a\<Colon>{semiring_div} \<Rightarrow> term" where
   258   "term_of_num two = (\<lambda>_. dummy_term)"
   259 
   260 lemma (in term_syntax) term_of_num_code [code]:
   261   "term_of_num two k = (if k = 0 then termify Int.Pls
   262     else (if k mod two = 0
   263       then termify Int.Bit0 <\<cdot>> term_of_num two (k div two)
   264       else termify Int.Bit1 <\<cdot>> term_of_num two (k div two)))"
   265   by (auto simp add: term_of_anything Const_def App_def term_of_num_def Let_def)
   266 
   267 lemma (in term_syntax) term_of_nat_code [code]:
   268   "term_of (n::nat) = termify (number_of :: int \<Rightarrow> nat) <\<cdot>> term_of_num (2::nat) n"
   269   by (simp only: term_of_anything)
   270 
   271 lemma (in term_syntax) term_of_int_code [code]:
   272   "term_of (k::int) = (if k = 0 then termify (0 :: int)
   273     else if k > 0 then termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) k
   274       else termify (uminus :: int \<Rightarrow> int) <\<cdot>> (termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) (- k)))"
   275   by (simp only: term_of_anything)
   276 
   277 lemma (in term_syntax) term_of_code_numeral_code [code]:
   278   "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num (2::code_numeral) k"
   279   by (simp only: term_of_anything)
   280 
   281 
   282 subsection {* Obfuscate *}
   283 
   284 print_translation {*
   285 let
   286   val term = Const ("<TERM>", dummyT);
   287   fun tr1' [_, _] = term;
   288   fun tr2' [] = term;
   289 in
   290   [(@{const_syntax Const}, tr1'),
   291     (@{const_syntax App}, tr1'),
   292     (@{const_syntax dummy_term}, tr2')]
   293 end
   294 *}
   295 
   296 
   297 subsection {* Evaluation setup *}
   298 
   299 ML {*
   300 signature CODE_EVALUATION =
   301 sig
   302   val eval_term: theory -> term -> term
   303   val put_term: (unit -> term) -> Proof.context -> Proof.context
   304   val tracing: string -> 'a -> 'a
   305 end;
   306 
   307 structure Code_Evaluation : CODE_EVALUATION =
   308 struct
   309 
   310 structure Evaluation = Proof_Data (
   311   type T = unit -> term
   312   fun init _ () = error "Evaluation"
   313 );
   314 val put_term = Evaluation.put;
   315 
   316 fun tracing s x = (Output.tracing s; x);
   317 
   318 fun eval_term thy t = Code_Runtime.dynamic_value_strict (Evaluation.get, put_term, "Code_Evaluation.put_term")
   319   thy NONE I (HOLogic.mk_term_of (fastype_of t) t) [];
   320 
   321 end
   322 *}
   323 
   324 setup {*
   325   Value.add_evaluator ("code", Code_Evaluation.eval_term o ProofContext.theory_of)
   326 *}
   327 
   328 definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
   329   [code del]: "tracing s x = x"
   330 
   331 code_const "tracing :: String.literal => 'a => 'a"
   332   (Eval "Code'_Evaluation.tracing")
   333 
   334 code_reserved Eval Code_Evaluation
   335 
   336 hide_const dummy_term App valapp
   337 hide_const (open) Const termify valtermify term_of term_of_num tracing
   338 
   339 end