src/HOL/Codatatype/Tools/bnf_fp_util.ML
changeset 49134 846264f80f16
parent 49132 81487fc17185
child 49141 aca966dc18f6
equal deleted inserted replaced
49133:4680ac067cb8 49134:846264f80f16
    96 
    96 
    97   val mk_tactics: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list
    97   val mk_tactics: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list
    98 
    98 
    99   val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
    99   val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
   100 
   100 
   101   val fp_bnf: (binding list -> typ list list -> BNF_Def.BNF list -> Proof.context -> 'a) ->
   101   val fp_bnf: (binding list -> (string * sort) list -> typ list list -> BNF_Def.BNF list ->
   102     binding list -> ((string * sort) * typ) list -> Proof.context -> 'a
   102     local_theory -> 'a) ->
   103   val fp_bnf_cmd: (binding list -> typ list list -> BNF_Def.BNF list -> Proof.context -> 'a) ->
   103     binding list -> (string * sort) list -> ((string * sort) * typ) list -> local_theory -> 'a
   104     binding list * (string list * string list) -> Proof.context -> 'a
   104   val fp_bnf_cmd: (binding list -> (string * sort) list -> typ list list -> BNF_Def.BNF list ->
       
   105     local_theory -> 'a) ->
       
   106     binding list * (string list * string list) -> local_theory -> 'a
   105 end;
   107 end;
   106 
   108 
   107 structure BNF_FP_Util : BNF_FP_UTIL =
   109 structure BNF_FP_Util : BNF_FP_UTIL =
   108 struct
   110 struct
   109 
   111 
   253   [mid, mcomp, mcong] @ snat @ [bdco, bdinf] @ sbd @ [inbd, wpull];
   255   [mid, mcomp, mcong] @ snat @ [bdco, bdinf] @ sbd @ [inbd, wpull];
   254 
   256 
   255 fun fp_sort lhss Ass = Library.sort (Term_Ord.typ_ord o pairself TFree)
   257 fun fp_sort lhss Ass = Library.sort (Term_Ord.typ_ord o pairself TFree)
   256   (subtract (op =) lhss (fold (fold (insert (op =))) Ass [])) @ lhss;
   258   (subtract (op =) lhss (fold (fold (insert (op =))) Ass [])) @ lhss;
   257 
   259 
   258 fun mk_fp_bnf timer construct bs sort bnfs deadss livess unfold lthy =
   260 fun mk_fp_bnf timer construct bs resBs sort bnfs deadss livess unfold lthy =
   259   let
   261   let
   260     val name = fold_rev (fn b => fn s => Binding.name_of b ^ s) bs "";
   262     val name = fold_rev (fn b => fn s => Binding.name_of b ^ s) bs "";
   261     fun qualify i bind =
   263     fun qualify i bind =
   262       let val namei = if i > 0 then name ^ string_of_int i else name;
   264       let val namei = if i > 0 then name ^ string_of_int i else name;
   263       in
   265       in
   264         if member (op =) (#2 (Binding.dest bind)) (namei, true) then bind
   266         if member (op =) (#2 (Binding.dest bind)) (namei, true) then bind
   265         else Binding.prefix_name namei bind
   267         else Binding.prefix_name namei bind
   266       end;
   268       end;
   267 
   269 
   268     val Ass = map (map dest_TFree) livess;
   270     val Ass = map (map dest_TFree) livess;
   269     val Ds = fold (fold Term.add_tfreesT) deadss [];
   271     val resDs = fold (subtract (op =)) Ass resBs;
       
   272     val Ds = fold (fold Term.add_tfreesT) deadss resDs;
   270 
   273 
   271     val _ = (case Library.inter (op =) Ds (fold (union (op =)) Ass []) of [] => ()
   274     val _ = (case Library.inter (op =) Ds (fold (union (op =)) Ass []) of [] => ()
   272       | A :: _ => error ("Nonadmissible type recursion (cannot take fixed point of dead type \
   275       | A :: _ => error ("Nonadmissible type recursion (cannot take fixed point of dead type \
   273         \variable " ^ quote (Syntax.string_of_typ lthy (TFree A)) ^ ")"));
   276         \variable " ^ quote (Syntax.string_of_typ lthy (TFree A)) ^ ")"));
   274 
   277 
   282     val (bnfs'', lthy'') =
   285     val (bnfs'', lthy'') =
   283       fold_map3 (seal_bnf unfold') (map (Binding.suffix_name "BNF") bs) Dss bnfs' lthy';
   286       fold_map3 (seal_bnf unfold') (map (Binding.suffix_name "BNF") bs) Dss bnfs' lthy';
   284 
   287 
   285     val timer = time (timer "Normalization & sealing of BNFs");
   288     val timer = time (timer "Normalization & sealing of BNFs");
   286 
   289 
   287     val res = construct bs Dss bnfs'' lthy'';
   290     val res = construct bs resDs Dss bnfs'' lthy'';
   288 
   291 
   289     val timer = time (timer "FP construction in total");
   292     val timer = time (timer "FP construction in total");
   290   in
   293   in
   291     res
   294     res
   292   end;
   295   end;
   293 
   296 
   294 fun fp_bnf construct bs eqs lthy =
   297 fun fp_bnf construct bs resBs eqs lthy =
   295   let
   298   let
   296     val timer = time (Timer.startRealTimer ());
   299     val timer = time (Timer.startRealTimer ());
   297     val (lhss, rhss) = split_list eqs;
   300     val (lhss, rhss) = split_list eqs;
   298     val sort = fp_sort lhss;
   301     val sort = fp_sort lhss;
   299     val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
   302     val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
   300       (fold_map2 (fn b => bnf_of_typ Smart_Inline (Binding.suffix_name "RAW" b) I sort) bs rhss
   303       (fold_map2 (fn b => bnf_of_typ Smart_Inline (Binding.suffix_name "RAW" b) I sort) bs rhss
   301         (empty_unfold, lthy));
   304         (empty_unfold, lthy));
   302   in
   305   in
   303     mk_fp_bnf timer construct bs sort bnfs Dss Ass unfold lthy'
   306     mk_fp_bnf timer construct bs resBs sort bnfs Dss Ass unfold lthy'
   304   end;
   307   end;
   305 
   308 
   306 fun fp_bnf_cmd construct (bs, (raw_lhss, raw_bnfs)) lthy =
   309 fun fp_bnf_cmd construct (bs, (raw_lhss, raw_bnfs)) lthy =
   307   let
   310   let
   308     val timer = time (Timer.startRealTimer ());
   311     val timer = time (Timer.startRealTimer ());
   311     val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
   314     val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
   312       (fold_map2 (fn b => fn rawT =>
   315       (fold_map2 (fn b => fn rawT =>
   313         (bnf_of_typ Smart_Inline (Binding.suffix_name "RAW" b) I sort (Syntax.read_typ lthy rawT)))
   316         (bnf_of_typ Smart_Inline (Binding.suffix_name "RAW" b) I sort (Syntax.read_typ lthy rawT)))
   314         bs raw_bnfs (empty_unfold, lthy));
   317         bs raw_bnfs (empty_unfold, lthy));
   315   in
   318   in
   316     mk_fp_bnf timer construct bs sort bnfs Dss Ass unfold lthy'
   319     mk_fp_bnf timer construct bs [] sort bnfs Dss Ass unfold lthy'
   317   end;
   320   end;
   318 
   321 
   319 end;
   322 end;