src/Tools/Code/code_preproc.ML
changeset 32873 333945c9ac6a
parent 32872 019201eb7e07
child 33063 4d462963a7db
equal deleted inserted replaced
32872:019201eb7e07 32873:333945c9ac6a
    17   val print_codeproc: theory -> unit
    17   val print_codeproc: theory -> unit
    18 
    18 
    19   type code_algebra
    19   type code_algebra
    20   type code_graph
    20   type code_graph
    21   val eqns: code_graph -> string -> (thm * bool) list
    21   val eqns: code_graph -> string -> (thm * bool) list
    22   val typ: code_graph -> string -> (string * sort) list * typ
    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: theory -> code_graph -> Pretty.T
    24   val pretty: theory -> code_graph -> Pretty.T
    25   val obtain: theory -> string list -> term list -> code_algebra * code_graph
    25   val obtain: theory -> string list -> term list -> code_algebra * code_graph
    26   val eval_conv: theory
    26   val eval_conv: theory
    27     -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
    27     -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
    60 (
    60 (
    61   type T = thmproc;
    61   type T = thmproc;
    62   val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
    62   val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
    63   fun copy spec = spec;
    63   fun copy spec = spec;
    64   val extend = copy;
    64   val extend = copy;
    65   fun merge pp = merge_thmproc;
    65   fun merge _ = merge_thmproc;
    66 );
    66 );
    67 
    67 
    68 fun the_thmproc thy = case Code_Preproc_Data.get thy
    68 fun the_thmproc thy = case Code_Preproc_Data.get thy
    69  of Thmproc x => x;
    69  of Thmproc x => x;
    70 
    70 
   194 
   194 
   195 
   195 
   196 (** sort algebra and code equation graph types **)
   196 (** sort algebra and code equation graph types **)
   197 
   197 
   198 type code_algebra = (sort -> sort) * Sorts.algebra;
   198 type code_algebra = (sort -> sort) * Sorts.algebra;
   199 type code_graph = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
   199 type code_graph = ((string * sort) list * (thm * bool) list) Graph.T;
   200 
   200 
   201 fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
   201 fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
   202 fun typ eqngr = fst o Graph.get_node eqngr;
   202 fun sortargs eqngr = map snd o fst o Graph.get_node eqngr
   203 fun all eqngr = Graph.keys eqngr;
   203 fun all eqngr = Graph.keys eqngr;
   204 
   204 
   205 fun pretty thy eqngr =
   205 fun pretty thy eqngr =
   206   AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
   206   AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
   207   |> (map o apfst) (Code.string_of_const thy)
   207   |> (map o apfst) (Code.string_of_const thy)
   225 
   225 
   226 fun inst_params thy tyco =
   226 fun inst_params thy tyco =
   227   map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
   227   map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
   228     o maps (#params o AxClass.get_info thy);
   228     o maps (#params o AxClass.get_info thy);
   229 
   229 
       
   230 fun typscheme_rhss thy c eqns =
       
   231   let
       
   232     val tyscm = Code.typscheme_eqns thy c (map fst eqns);
       
   233     val rhss = [] |> (fold o fold o fold_aterms)
       
   234       (fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I)
       
   235         (map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns);
       
   236   in (tyscm, rhss) end;
       
   237 
   230 
   238 
   231 (* data structures *)
   239 (* data structures *)
   232 
   240 
   233 datatype const = Fun of string | Inst of class * string;
   241 datatype const = Fun of string | Inst of class * string;
   234 
   242 
   260 
   268 
   261 (* retrieving equations and instances from the background context *)
   269 (* retrieving equations and instances from the background context *)
   262 
   270 
   263 fun obtain_eqns thy eqngr c =
   271 fun obtain_eqns thy eqngr c =
   264   case try (Graph.get_node eqngr) c
   272   case try (Graph.get_node eqngr) c
   265    of SOME ((lhs, _), eqns) => ((lhs, []), [])
   273    of SOME (lhs, eqns) => ((lhs, []), [])
   266     | NONE => let
   274     | NONE => let
   267         val eqns = Code.these_eqns thy c
   275         val eqns = Code.these_eqns thy c
   268           |> preprocess thy c;
   276           |> preprocess thy c;
   269         val ((lhs, _), rhss) = Code.typscheme_rhss_eqns thy c (map fst eqns);
   277         val ((lhs, _), rhss) = typscheme_rhss thy c eqns;
   270       in ((lhs, rhss), eqns) end;
   278       in ((lhs, rhss), eqns) end;
   271 
   279 
   272 fun obtain_instance thy arities (inst as (class, tyco)) =
   280 fun obtain_instance thy arities (inst as (class, tyco)) =
   273   case AList.lookup (op =) arities inst
   281   case AList.lookup (op =) arities inst
   274    of SOME classess => (classess, ([], []))
   282    of SOME classess => (classess, ([], []))
   411       (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
   419       (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
   412     val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
   420     val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
   413       Vartab.update ((v, 0), sort)) lhs;
   421       Vartab.update ((v, 0), sort)) lhs;
   414     val eqns = proto_eqns
   422     val eqns = proto_eqns
   415       |> (map o apfst) (inst_thm thy inst_tab);
   423       |> (map o apfst) (inst_thm thy inst_tab);
   416     val (tyscm, rhss') = Code.typscheme_rhss_eqns thy c (map fst eqns);
   424     val ((vs, _), rhss') = typscheme_rhss thy c eqns;
   417     val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr;
   425     val eqngr' = Graph.new_node (c, (vs, eqns)) eqngr;
   418   in (map (pair c) rhss' @ rhss, eqngr') end;
   426   in (map (pair c) rhss' @ rhss, eqngr') end;
   419 
   427 
   420 fun extend_arities_eqngr thy cs ts (arities, eqngr) =
   428 fun extend_arities_eqngr thy cs ts (arities, (eqngr : code_graph)) =
   421   let
   429   let
   422     val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
   430     val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
   423       insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
   431       insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
   424     val (vardeps, (eqntab, insts)) = empty_vardeps_data
   432     val (vardeps, (eqntab, insts)) = empty_vardeps_data
   425       |> fold (ensure_fun thy arities eqngr) cs
   433       |> fold (ensure_fun thy arities eqngr) cs
   428     val pp = Syntax.pp_global thy;
   436     val pp = Syntax.pp_global thy;
   429     val algebra = Sorts.subalgebra pp (is_proper_class thy)
   437     val algebra = Sorts.subalgebra pp (is_proper_class thy)
   430       (AList.lookup (op =) arities') (Sign.classes_of thy);
   438       (AList.lookup (op =) arities') (Sign.classes_of thy);
   431     val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr);
   439     val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr);
   432     fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
   440     fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
   433       (rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c);
   441       (rhs ~~ sortargs eqngr' c);
   434     val eqngr'' = fold (fn (c, rhs) => fold
   442     val eqngr'' = fold (fn (c, rhs) => fold
   435       (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
   443       (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
   436   in (algebra, (arities', eqngr'')) end;
   444   in (algebra, (arities', eqngr'')) end;
   437 
   445 
   438 
   446