src/HOL/BNF/Tools/bnf_fp_rec_sugar_util.ML
author blanchet
Mon, 04 Nov 2013 16:53:43 +0100
changeset 55698 8fdb4dc08ed1
parent 55695 a596292be9a8
child 55751 bc24e1ccfd35
permissions -rw-r--r--
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
     1 (*  Title:      HOL/BNF/Tools/bnf_fp_rec_sugar_util.ML
     2     Author:     Lorenz Panny, TU Muenchen
     3     Author:     Jasmin Blanchette, TU Muenchen
     4     Copyright   2013
     5 
     6 Library for recursor and corecursor sugar.
     7 *)
     8 
     9 signature BNF_FP_REC_SUGAR_UTIL =
    10 sig
    11   val indexed: 'a list -> int -> int list * int
    12   val indexedd: 'a list list -> int -> int list list * int
    13   val indexeddd: ''a list list list -> int -> int list list list * int
    14   val indexedddd: 'a list list list list -> int -> int list list list list * int
    15   val find_index_eq: ''a list -> ''a -> int
    16   val finds: ('a * 'b -> bool) -> 'a list -> 'b list -> ('a * 'b list) list * 'b list
    17 
    18   val drop_All: term -> term
    19 
    20   val mk_partial_compN: int -> typ -> term -> term
    21   val mk_partial_comp: typ -> typ -> term -> term
    22   val mk_compN: int -> typ list -> term * term -> term
    23   val mk_comp: typ list -> term * term -> term
    24 
    25   val get_indices: ((binding * typ) * 'a) list -> term -> int list
    26 end;
    27 
    28 structure BNF_FP_Rec_Sugar_Util : BNF_FP_REC_SUGAR_UTIL =
    29 struct
    30 
    31 fun indexed xs h = let val h' = h + length xs in (h upto h' - 1, h') end;
    32 fun indexedd xss = fold_map indexed xss;
    33 fun indexeddd xsss = fold_map indexedd xsss;
    34 fun indexedddd xssss = fold_map indexeddd xssss;
    35 
    36 fun find_index_eq hs h = find_index (curry (op =) h) hs;
    37 
    38 fun finds eq = fold_map (fn x => List.partition (curry eq x) #>> pair x);
    39 
    40 fun drop_All t =
    41   subst_bounds (strip_qnt_vars @{const_name all} t |> map Free |> rev,
    42     strip_qnt_body @{const_name all} t);
    43 
    44 fun mk_partial_comp gT fT g =
    45   let val T = domain_type fT --> range_type gT in
    46     Const (@{const_name Fun.comp}, gT --> fT --> T) $ g
    47   end;
    48 
    49 fun mk_partial_compN 0 _ g = g
    50   | mk_partial_compN n fT g =
    51     let val g' = mk_partial_compN (n - 1) (range_type fT) g in
    52       mk_partial_comp (fastype_of g') fT g'
    53     end;
    54 
    55 fun mk_compN n bound_Ts (g, f) =
    56   let val typof = curry fastype_of1 bound_Ts in
    57     mk_partial_compN n (typof f) g $ f
    58   end;
    59 
    60 val mk_comp = mk_compN 1;
    61 
    62 fun get_indices fixes t = map (fst #>> Binding.name_of #> Free) fixes
    63   |> map_index (fn (i, v) => if exists_subterm (equal v) t then SOME i else NONE)
    64   |> map_filter I;
    65 
    66 end;