src/Tools/code/code_wellsorted.ML
changeset 30947 dd551284a300
parent 30942 1e246776f876
child 30970 3fe2e418a071
equal deleted inserted replaced
30946:585c3f2622ea 30947:dd551284a300
     5 with explicit dependencies -- the Waisenhaus algorithm.
     5 with explicit dependencies -- the Waisenhaus algorithm.
     6 *)
     6 *)
     7 
     7 
     8 signature CODE_WELLSORTED =
     8 signature CODE_WELLSORTED =
     9 sig
     9 sig
    10   type T
    10   type code_algebra
    11   val eqns: T -> string -> (thm * bool) list
    11   type code_graph
    12   val typ: T -> string -> (string * sort) list * typ
    12   val eqns: code_graph -> string -> (thm * bool) list
    13   val all: T -> string list
    13   val typ: code_graph -> string -> (string * sort) list * typ
    14   val pretty: theory -> T -> Pretty.T
    14   val all: code_graph -> string list
    15   val obtain: theory -> string list -> term list -> ((sort -> sort) * Sorts.algebra) * T
    15   val pretty: theory -> code_graph -> Pretty.T
    16   val preprocess: theory -> cterm list -> (cterm * (thm -> thm)) list
    16   val obtain: theory -> string list -> term list -> code_algebra * code_graph
    17   val preprocess_term: theory -> term list -> (term * (term -> term)) list
    17   val eval_conv: theory -> (sort -> sort)
    18   val eval_conv: theory
    18     -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
    19     -> (term -> term) -> (term -> (sort -> sort) * Sorts.algebra -> T -> term -> thm) -> cterm -> thm
    19   val eval_term: theory -> (sort -> sort)
    20   val eval_term: theory
    20     -> (code_algebra -> code_graph -> (string * sort) list -> term -> term) -> term -> term
    21     -> (term -> term) -> (term -> (sort -> sort) * Sorts.algebra -> T -> term -> 'a) -> term -> 'a
    21   val eval: theory -> (sort -> sort)
       
    22     -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
    22 end
    23 end
    23 
    24 
    24 structure Code_Wellsorted : CODE_WELLSORTED =
    25 structure Code_Wellsorted : CODE_WELLSORTED =
    25 struct
    26 struct
    26 
    27 
    27 (** the equation graph type **)
    28 (** the algebra and code equation graph types **)
    28 
    29 
    29 type T = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
    30 type code_algebra = (sort -> sort) * Sorts.algebra;
       
    31 type code_graph = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
    30 
    32 
    31 fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
    33 fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
    32 fun typ eqngr = fst o Graph.get_node eqngr;
    34 fun typ eqngr = fst o Graph.get_node eqngr;
    33 fun all eqngr = Graph.keys eqngr;
    35 fun all eqngr = Graph.keys eqngr;
    34 
    36 
   269 
   271 
   270 (** store **)
   272 (** store **)
   271 
   273 
   272 structure Wellsorted = CodeDataFun
   274 structure Wellsorted = CodeDataFun
   273 (
   275 (
   274   type T = ((string * class) * sort list) list * T;
   276   type T = ((string * class) * sort list) list * code_graph;
   275   val empty = ([], Graph.empty);
   277   val empty = ([], Graph.empty);
   276   fun purge thy cs (arities, eqngr) =
   278   fun purge thy cs (arities, eqngr) =
   277     let
   279     let
   278       val del_cs = ((Graph.all_preds eqngr
   280       val del_cs = ((Graph.all_preds eqngr
   279         o filter (can (Graph.get_node eqngr))) cs);
   281         o filter (can (Graph.get_node eqngr))) cs);
   291 (** retrieval interfaces **)
   293 (** retrieval interfaces **)
   292 
   294 
   293 fun obtain thy cs ts = apsnd snd
   295 fun obtain thy cs ts = apsnd snd
   294   (Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts));
   296   (Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts));
   295 
   297 
   296 fun preprocess thy cts =
   298 fun prepare_sorts prep_sort (Const (c, ty)) = Const (c, map_type_tfree
   297   let
   299       (fn (v, sort) => TFree (v, prep_sort sort)) ty)
   298     val ts = map Thm.term_of cts;
   300   | prepare_sorts prep_sort (t1 $ t2) =
   299     val _ = map
   301       prepare_sorts prep_sort t1 $ prepare_sorts prep_sort t2
   300       (Sign.no_vars (Syntax.pp_global thy) o Term.map_types Type.no_tvars) ts;
   302   | prepare_sorts prep_sort (Abs (v, ty, t)) =
   301     fun make thm1 = (Thm.rhs_of thm1, fn thm2 =>
   303       Abs (v, Type.strip_sorts ty, prepare_sorts prep_sort t)
   302       let
   304   | prepare_sorts _ (Term.Free (v, ty)) = Term.Free (v, Type.strip_sorts ty)
   303         val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
   305   | prepare_sorts _ (t as Bound _) = t;
   304       in
   306 
   305         Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   307 fun gen_eval thy cterm_of conclude_evaluation prep_sort evaluator proto_ct =
   306           error ("could not construct evaluation proof:\n"
       
   307           ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
       
   308       end);
       
   309   in map (make o Code.preprocess_conv thy) cts end;
       
   310 
       
   311 fun preprocess_term thy ts =
       
   312   let
       
   313     val cts = map (Thm.cterm_of thy) ts;
       
   314     val postprocess = Code.postprocess_term thy;
       
   315   in map (fn (ct, _) => (Thm.term_of ct, postprocess)) (preprocess thy cts) end;
       
   316 
       
   317 (*FIXME rearrange here*)
       
   318 
       
   319 fun proto_eval thy cterm_of evaluator_lift preproc evaluator proto_ct =
       
   320   let
   308   let
   321     val ct = cterm_of proto_ct;
   309     val ct = cterm_of proto_ct;
   322     val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
   310     val _ = (Term.map_types Type.no_tvars o Sign.no_vars (Syntax.pp_global thy))
   323     val _ = Term.map_types Type.no_tvars (Thm.term_of ct);
   311       (Thm.term_of ct);
   324     fun consts_of t =
       
   325       fold_aterms (fn Const c_ty => cons c_ty | _ => I) t [];
       
   326     val thm = Code.preprocess_conv thy ct;
   312     val thm = Code.preprocess_conv thy ct;
   327     val ct' = Thm.rhs_of thm;
   313     val ct' = Thm.rhs_of thm;
   328     val t' = Thm.term_of ct';
   314     val t' = Thm.term_of ct';
   329     val consts = map fst (consts_of t');
   315     val vs = Term.add_tfrees t' [];
   330     val t'' = preproc t';
   316     val consts = fold_aterms
       
   317       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
       
   318     val t'' = prepare_sorts prep_sort t';
   331     val (algebra', eqngr') = obtain thy consts [t''];
   319     val (algebra', eqngr') = obtain thy consts [t''];
   332   in evaluator_lift (evaluator t' algebra' eqngr' t'') thm end;
   320   in conclude_evaluation (evaluator algebra' eqngr' vs t'' ct') thm end;
       
   321 
       
   322 fun simple_evaluator evaluator algebra eqngr vs t ct =
       
   323   evaluator algebra eqngr vs t;
   333 
   324 
   334 fun eval_conv thy =
   325 fun eval_conv thy =
   335   let
   326   let
   336     fun evaluator_lift thm2 thm1 =
   327     fun conclude_evaluation thm2 thm1 =
   337       let
   328       let
   338         val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
   329         val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
   339       in
   330       in
   340         Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   331         Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   341           error ("could not construct evaluation proof:\n"
   332           error ("could not construct evaluation proof:\n"
   342           ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
   333           ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
   343       end;
   334       end;
   344   in proto_eval thy I evaluator_lift end;
   335   in gen_eval thy I conclude_evaluation end;
   345 
   336 
   346 fun eval_term thy = proto_eval thy (Thm.cterm_of thy) (fn t => K t);
   337 fun eval_term thy prep_sort evaluator = gen_eval thy (Thm.cterm_of thy)
       
   338   (fn t => K (Code.postprocess_term thy t)) prep_sort (simple_evaluator evaluator);
       
   339 
       
   340 fun eval thy prep_sort evaluator = gen_eval thy (Thm.cterm_of thy)
       
   341   (fn t => K t) prep_sort (simple_evaluator evaluator);
   347 
   342 
   348 end; (*struct*)
   343 end; (*struct*)