src/HOL/Tools/code_evaluation.ML
changeset 56973 62da80041afd
parent 56926 aaea99edc040
child 59058 a78612c67ec0
equal deleted inserted replaced
56972:f64730f667b9 56973:62da80041afd
     7 signature CODE_EVALUATION =
     7 signature CODE_EVALUATION =
     8 sig
     8 sig
     9   val dynamic_value: Proof.context -> term -> term option
     9   val dynamic_value: Proof.context -> term -> term option
    10   val dynamic_value_strict: Proof.context -> term -> term
    10   val dynamic_value_strict: Proof.context -> term -> term
    11   val dynamic_value_exn: Proof.context -> term -> term Exn.result
    11   val dynamic_value_exn: Proof.context -> term -> term Exn.result
    12   val static_value: Proof.context -> string list -> typ list -> Proof.context -> term -> term option
    12   val static_value: { ctxt: Proof.context, consts: string list, Ts: typ list }
    13   val static_value_strict: Proof.context -> string list -> typ list -> Proof.context -> term -> term
    13     -> Proof.context -> term -> term option
    14   val static_value_exn: Proof.context -> string list -> typ list -> Proof.context -> term -> term Exn.result
    14   val static_value_strict: { ctxt: Proof.context, consts: string list, Ts: typ list }
       
    15     -> Proof.context -> term -> term
       
    16   val static_value_exn: { ctxt: Proof.context, consts: string list, Ts: typ list }
       
    17     -> Proof.context -> term -> term Exn.result
    15   val dynamic_conv: Proof.context -> conv
    18   val dynamic_conv: Proof.context -> conv
    16   val static_conv: Proof.context -> string list -> typ list -> Proof.context -> conv
    19   val static_conv: { ctxt: Proof.context, consts: string list, Ts: typ list }
       
    20     -> Proof.context -> conv
    17   val put_term: (unit -> term) -> Proof.context -> Proof.context
    21   val put_term: (unit -> term) -> Proof.context -> Proof.context
    18   val tracing: string -> 'a -> 'a
    22   val tracing: string -> 'a -> 'a
    19 end;
    23 end;
    20 
    24 
    21 structure Code_Evaluation : CODE_EVALUATION =
    25 structure Code_Evaluation : CODE_EVALUATION =
   158 and subst_termify (Abs (v, T, t)) = (case subst_termify t
   162 and subst_termify (Abs (v, T, t)) = (case subst_termify t
   159      of SOME t' => SOME (Abs (v, T, t'))
   163      of SOME t' => SOME (Abs (v, T, t'))
   160       | NONE => NONE)
   164       | NONE => NONE)
   161   | subst_termify t = subst_termify_app (strip_comb t) 
   165   | subst_termify t = subst_termify_app (strip_comb t) 
   162 
   166 
   163 fun check_termify ctxt ts =
   167 fun check_termify _ ts = the_default ts (map_default subst_termify ts);
   164   the_default ts (map_default subst_termify ts);
       
   165 
   168 
   166 val _ = Context.>> (Syntax_Phases.term_check 0 "termify" check_termify);
   169 val _ = Context.>> (Syntax_Phases.term_check 0 "termify" check_termify);
   167 
   170 
   168 
   171 
   169 (** evaluation **)
   172 (** evaluation **)
   186 
   189 
   187 val dynamic_value = gen_dynamic_value Code_Runtime.dynamic_value;
   190 val dynamic_value = gen_dynamic_value Code_Runtime.dynamic_value;
   188 val dynamic_value_strict = gen_dynamic_value Code_Runtime.dynamic_value_strict;
   191 val dynamic_value_strict = gen_dynamic_value Code_Runtime.dynamic_value_strict;
   189 val dynamic_value_exn = gen_dynamic_value Code_Runtime.dynamic_value_exn;
   192 val dynamic_value_exn = gen_dynamic_value Code_Runtime.dynamic_value_exn;
   190 
   193 
   191 fun gen_static_value static_value ctxt consts Ts =
   194 fun gen_static_value static_value { ctxt, consts, Ts } =
   192   let
   195   let
   193     val static_value' = static_value cookie ctxt NONE I
   196     val static_value' = static_value cookie
   194       (union (op =) (map (term_of_const_for (Proof_Context.theory_of ctxt)) Ts) consts)
   197       { ctxt = ctxt, target = NONE, lift_postproc = I, consts =
       
   198         union (op =) (map (term_of_const_for (Proof_Context.theory_of ctxt)) Ts) consts }
   195   in fn ctxt' => fn t => static_value' ctxt' (mk_term_of t) end;
   199   in fn ctxt' => fn t => static_value' ctxt' (mk_term_of t) end;
   196 
   200 
   197 val static_value = gen_static_value Code_Runtime.static_value;
   201 val static_value = gen_static_value Code_Runtime.static_value;
   198 val static_value_strict = gen_static_value Code_Runtime.static_value_strict;
   202 val static_value_strict = gen_static_value Code_Runtime.static_value_strict;
   199 val static_value_exn = gen_static_value Code_Runtime.static_value_exn;
   203 val static_value_exn = gen_static_value Code_Runtime.static_value_exn;
   212   end;
   216   end;
   213 
   217 
   214 fun dynamic_conv ctxt = certify_eval ctxt dynamic_value
   218 fun dynamic_conv ctxt = certify_eval ctxt dynamic_value
   215   Code_Runtime.dynamic_holds_conv;
   219   Code_Runtime.dynamic_holds_conv;
   216 
   220 
   217 fun static_conv ctxt consts Ts =
   221 fun static_conv { ctxt, consts, Ts }  =
   218   let
   222   let
   219     val eqs = @{const_name Pure.eq} :: @{const_name HOL.eq} ::
   223     val eqs = @{const_name Pure.eq} :: @{const_name HOL.eq} ::
   220       map (fn T => Axclass.unoverload_const (Proof_Context.theory_of ctxt)
   224       map (fn T => Axclass.unoverload_const (Proof_Context.theory_of ctxt)
   221         (@{const_name HOL.equal}, T)) Ts; (*assumes particular code equations for Pure.eq etc.*)
   225         (@{const_name HOL.equal}, T)) Ts; (*assumes particular code equations for Pure.eq etc.*)
   222     val value = static_value ctxt consts Ts;
   226     val value = static_value { ctxt = ctxt, consts = consts, Ts = Ts };
   223     val holds = Code_Runtime.static_holds_conv ctxt (union (op =) eqs consts);
   227     val holds = Code_Runtime.static_holds_conv { ctxt = ctxt, consts = union (op =) eqs consts };
   224   in
   228   in
   225     fn ctxt' => certify_eval ctxt' value holds
   229     fn ctxt' => certify_eval ctxt' value holds
   226   end;
   230   end;
   227 
   231 
   228 
   232