1 (* Title: HOL/BNF/Tools/bnf_fp_rec_sugar_util.ML
2 Author: Lorenz Panny, TU Muenchen
3 Author: Jasmin Blanchette, TU Muenchen
6 Library for recursor and corecursor sugar.
9 signature BNF_FP_REC_SUGAR_UTIL =
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
18 val drop_All: term -> term
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
25 val get_indices: ((binding * typ) * 'a) list -> term -> int list
28 structure BNF_FP_Rec_Sugar_Util : BNF_FP_REC_SUGAR_UTIL =
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;
36 fun find_index_eq hs h = find_index (curry (op =) h) hs;
38 fun finds eq = fold_map (fn x => List.partition (curry eq x) #>> pair x);
41 subst_bounds (strip_qnt_vars @{const_name all} t |> map Free |> rev,
42 strip_qnt_body @{const_name all} t);
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
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'
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
60 val mk_comp = mk_compN 1;
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)