src/HOL/Codatatype/Tools/bnf_fp_util.ML
author blanchet
Mon Sep 10 17:35:53 2012 +0200 (2012-09-10)
changeset 49255 2ecc533d6697
parent 49240 f7e75b802ef2
child 49264 9059e0dbdbc1
permissions -rw-r--r--
use balanced sums for constructors (to gracefully handle 100 constructors or more)
     1 (*  Title:      HOL/Codatatype/Tools/bnf_fp_util.ML
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Copyright   2012
     4 
     5 Shared library for the datatype and the codatatype construction.
     6 *)
     7 
     8 signature BNF_FP_UTIL =
     9 sig
    10   val time: Timer.real_timer -> string -> Timer.real_timer
    11 
    12   val IITN: string
    13   val LevN: string
    14   val algN: string
    15   val behN: string
    16   val bisN: string
    17   val carTN: string
    18   val coN: string
    19   val coinductN: string
    20   val coiterN: string
    21   val unf_coiter_uniqueN: string
    22   val corecN: string
    23   val exhaustN: string
    24   val fldN: string
    25   val fld_exhaustN: string
    26   val fld_induct2N: string
    27   val fld_inductN: string
    28   val fld_injectN: string
    29   val fld_iterN: string
    30   val fld_itersN: string
    31   val fld_recN: string
    32   val fld_recsN: string
    33   val fld_unfN: string
    34   val fld_unf_coitersN: string
    35   val fld_unf_corecsN: string
    36   val hsetN: string
    37   val hset_recN: string
    38   val inductN: string
    39   val injectN: string
    40   val isNodeN: string
    41   val iterN: string
    42   val fld_iter_uniqueN: string
    43   val lsbisN: string
    44   val map_simpsN: string
    45   val map_uniqueN: string
    46   val min_algN: string
    47   val morN: string
    48   val nchotomyN: string
    49   val pred_coinductN: string
    50   val pred_coinduct_uptoN: string
    51   val recN: string
    52   val rel_coinductN: string
    53   val rel_coinduct_uptoN: string
    54   val rvN: string
    55   val set_inclN: string
    56   val set_set_inclN: string
    57   val strTN: string
    58   val str_initN: string
    59   val sum_bdN: string
    60   val sum_bdTN: string
    61   val unfN: string
    62   val unf_coinductN: string
    63   val unf_coinduct_uptoN: string
    64   val unf_coiterN: string
    65   val unf_coitersN: string
    66   val unf_corecN: string
    67   val unf_corecsN: string
    68   val unf_exhaustN: string
    69   val unf_fldN: string
    70   val unf_injectN: string
    71   val uniqueN: string
    72   val uptoN: string
    73 
    74   val mk_exhaustN: string -> string
    75   val mk_injectN: string -> string
    76   val mk_nchotomyN: string -> string
    77   val mk_set_simpsN: int -> string
    78   val mk_set_minimalN: int -> string
    79   val mk_set_inductN: int -> string
    80 
    81   val split_conj_thm: thm -> thm list
    82   val split_conj_prems: int -> thm -> thm
    83 
    84   val mk_sumTN: typ list -> typ
    85 
    86   val Inl_const: typ -> typ -> term
    87   val Inr_const: typ -> typ -> term
    88 
    89   val mk_Inl: typ -> term -> term
    90   val mk_Inr: typ -> term -> term
    91   val mk_InN: typ list -> term -> int -> term
    92   val mk_sum_case: term * term -> term
    93   val mk_sum_caseN: term list -> term
    94 
    95   val dest_sumT: typ -> typ * typ
    96   val dest_sumTN: int -> typ -> typ list
    97   val dest_tupleT: int -> typ -> typ list
    98 
    99   val mk_Field: term -> term
   100   val mk_union: term * term -> term
   101 
   102   val mk_sumEN: int -> thm
   103   val mk_sum_casesN: int -> int -> thm
   104 
   105   val mk_tactics: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list
   106 
   107   val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
   108 
   109   val fp_bnf: (mixfix list -> (string * sort) list option -> binding list ->
   110     typ list * typ list list -> BNF_Def.BNF list -> local_theory -> 'a) ->
   111     binding list -> mixfix list -> (string * sort) list -> ((string * sort) * typ) list ->
   112     local_theory -> BNF_Def.BNF list * 'a
   113   val fp_bnf_cmd: (mixfix list -> (string * sort) list option -> binding list ->
   114     typ list * typ list list -> BNF_Def.BNF list -> local_theory -> 'a) ->
   115     binding list * (string list * string list) -> local_theory -> 'a
   116 end;
   117 
   118 structure BNF_FP_Util : BNF_FP_UTIL =
   119 struct
   120 
   121 open BNF_Comp
   122 open BNF_Def
   123 open BNF_Util
   124 
   125 val timing = true;
   126 fun time timer msg = (if timing
   127   then warning (msg ^ ": " ^ ATP_Util.string_from_time (Timer.checkRealTimer timer))
   128   else (); Timer.startRealTimer ());
   129 
   130 val preN = "pre_"
   131 val rawN = "raw_"
   132 
   133 val coN = "co"
   134 val algN = "alg"
   135 val IITN = "IITN"
   136 val iterN = "iter"
   137 val coiterN = coN ^ iterN
   138 val uniqueN = "_unique"
   139 val fldN = "fld"
   140 val unfN = "unf"
   141 val fld_iterN = fldN ^ "_" ^ iterN
   142 val fld_itersN = fld_iterN ^ "s"
   143 val unf_coiterN = unfN ^ "_" ^ coiterN
   144 val unf_coitersN = unf_coiterN ^ "s"
   145 val fld_iter_uniqueN = fld_iterN ^ uniqueN
   146 val unf_coiter_uniqueN = unf_coiterN ^ uniqueN
   147 val fld_unf_coitersN = fldN ^ "_" ^ unf_coiterN ^ "s"
   148 val map_simpsN = mapN ^ "_simps"
   149 val map_uniqueN = mapN ^ uniqueN
   150 val min_algN = "min_alg"
   151 val morN = "mor"
   152 val bisN = "bis"
   153 val lsbisN = "lsbis"
   154 val sum_bdTN = "sbdT"
   155 val sum_bdN = "sbd"
   156 val carTN = "carT"
   157 val strTN = "strT"
   158 val isNodeN = "isNode"
   159 val LevN = "Lev"
   160 val rvN = "recover"
   161 val behN = "beh"
   162 fun mk_set_simpsN i = mk_setN i ^ "_simps"
   163 fun mk_set_minimalN i = mk_setN i ^ "_minimal"
   164 fun mk_set_inductN i = mk_setN i ^ "_induct"
   165 
   166 val str_initN = "str_init"
   167 val recN = "rec"
   168 val corecN = coN ^ recN
   169 val fld_recN = fldN ^ "_" ^ recN
   170 val fld_recsN = fld_recN ^ "s"
   171 val unf_corecN = unfN ^ "_" ^ corecN
   172 val unf_corecsN = unf_corecN ^ "s"
   173 val fld_unf_corecsN = fldN ^ "_" ^ unf_corecN ^ "s"
   174 
   175 val fld_unfN = fldN ^ "_" ^ unfN
   176 val unf_fldN = unfN ^ "_" ^ fldN
   177 val nchotomyN = "nchotomy"
   178 fun mk_nchotomyN s = s ^ "_" ^ nchotomyN
   179 val injectN = "inject"
   180 fun mk_injectN s = s ^ "_" ^ injectN
   181 val exhaustN = "exhaust"
   182 fun mk_exhaustN s = s ^ "_" ^ exhaustN
   183 val fld_injectN = mk_injectN fldN
   184 val fld_exhaustN = mk_exhaustN fldN
   185 val unf_injectN = mk_injectN unfN
   186 val unf_exhaustN = mk_exhaustN unfN
   187 val inductN = "induct"
   188 val coinductN = coN ^ inductN
   189 val fld_inductN = fldN ^ "_" ^ inductN
   190 val fld_induct2N = fld_inductN ^ "2"
   191 val unf_coinductN = unfN ^ "_" ^ coinductN
   192 val rel_coinductN = relN ^ "_" ^ coinductN
   193 val pred_coinductN = predN ^ "_" ^ coinductN
   194 val uptoN = "upto"
   195 val unf_coinduct_uptoN = unf_coinductN ^ "_" ^ uptoN
   196 val rel_coinduct_uptoN = rel_coinductN ^ "_" ^ uptoN
   197 val pred_coinduct_uptoN = pred_coinductN ^ "_" ^ uptoN
   198 val hsetN = "Hset"
   199 val hset_recN = hsetN ^ "_rec"
   200 val set_inclN = "set_incl"
   201 val set_set_inclN = "set_set_incl"
   202 
   203 fun mk_sumTN Ts = Library.foldr1 mk_sumT Ts;
   204 
   205 fun Inl_const LT RT = Const (@{const_name Inl}, LT --> mk_sumT (LT, RT));
   206 fun mk_Inl RT t = Inl_const (fastype_of t) RT $ t;
   207 
   208 fun Inr_const LT RT = Const (@{const_name Inr}, RT --> mk_sumT (LT, RT));
   209 fun mk_Inr LT t = Inr_const LT (fastype_of t) $ t;
   210 
   211 fun mk_InN [_] t 1 = t
   212   | mk_InN (_ :: Ts) t 1 = mk_Inl (mk_sumTN Ts) t
   213   | mk_InN (LT :: Ts) t m = mk_Inr LT (mk_InN Ts t (m - 1))
   214   | mk_InN Ts t _ = raise (TYPE ("mk_InN", Ts, [t]));
   215 
   216 fun mk_sum_case (f, g) =
   217   let
   218     val fT = fastype_of f;
   219     val gT = fastype_of g;
   220   in
   221     Const (@{const_name sum_case},
   222       fT --> gT --> mk_sumT (domain_type fT, domain_type gT) --> range_type fT) $ f $ g
   223   end;
   224 
   225 fun mk_sum_caseN [f] = f
   226   | mk_sum_caseN (f :: fs) = mk_sum_case (f, mk_sum_caseN fs);
   227 
   228 fun dest_sumT (Type (@{type_name sum}, [T, T'])) = (T, T');
   229 
   230 fun dest_sumTN 1 T = [T]
   231   | dest_sumTN n (Type (@{type_name sum}, [T, T'])) = T :: dest_sumTN (n - 1) T';
   232 
   233 (* TODO: move something like this to "HOLogic"? *)
   234 fun dest_tupleT 0 @{typ unit} = []
   235   | dest_tupleT 1 T = [T]
   236   | dest_tupleT n (Type (@{type_name prod}, [T, T'])) = T :: dest_tupleT (n - 1) T';
   237 
   238 fun mk_Field r =
   239   let val T = fst (dest_relT (fastype_of r));
   240   in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
   241 
   242 val mk_union = HOLogic.mk_binop @{const_name sup};
   243 
   244 (*dangerous; use with monotonic, converging functions only!*)
   245 fun fixpoint eq f X = if subset eq (f X, X) then X else fixpoint eq f (f X);
   246 
   247 (* stolen from "~~/src/HOL/Tools/Datatype/datatype_aux.ML" *)
   248 fun split_conj_thm th =
   249   ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
   250 
   251 fun split_conj_prems limit th =
   252   let
   253     fun split n i th =
   254       if i = n then th else split n (i + 1) (conjI RSN (i, th)) handle THM _ => th;
   255   in split limit 1 th end;
   256 
   257 fun mk_sumEN 1 = @{thm one_pointE}
   258   | mk_sumEN 2 = @{thm sumE}
   259   | mk_sumEN n =
   260     (fold (fn i => fn thm => @{thm obj_sum_step} RSN (i, thm)) (2 upto n - 1) @{thm obj_sumE}) OF
   261       replicate n (impI RS allI);
   262 
   263 fun mk_sum_casesN 1 1 = @{thm refl}
   264   | mk_sum_casesN _ 1 = @{thm sum.cases(1)}
   265   | mk_sum_casesN 2 2 = @{thm sum.cases(2)}
   266   | mk_sum_casesN n m = trans OF [@{thm sum_case_step(2)}, mk_sum_casesN (n - 1) (m - 1)];
   267 
   268 fun mk_tactics mid mcomp mcong snat bdco bdinf sbd inbd wpull =
   269   [mid, mcomp, mcong] @ snat @ [bdco, bdinf] @ sbd @ [inbd, wpull];
   270 
   271 (* FIXME: because of "@ lhss", the output could contain type variables that are not in the input;
   272    also, "fp_sort" should put the "resBs" first and in the order in which they appear *)
   273 fun fp_sort lhss NONE Ass = Library.sort (Term_Ord.typ_ord o pairself TFree)
   274     (subtract (op =) lhss (fold (fold (insert (op =))) Ass [])) @ lhss
   275   | fp_sort lhss (SOME resBs) Ass =
   276     (subtract (op =) lhss (filter (fn T => exists (fn Ts => member (op =) Ts T) Ass) resBs)) @ lhss;
   277 
   278 fun mk_fp_bnf timer construct resBs bs sort lhss bnfs deadss livess unfold lthy =
   279   let
   280     val name = fold_rev (fn b => fn s => Binding.name_of b ^ s) bs "";
   281     fun qualify i bind =
   282       let val namei = if i > 0 then name ^ string_of_int i else name;
   283       in
   284         if member (op =) (#2 (Binding.dest bind)) (namei, true) then bind
   285         else Binding.prefix_name namei bind
   286       end;
   287 
   288     val Ass = map (map dest_TFree) livess;
   289     val resDs = (case resBs of NONE => [] | SOME Ts => fold (subtract (op =)) Ass Ts);
   290     val Ds = fold (fold Term.add_tfreesT) deadss [];
   291 
   292     val _ = (case Library.inter (op =) Ds lhss of [] => ()
   293       | A :: _ => error ("Nonadmissible type recursion (cannot take fixed point of dead type \
   294         \variable " ^ quote (Syntax.string_of_typ lthy (TFree A)) ^ ")"));
   295 
   296     val timer = time (timer "Construction of BNFs");
   297 
   298     val ((kill_poss, _), (bnfs', (unfold', lthy'))) =
   299       normalize_bnfs qualify Ass Ds sort bnfs unfold lthy;
   300 
   301     val Dss = map3 (append oo map o nth) livess kill_poss deadss;
   302 
   303     val ((bnfs'', deadss), lthy'') =
   304       fold_map3 (seal_bnf unfold') (map (Binding.prefix_name preN) bs) Dss bnfs' lthy'
   305       |>> split_list;
   306 
   307     val timer = time (timer "Normalization & sealing of BNFs");
   308 
   309     val res = construct resBs bs (map TFree resDs, deadss) bnfs'' lthy'';
   310 
   311     val timer = time (timer "FP construction in total");
   312   in
   313     (bnfs'', res)
   314   end;
   315 
   316 fun fp_bnf construct bs mixfixes resBs eqs lthy =
   317   let
   318     val timer = time (Timer.startRealTimer ());
   319     val (lhss, rhss) = split_list eqs;
   320     val sort = fp_sort lhss (SOME resBs);
   321     val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
   322       (fold_map2 (fn b => bnf_of_typ Smart_Inline (Binding.prefix_name rawN b) I sort) bs rhss
   323         (empty_unfold, lthy));
   324   in
   325     mk_fp_bnf timer (construct mixfixes) (SOME resBs) bs sort lhss bnfs Dss Ass unfold lthy'
   326   end;
   327 
   328 fun fp_bnf_cmd construct (bs, (raw_lhss, raw_bnfs)) lthy =
   329   let
   330     val timer = time (Timer.startRealTimer ());
   331     val lhss = map (dest_TFree o Syntax.read_typ lthy) raw_lhss;
   332     val sort = fp_sort lhss NONE;
   333     val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
   334       (fold_map2 (fn b => fn rawT =>
   335         (bnf_of_typ Smart_Inline (Binding.prefix_name rawN b) I sort (Syntax.read_typ lthy rawT)))
   336       bs raw_bnfs (empty_unfold, lthy));
   337   in
   338     snd (mk_fp_bnf timer (construct (map (K NoSyn) bs)) NONE bs sort lhss bnfs Dss Ass unfold lthy')
   339   end;
   340 
   341 end;