src/Tools/Code/code_preproc.ML
changeset 56920 d651b944c67e
parent 56334 6b3739fee456
child 56929 40213e24c8c4
equal deleted inserted replaced
56919:6389a8c1268a 56920:d651b944c67e
    22   val sortargs: code_graph -> string -> sort list
    22   val sortargs: code_graph -> string -> sort list
    23   val all: code_graph -> string list
    23   val all: code_graph -> string list
    24   val pretty: Proof.context -> code_graph -> Pretty.T
    24   val pretty: Proof.context -> code_graph -> Pretty.T
    25   val obtain: bool -> theory -> string list -> term list -> code_algebra * code_graph
    25   val obtain: bool -> theory -> string list -> term list -> code_algebra * code_graph
    26   val dynamic_conv: Proof.context
    26   val dynamic_conv: Proof.context
    27     -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv
    27     -> (code_algebra -> code_graph -> term -> conv) -> conv
    28   val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'a)
    28   val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'a)
    29     -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
    29     -> (code_algebra -> code_graph -> term -> 'a) -> term -> 'a
    30   val static_conv: Proof.context -> string list
    30   val static_conv: Proof.context -> string list
    31     -> (code_algebra -> code_graph -> Proof.context -> (string * sort) list -> term -> conv)
    31     -> (code_algebra -> code_graph -> Proof.context -> term -> conv)
    32     -> Proof.context -> conv
    32     -> Proof.context -> conv
    33   val static_value: Proof.context -> ((term -> term) -> 'a -> 'a) -> string list
    33   val static_value: Proof.context -> ((term -> term) -> 'a -> 'a) -> string list
    34     -> (code_algebra -> code_graph -> Proof.context -> (string * sort) list -> term -> 'a)
    34     -> (code_algebra -> code_graph -> Proof.context -> term -> 'a)
    35     -> Proof.context -> term -> 'a
    35     -> Proof.context -> term -> 'a
    36 
    36 
    37   val setup: theory -> theory
    37   val setup: theory -> theory
    38 end
    38 end
    39 
    39 
   460 
   460 
   461 fun obtain ignore_cache thy consts ts = apsnd snd
   461 fun obtain ignore_cache thy consts ts = apsnd snd
   462   (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy)
   462   (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy)
   463     (extend_arities_eqngr (Proof_Context.init_global thy) consts ts));
   463     (extend_arities_eqngr (Proof_Context.init_global thy) consts ts));
   464 
   464 
   465 fun dest_cterm ct = let val t = Thm.term_of ct in (Term.add_tfrees t [], t) end;
       
   466 
       
   467 fun dynamic_conv ctxt conv = no_variables_conv ctxt (fn ct =>
   465 fun dynamic_conv ctxt conv = no_variables_conv ctxt (fn ct =>
   468   let
   466   let
   469     val thm1 = preprocess_conv ctxt ctxt ct;
   467     val thm1 = preprocess_conv ctxt ctxt ct;
   470     val ct' = Thm.rhs_of thm1;
   468     val ct' = Thm.rhs_of thm1;
   471     val (vs', t') = dest_cterm ct';
   469     val t' = Thm.term_of ct';
   472     val consts = fold_aterms
   470     val consts = fold_aterms
   473       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   471       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   474     val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
   472     val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
   475     val thm2 = conv algebra' eqngr' vs' t' ct';
   473     val thm2 = conv algebra' eqngr' t' ct';
   476     val thm3 = postprocess_conv ctxt ctxt (Thm.rhs_of thm2);
   474     val thm3 = postprocess_conv ctxt ctxt (Thm.rhs_of thm2);
   477   in
   475   in
   478     Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   476     Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   479       error ("could not construct evaluation proof:\n"
   477       error ("could not construct evaluation proof:\n"
   480       ^ (cat_lines o map (Display.string_of_thm ctxt)) [thm1, thm2, thm3])
   478       ^ (cat_lines o map (Display.string_of_thm ctxt)) [thm1, thm2, thm3])
   481   end);
   479   end);
   482 
   480 
   483 fun dynamic_value ctxt postproc evaluator t =
   481 fun dynamic_value ctxt postproc evaluator t =
   484   let
   482   let
   485     val (resubst, t') = preprocess_term ctxt ctxt t;
   483     val (resubst, t') = preprocess_term ctxt ctxt t;
   486     val vs' = Term.add_tfrees t' [];
       
   487     val consts = fold_aterms
   484     val consts = fold_aterms
   488       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   485       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   489     val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
   486     val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
   490   in
   487   in
   491     t'
   488     t'
   492     |> evaluator algebra' eqngr' vs'
   489     |> evaluator algebra' eqngr'
   493     |> postproc (postprocess_term ctxt ctxt o resubst)
   490     |> postproc (postprocess_term ctxt ctxt o resubst)
   494   end;
   491   end;
   495 
   492 
   496 fun static_conv ctxt consts conv =
   493 fun static_conv ctxt consts conv =
   497   let
   494   let
   498     val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
   495     val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
   499     val pre_conv = preprocess_conv ctxt;
   496     val pre_conv = preprocess_conv ctxt;
   500     val conv' = conv algebra eqngr;
   497     val conv' = conv algebra eqngr;
   501     val post_conv = postprocess_conv ctxt;
   498     val post_conv = postprocess_conv ctxt;
   502   in fn ctxt' => no_variables_conv ctxt' ((pre_conv ctxt')
   499   in fn ctxt' => no_variables_conv ctxt' ((pre_conv ctxt')
   503     then_conv (fn ct => uncurry (conv' ctxt') (dest_cterm ct) ct)
   500     then_conv (fn ct => conv' ctxt' (Thm.term_of ct) ct)
   504     then_conv (post_conv ctxt'))
   501     then_conv (post_conv ctxt'))
   505   end;
   502   end;
   506 
   503 
   507 fun static_value ctxt postproc consts evaluator =
   504 fun static_value ctxt postproc consts evaluator =
   508   let
   505   let
   511     val evaluator' = evaluator algebra eqngr;
   508     val evaluator' = evaluator algebra eqngr;
   512     val postproc' = postprocess_term ctxt;
   509     val postproc' = postprocess_term ctxt;
   513   in fn ctxt' => 
   510   in fn ctxt' => 
   514     preproc ctxt'
   511     preproc ctxt'
   515     #-> (fn resubst => fn t => t
   512     #-> (fn resubst => fn t => t
   516       |> evaluator' ctxt' (Term.add_tfrees t [])
   513       |> evaluator' ctxt'
   517       |> postproc (postproc' ctxt' o resubst))
   514       |> postproc (postproc' ctxt' o resubst))
   518   end;
   515   end;
   519 
   516 
   520 
   517 
   521 (** setup **)
   518 (** setup **)