src/HOL/Code_Eval.thy
changeset 28243 84d90ec67059
parent 28228 7ebe8dc06cbb
child 28313 1742947952f8
equal deleted inserted replaced
28242:f978c8e75118 28243:84d90ec67059
    59     let
    59     let
    60       val lhs = Const (@{const_name term_of}, ty --> @{typ term})
    60       val lhs = Const (@{const_name term_of}, ty --> @{typ term})
    61         $ Free ("x", ty);
    61         $ Free ("x", ty);
    62       val rhs = @{term "undefined \<Colon> term"};
    62       val rhs = @{term "undefined \<Colon> term"};
    63       val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
    63       val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
       
    64       fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
       
    65         o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
    64     in
    66     in
    65       thy
    67       thy
    66       |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
    68       |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
    67       |> `(fn lthy => Syntax.check_term lthy eq)
    69       |> `(fn lthy => Syntax.check_term lthy eq)
    68       |-> (fn eq => Specification.definition (NONE, (Attrib.no_binding, eq)))
    70       |-> (fn eq => Specification.definition (NONE, ((Name.binding (triv_name_of eq), []), eq)))
    69       |> snd
    71       |> snd
    70       |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
    72       |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
    71       |> LocalTheory.exit
    73       |> LocalTheory.exit
    72       |> ProofContext.theory_of
    74       |> ProofContext.theory_of
    73     end;
    75     end;
   137 
   139 
   138 lemma [code func, code func del]: "(term_of \<Colon> rtype \<Rightarrow> term) = term_of" ..
   140 lemma [code func, code func del]: "(term_of \<Colon> rtype \<Rightarrow> term) = term_of" ..
   139 lemma [code func, code func del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
   141 lemma [code func, code func del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
   140 lemma [code func, code func del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" ..
   142 lemma [code func, code func del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" ..
   141 
   143 
       
   144 lemma term_of_char [unfolded rtype_fun_def rtype_char_def rtype_nibble_def, code func]: "Code_Eval.term_of c =
       
   145     (let (n, m) = nibble_pair_of_char c
       
   146   in Code_Eval.App (Code_Eval.App (Code_Eval.Const (STR ''Pair'') (RTYPE(nibble \<Rightarrow> nibble \<Rightarrow> char)))
       
   147     (Code_Eval.term_of n)) (Code_Eval.term_of m))"
       
   148   by (subst term_of_anything) rule 
       
   149 
   142 code_type "term"
   150 code_type "term"
   143   (SML "Term.term")
   151   (SML "Term.term")
   144 
   152 
   145 code_const Const and App
   153 code_const Const and App
   146   (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)")
   154   (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)")
   147 
   155 
   148 code_const "term_of \<Colon> message_string \<Rightarrow> term"
   156 code_const "term_of \<Colon> message_string \<Rightarrow> term"
   149   (SML "Message'_String.mk")
   157   (SML "Message'_String.mk")
       
   158 
       
   159 
       
   160 subsection {* Evaluation setup *}
       
   161 
       
   162 ML {*
       
   163 signature EVAL =
       
   164 sig
       
   165   val mk_term: ((string * typ) -> term) -> (typ -> term) -> term -> term
       
   166   val eval_ref: (unit -> term) option ref
       
   167   val eval_term: theory -> term -> term
       
   168 end;
       
   169 
       
   170 structure Eval : EVAL =
       
   171 struct
       
   172 
       
   173 open Eval;
       
   174 
       
   175 val eval_ref = ref (NONE : (unit -> term) option);
       
   176 
       
   177 fun eval_term thy t =
       
   178   t 
       
   179   |> Eval.mk_term_of (fastype_of t)
       
   180   |> (fn t => Code_ML.eval_term ("Eval.eval_ref", eval_ref) thy t [])
       
   181   |> Code.postprocess_term thy;
       
   182 
       
   183 end;
       
   184 *}
       
   185 
       
   186 setup {*
       
   187   Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of)
       
   188 *}
   150 
   189 
   151 
   190 
   152 subsubsection {* Syntax *}
   191 subsubsection {* Syntax *}
   153 
   192 
   154 print_translation {*
   193 print_translation {*
   160   [(@{const_syntax Const}, tr1'),
   199   [(@{const_syntax Const}, tr1'),
   161     (@{const_syntax App}, tr1'),
   200     (@{const_syntax App}, tr1'),
   162     (@{const_syntax dummy_term}, tr2')]
   201     (@{const_syntax dummy_term}, tr2')]
   163 end
   202 end
   164 *}
   203 *}
   165 setup {*
       
   166   Sign.declare_const [] ((Name.binding "rterm_of", @{typ "'a \<Rightarrow> 'b"}), NoSyn)
       
   167   #> snd
       
   168 *}
       
   169 
       
   170 notation (output)
       
   171   rterm_of ("\<guillemotleft>_\<guillemotright>")
       
   172 
       
   173 locale rterm_syntax =
       
   174   fixes rterm_of_syntax :: "'a \<Rightarrow> 'b" ("\<guillemotleft>_\<guillemotright>")
       
   175 
       
   176 parse_translation {*
       
   177 let
       
   178   fun rterm_of_tr [t] = Lexicon.const @{const_name rterm_of} $ t
       
   179     | rterm_of_tr ts = raise TERM ("rterm_of_tr", ts);
       
   180 in
       
   181   [(Syntax.fixedN ^ "rterm_of_syntax", rterm_of_tr)]
       
   182 end
       
   183 *}
       
   184 
       
   185 setup {*
       
   186 let
       
   187   val subst_rterm_of = Eval.mk_term
       
   188     (fn (v, _) => error ("illegal free variable in term quotation: " ^ quote v))
       
   189     (RType.mk (fn (v, sort) => RType.rtype (TFree (v, sort))));
       
   190   fun subst_rterm_of' (Const (@{const_name rterm_of}, _), [t]) = subst_rterm_of t
       
   191     | subst_rterm_of' (Const (@{const_name rterm_of}, _), _) =
       
   192         error ("illegal number of arguments for " ^ quote @{const_name rterm_of})
       
   193     | subst_rterm_of' (t, ts) = list_comb (t, map (subst_rterm_of' o strip_comb) ts);
       
   194   fun subst_rterm_of'' t = 
       
   195     let
       
   196       val t' = subst_rterm_of' (strip_comb t);
       
   197     in if t aconv t'
       
   198       then NONE
       
   199       else SOME t'
       
   200     end;
       
   201   fun check_rterm_of ts ctxt =
       
   202     let
       
   203       val ts' = map subst_rterm_of'' ts;
       
   204     in if exists is_some ts'
       
   205       then SOME (map2 the_default ts ts', ctxt)
       
   206       else NONE
       
   207     end;
       
   208 in
       
   209   Context.theory_map (Syntax.add_term_check 0 "rterm_of" check_rterm_of)
       
   210 end;
       
   211 *}
       
   212 
   204 
   213 hide const dummy_term
   205 hide const dummy_term
   214 hide (open) const Const App
   206 hide (open) const Const App
   215 hide (open) const term_of
   207 hide (open) const term_of
   216 
   208 
   217 
   209 end
   218 subsection {* Evaluation setup *}
       
   219 
       
   220 ML {*
       
   221 signature EVAL =
       
   222 sig
       
   223   val mk_term: ((string * typ) -> term) -> (typ -> term) -> term -> term
       
   224   val eval_ref: (unit -> term) option ref
       
   225   val eval_term: theory -> term -> term
       
   226 end;
       
   227 
       
   228 structure Eval : EVAL =
       
   229 struct
       
   230 
       
   231 open Eval;
       
   232 
       
   233 val eval_ref = ref (NONE : (unit -> term) option);
       
   234 
       
   235 fun eval_term thy t =
       
   236   t 
       
   237   |> Eval.mk_term_of (fastype_of t)
       
   238   |> (fn t => Code_ML.eval_term ("Eval.eval_ref", eval_ref) thy t [])
       
   239   |> Code.postprocess_term thy;
       
   240 
       
   241 end;
       
   242 *}
       
   243 
       
   244 setup {*
       
   245   Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of)
       
   246 *}
       
   247 
       
   248 end