val abstract_over_list: term list -> term -> term val abs_tuple_balanced: term list -> term -> term
val mk_conjs: term list -> term val mk_disjs: term list -> term val mk_dnf: term listlist -> term val conjuncts_s: term -> term list val s_not: term -> term val s_not_conj: term list -> term list val s_conjs: term list -> term val s_disjs: term list -> term val s_dnf: term listlist -> term list
val case_of: Proof.context -> string -> (string * bool) option val fold_rev_let_if_case: Proof.context -> (term list -> term -> 'a -> 'a) -> typ list ->
term -> 'a -> 'a val massage_let_if_case: Proof.context -> (term -> bool) -> (typ list -> term -> term) ->
(typ list -> term -> unit) -> (typ list -> term -> term) -> typ list -> term -> term val massage_nested_corec_call: Proof.context -> (term -> bool) ->
(typ list -> typ -> typ -> term -> term) -> (typ list -> typ -> typ -> term -> term) ->
typ list -> typ -> typ -> term -> term val expand_to_ctr_term: Proof.context -> typ -> term -> term val massage_corec_code_rhs: Proof.context -> (typ list -> term -> term list -> term) ->
typ list -> term -> term val fold_rev_corec_code_rhs: Proof.context -> (term list -> term -> term list -> 'a -> 'a) ->
typ list -> term -> 'a -> 'a val case_thms_of_term: Proof.context -> term ->
thm list * thm list * thm list * thm list * thm list val map_thms_of_type: Proof.context -> typ -> thm list
val corec_specs_of: binding list -> typ list -> typ list -> term list ->
(term * term listlist) listlist -> local_theory ->
corec_spec list * typ list * thm * thm * thm list * thm list * (Token.src list * Token.src list)
* bool * local_theory
val gfp_rec_sugar_interpretation: string ->
(BNF_FP_Rec_Sugar_Util.fp_rec_sugar -> local_theory -> local_theory) -> theory -> theory
val primcorec_ursive: bool -> bool -> corec_option list -> ((binding * typ) * mixfix) list ->
((binding * Token.T listlist) * term) list -> term optionlist -> Proof.context ->
(term * 'a list) list list * (thm list list -> local_theory -> local_theory) * local_theory val primcorec_ursive_cmd: bool -> bool -> corec_option list ->
(binding * stringoption * mixfix) list * ((Attrib.binding * string) * stringoption) list ->
Proof.context ->
(term * 'a list) list list * (thm list list -> local_theory -> local_theory) * local_theory val primcorecursive_cmd: bool -> corec_option list ->
(binding * stringoption * mixfix) list * ((Attrib.binding * string) * stringoption) list ->
Proof.context -> Proof.state val primcorec_cmd: bool -> corec_option list ->
(binding * stringoption * mixfix) list * ((Attrib.binding * string) * stringoption) list ->
local_theory -> local_theory end;
open Ctr_Sugar_General_Tactics open Ctr_Sugar open BNF_Util open BNF_Def open BNF_FP_Util open BNF_FP_Def_Sugar open BNF_FP_N2M_Sugar open BNF_FP_Rec_Sugar_Util open BNF_FP_Rec_Sugar_Transfer open BNF_GFP_Rec_Sugar_Tactics
val codeN = "code"; val ctrN = "ctr"; val discN = "disc"; val disc_iffN = "disc_iff"; val excludeN = "exclude"; val selN = "sel";
val nitpicksimp_attrs = @{attributes [nitpick_simp]}; val simp_attrs = @{attributes [simp]};
fun abstract_over_list rev_vs = let val vs = rev rev_vs;
fun abs n (t $ u) = abs n t $ abs n u
| abs n (Abs (s, T, t)) = Abs (s, T, abs (n + 1) t)
| abs n t = letval j = find_index (curry (op =) t) vs in if j < 0 then t else Bound (n + j) end; in
abs 0 end;
val abs_tuple_balanced = HOLogic.tupled_lambda o mk_tuple_balanced;
fun sort_list_duplicates xs = map snd (sort (int_ord o apply2 fst) xs);
val mk_conjs = try (foldr1 HOLogic.mk_conj) #> the_default \<^Const>\<open>True\<close>; val mk_disjs = try (foldr1 HOLogic.mk_disj) #> the_default \<^Const>\<open>False\<close>; val mk_dnf = mk_disjs o map mk_conjs;
val conjuncts_s = filter_out (curry (op aconv) \<^Const>\<open>True\<close>) o HOLogic.conjuncts;
fun s_not \<^Const_>\<open>True\<close> = \<^Const>\<open>False\<close>
| s_not \<^Const_>\<open>False\<close> = \<^Const>\<open>True\<close>
| s_not \<^Const_>\<open>Not for t\<close> = t
| s_not \<^Const_>\<open>conj for t u\<close> = \<^Const>\<open>disj for \<open>s_not t\<close> \<open>s_not u\<close>\<close>
| s_not \<^Const_>\<open>disj for t u\<close> = \<^Const>\<open>conj for \<open>s_not t\<close> \<open>s_not u\<close>\<close>
| s_not t = \<^Const>\<open>Not for t\<close>;
val s_not_conj = conjuncts_s o s_not o mk_conjs;
fun propagate_unit_pos u cs = if member (op aconv) cs u then [\<^Const>\<open>False\<close>] else cs; fun propagate_unit_neg not_u cs = remove (op aconv) not_u cs;
fun s_conjs cs = if member (op aconv) cs \<^Const>\<open>False\<close> then \<^Const>\<open>False\<close> else mk_conjs (remove (op aconv) \<^Const>\<open>True\<close> cs);
fun s_disjs ds = if member (op aconv) ds \<^Const>\<open>True\<close> then \<^Const>\<open>True\<close> else mk_disjs (remove (op aconv) \<^Const>\<open>False\<close> ds);
fun s_dnf css0 = letval css = propagate_units css0 in if null css then
[\<^Const>\<open>False\<close>] elseifexists null css then
[] else map (fn c :: cs => (c, cs)) css
|> AList.coalesce (op =)
|> map (fn (c, css) => c :: s_dnf css)
|> (fn [cs] => cs | css => [s_disjs (map s_conjs css)]) end;
fun fold_rev_let_if_case ctxt f bound_Ts = let val thy = Proof_Context.theory_of ctxt;
fun fld conds t =
(case Term.strip_comb t of
(Const (\<^const_name>\<open>Let\<close>, _), [_, _]) => fld conds (unfold_lets_splits t)
| (Const (\<^const_name>\<open>If\<close>, _), [cond, then_branch, else_branch]) =>
fld (conds @ conjuncts_s cond) then_branch o fld (conds @ s_not_conj [cond]) else_branch
| (Const (c, _), args as _ :: _ :: _) => letval n = num_binder_types (Sign.the_const_type thy c) - 1 in if n >= 0 andalso n < length args then
(case fastype_of1 (bound_Ts, nth args n) of Type (s, Ts) =>
(case dest_case ctxt s Ts t of
SOME ({split_sels = _ :: _, ...}, conds', branches) =>
fold_rev (uncurry fld) (map (append conds o conjuncts_s) conds' ~~ branches)
| _ => f conds t)
| _ => f conds t) else
f conds t end
| _ => f conds t); in
fld [] end;
fun case_of ctxt s =
(case ctr_sugar_of ctxt s of
SOME {casex = Const (s', _), split_sels, ...} => SOME (s', not (null split_sels))
| _ => NONE);
fun massage_let_if_case ctxt has_call massage_leaf unexpected_call unsupported_case bound_Ts t0 = let val thy = Proof_Context.theory_of ctxt;
fun check_no_call bound_Ts t = if has_call t then unexpected_call bound_Ts t else ();
fun massage_abs bound_Ts 0 t = massage_rec bound_Ts t
| massage_abs bound_Ts m (Abs (s, T, t)) = Abs (s, T, massage_abs (T :: bound_Ts) (m - 1) t)
| massage_abs bound_Ts m t = letval T = domain_type (fastype_of1 (bound_Ts, t)) in
Abs (Name.uu, T, massage_abs (T :: bound_Ts) (m - 1) (incr_boundvars 1 t $ Bound 0)) end and massage_rec bound_Ts t = letval typof = curry fastype_of1 bound_Ts in
(case Term.strip_comb t of
(Const (\<^const_name>\<open>Let\<close>, _), [_, _]) => massage_rec bound_Ts (unfold_lets_splits t)
| (Const (\<^const_name>\<open>If\<close>, _), obj :: (branches as [_, _])) =>
(caseList.partition Term.is_dummy_pattern (map (massage_rec bound_Ts) branches) of
(dummy_branch' :: _, []) => dummy_branch'
| (_, [branch']) => branch'
| (_, branches') =>
Term.list_comb (If_const (typof (hd branches')) $ tap (check_no_call bound_Ts) obj,
branches'))
| (c as Const (\<^const_name>\<open>case_prod\<close>, _), arg :: args) =>
massage_rec bound_Ts
(unfold_splits_lets (Term.list_comb (c $ Envir.eta_long bound_Ts arg, args)))
| (Const (c, _), args as _ :: _ :: _) =>
(casetry strip_fun_type (Sign.the_const_type thy c) of
SOME (gen_branch_Ts, gen_body_fun_T) => let val gen_branch_ms = map num_binder_types gen_branch_Ts; val n = length gen_branch_ms; in if n < length args then
(case gen_body_fun_T of Type (_, [Type (T_name, _), _]) =>
(case case_of ctxt T_name of
SOME (c', has_split_sels) => if c' = c then if has_split_sels then let val (branches, obj_leftovers) = chop n args; val branches' = map2 (massage_abs bound_Ts) gen_branch_ms branches; val branch_Ts' = map typof branches'; val body_T' = snd (strip_typeN (hd gen_branch_ms) (hd branch_Ts')); val casex' = Const (c, branch_Ts' ---> map typof obj_leftovers ---> body_T'); in
Term.list_comb (casex',
branches' @ tap (List.app (check_no_call bound_Ts)) obj_leftovers) end else
unsupported_case bound_Ts t else
massage_leaf bound_Ts t
| NONE => massage_leaf bound_Ts t)
| _ => massage_leaf bound_Ts t) else
massage_leaf bound_Ts t end
| NONE => massage_leaf bound_Ts t)
| _ => massage_leaf bound_Ts t) end; in
massage_rec bound_Ts t0
|> Term.map_aterms (fn t => if Term.is_dummy_pattern t thenConst (\<^const_name>\<open>undefined\<close>, fastype_of t) else t) end;
fun massage_nested_corec_call ctxt has_call massage_call massage_noncall bound_Ts U T t0 = let fun check_no_call t = if has_call t then unexpected_corec_call_in ctxt [t0] t else ();
fun massage_mutual_call bound_Ts (Type (\<^type_name>\<open>fun\<close>, [_, U2]))
(Type (\<^type_name>\<open>fun\<close>, [T1, T2])) t =
Abs (Name.uu, T1, massage_mutual_call (T1 :: bound_Ts) U2 T2 (incr_boundvars 1 t $ Bound 0))
| massage_mutual_call bound_Ts U T t =
(if has_call t then massage_call else massage_noncall) bound_Ts U T t;
fun massage_map bound_Ts (Type (_, Us)) (Type (s, Ts)) t =
(casetry (dest_map ctxt s) t of
SOME (map0, fs) => let valType (_, dom_Ts) = domain_type (fastype_of1 (bound_Ts, t)); valmap' = mk_map (length fs) dom_Ts Us map0; val fs' =
map_flattened_map_args ctxt s (@{map 3} (massage_map_or_map_arg bound_Ts) Us Ts) fs; in
Term.list_comb (map', fs') end
| NONE => raise NO_MAP t)
| massage_map _ _ _ t = raise NO_MAP t and massage_map_or_map_arg bound_Ts U T t = if T = U then
tap check_no_call t else
massage_map bound_Ts U T t handle NO_MAP _ => massage_mutual_fun bound_Ts U T t and massage_mutual_fun bound_Ts U T t = let val j = Term.maxidx_of_term t + 1; val var = Var ((Name.uu, j), domain_type (fastype_of1 (bound_Ts, t)));
fun massage_body () =
Term.lambda var (Term.incr_boundvars 1 (massage_any_call bound_Ts U T
(betapply (t, var)))); in
(case t of Const (\<^const_name>\<open>comp\<close>, _) $ t1 $ t2 => if has_call t2 then massage_body () else mk_comp bound_Ts (massage_mutual_fun bound_Ts U T t1, t2)
| _ => massage_body ()) end and massage_any_call bound_Ts U T =
massage_let_if_case_corec ctxt has_call (fn bound_Ts => fn t => if has_call t then
(case U of Type (s, Us) =>
(casetry (dest_ctr ctxt s) t of
SOME (f, args) => let val typof = curry fastype_of1 bound_Ts; val f' = mk_ctr Us f val f'_T = typof f'; val arg_Ts = map typof args; in
Term.list_comb (f',
@{map 3} (massage_any_call bound_Ts) (binder_types f'_T) arg_Ts args) end
| NONE =>
(case t of Const (\<^const_name>\<open>case_prod\<close>, _) $ t' => let val U' = curried_type U; val T' = curried_type T; in Const (\<^const_name>\<open>case_prod\<close>, U' --> U) $ massage_any_call bound_Ts U' T' t' end
| t1 $ t2 =>
(if has_call t2 then
massage_mutual_call bound_Ts U T t else
massage_map bound_Ts U T t1 $ t2 handle NO_MAP _ => massage_mutual_call bound_Ts U T t)
| Abs (s, T', t') =>
Abs (s, T', massage_any_call (T' :: bound_Ts) (range_type U) (range_type T) t')
| _ => massage_mutual_call bound_Ts U T t))
| _ => ill_formed_corec_call ctxt t) else
massage_noncall bound_Ts U T t) bound_Ts; in
(if has_call t0 then massage_any_call else massage_noncall) bound_Ts U T t0 end;
fun expand_to_ctr_term ctxt (T as Type (s, Ts)) t =
(case ctr_sugar_of ctxt s of
SOME {ctrs, casex, ...} => Term.list_comb (mk_case Ts T casex, map (mk_ctr Ts) ctrs) $ t
| NONE => raise Fail "expand_to_ctr_term");
fun expand_corec_code_rhs ctxt has_call bound_Ts t =
(case fastype_of1 (bound_Ts, t) of
T as Type (s, _) =>
massage_let_if_case_corec ctxt has_call (fn _ => fn t => if can (dest_ctr ctxt s) t then t else expand_to_ctr_term ctxt T t) bound_Ts t
| _ => raise Fail "expand_corec_code_rhs");
fun massage_corec_code_rhs ctxt massage_ctr =
massage_let_if_case_corec ctxt (K false)
(fn bound_Ts => uncurry (massage_ctr bound_Ts) o Term.strip_comb);
fun fold_rev_corec_code_rhs ctxt f =
fold_rev_let_if_case ctxt (fn conds => uncurry (f conds) o Term.strip_comb);
fun case_thms_of_term ctxt t = letval ctr_sugars = map_filter (Ctr_Sugar.ctr_sugar_of_case ctxt o fst) (Term.add_consts t []) in
(maps #distincts ctr_sugars, maps #discIs ctr_sugars, maps #exhaust_discs ctr_sugars,
maps #split_sels ctr_sugars, maps #split_sel_asms ctr_sugars) end;
fun basic_corec_specs_of ctxt res_T =
(case res_T of Type (T_name, _) =>
(case Ctr_Sugar.ctr_sugar_of ctxt T_name of
NONE => not_codatatype ctxt res_T
| SOME {T = fpT, ctrs, discs, selss, ...} => let val thy = Proof_Context.theory_of ctxt;
val As_rho = tvar_subst thy [fpT] [res_T]; val substA = Term.subst_TVars As_rho;
fun mk_spec ctr disc sels = {ctr = substA ctr, disc = substA disc, sels = map substA sels}; in
@{map 3} mk_spec ctrs discs selss handle ListPair.UnequalLengths => not_codatatype ctxt res_T end)
| _ => not_codatatype ctxt res_T);
fun map_thms_of_type ctxt (Type (s, _)) =
(case fp_sugar_of ctxt s of SOME {fp_bnf_sugar = {map_thms, ...}, ...} => map_thms | NONE => [])
| map_thms_of_type _ _ = [];
structure GFP_Rec_Sugar_Plugin = Plugin(type T = fp_rec_sugar);
fun gfp_rec_sugar_interpretation name f =
GFP_Rec_Sugar_Plugin.interpretation name (fn fp_rec_sugar => fn lthy =>
f (transfer_fp_rec_sugar (Proof_Context.theory_of lthy) fp_rec_sugar) lthy);
val interpret_gfp_rec_sugar = GFP_Rec_Sugar_Plugin.data;
fun corec_specs_of bs arg_Ts res_Ts callers callssss0 lthy0 = let val thy = Proof_Context.theory_of lthy0;
val ((missing_res_Ts, perm0_kks, fp_sugars as {fp_nesting_bnfs,
fp_co_induct_sugar = SOME {common_co_inducts = common_coinduct_thms, ...}, ...} :: _,
(_, gfp_sugar_thms)), lthy) =
nested_to_mutual_fps (K true) Greatest_FP bs res_Ts callers callssss0 lthy0;
val coinduct_attrs_pair =
(case gfp_sugar_thms of SOME ((_, attrs_pair), _, _, _, _) => attrs_pair | NONE => ([], []));
val perm_fp_sugars = sort (int_ord o apply2 #fp_res_index) fp_sugars;
val indices = map #fp_res_index fp_sugars; val perm_indices = map #fp_res_index perm_fp_sugars;
val perm_fpTs = map #T perm_fp_sugars; val perm_ctrXs_Tsss' = map (repair_nullary_single_ctr o #ctrXs_Tss o #fp_ctr_sugar) perm_fp_sugars;
val nn0 = length res_Ts; val nn = length perm_fpTs; val kks = 0 upto nn - 1; val perm_ns' = map length perm_ctrXs_Tsss';
val perm_Ts = map #T perm_fp_sugars; val perm_Xs = map #X perm_fp_sugars; val perm_Cs = map (domain_type o body_fun_type o fastype_of o #co_rec o the o #fp_co_induct_sugar)
perm_fp_sugars; val Xs_TCs = perm_Xs ~~ (perm_Ts ~~ perm_Cs);
fun zip_corecT (Type (s, Us)) = [Type (s, map (mk_sumTN o zip_corecT) Us)]
| zip_corecT U =
(case AList.lookup (op =) Xs_TCs U of
SOME (T, C) => [T, C]
| NONE => [U]);
val perm_p_Tss = mk_corec_p_pred_types perm_Cs perm_ns'; val perm_f_Tssss =
map2 (fn C => map (map (map (curry (op -->) C) o zip_corecT))) perm_Cs perm_ctrXs_Tsss'; val perm_q_Tssss = map (map (map (fn [_] => [] | [_, T] => [mk_pred1T (domain_type T)]))) perm_f_Tssss;
val (perm_p_hss, h) = indexedd perm_p_Tss 0; val (perm_q_hssss, h') = indexedddd perm_q_Tssss h; val (perm_f_hssss, _) = indexedddd perm_f_Tssss h';
val fun_arg_hs =
flat (@{map 3} flat_corec_preds_predsss_gettersss perm_p_hss perm_q_hssss perm_f_hssss);
fun unpermute0 perm0_xs = permute_like_unique (op =) perm0_kks kks perm0_xs; fun unpermute perm_xs = permute_like_unique (op =) perm_indices indices perm_xs;
val coinduct_thmss = map (unpermute0 o conj_dests nn) common_coinduct_thms;
fun dissect_coeqn_disc ctxt fun_names sequentials
(basic_ctr_specss : basic_corec_ctr_spec listlist) eqn_pos ctr_rhs_opt code_rhs_opt prems0
concl matchedsss = let fun find_subterm p = let(* FIXME \<exists>? *) funfind (t as u $ v) = if p t then SOME t else merge_options (find u, find v)
| find t = if p t then SOME t else NONE; infindend;
val applied_fun = concl
|> find_subterm (member (op = o apsnd SOME) fun_names o try (fst o dest_Free o head_of))
|> the handleOption.Option => error_at ctxt [concl] "Ill-formed discriminator formula"; val ((fun_name, fun_T), fun_args) = strip_comb applied_fun |>> dest_Free;
val _ = check_fun_args ctxt concl fun_args;
val bads = filter (Term.exists_subterm (is_free_in fun_names)) prems0; val _ = null bads orelse unexpected_rec_call_in ctxt [] (hd bads);
val (sequential, basic_ctr_specs) =
the (AList.lookup (op =) (fun_names ~~ (sequentials ~~ basic_ctr_specss)) fun_name);
val discs = map #disc basic_ctr_specs; val ctrs = map #ctr basic_ctr_specs; val not_disc = head_of concl = \<^term>\<open>Not\<close>; val _ = not_disc andalso length ctrs <> 2 andalso
error_at ctxt [concl] "Negated discriminator for a type with \ 2 constructors"; val disc' = find_subterm (member (op =) discs o head_of) concl; val eq_ctr0 = concl |> perhaps (try HOLogic.dest_not) |> try (HOLogic.dest_eq #> snd)
|> (fn SOME t => letval n = find_index (curry (op =) t) ctrs in if n >= 0 then SOME n else NONE end | _ => NONE);
val _ = is_none disc' orelse perhaps (try HOLogic.dest_not) concl = the disc' orelse
error_at ctxt [concl] "Ill-formed discriminator formula"; val _ = is_some disc' orelse is_some eq_ctr0 orelse
error_at ctxt [concl] "No discriminator in equation";
val ctr_no' = if is_none disc' then the eq_ctr0 else find_index (curry (op =) (head_of (the disc'))) discs; val ctr_no = if not_disc then 1 - ctr_no' else ctr_no'; val {ctr, disc, ...} = nth basic_ctr_specs ctr_no;
val catch_all =
(case prems0 of
[prem] => is_catch_all_prem prem
| _ => ifexists is_catch_all_prem prems0 then error_at ctxt [concl] "Superfluous premises" elsefalse); val matchedss = AList.lookup (op =) matchedsss fun_name |> the_default []; val prems = map (abstract_over_list fun_args) prems0; val actual_prems =
(if catch_all orelse sequential then maps s_not_conj matchedss else []) @
(if catch_all then [] else prems);
val matchedsss' = AList.delete (op =) fun_name matchedsss
|> cons (fun_name, if sequential then matchedss @ [prems] else matchedss @ [actual_prems]);
val user_eqn =
(actual_prems, concl)
|>> map HOLogic.mk_Trueprop ||> HOLogic.mk_Trueprop o abstract_over_list fun_args
|> curry Logic.list_all (map dest_Free fun_args) o Logic.list_implies;
fun check_num_args () =
is_none rhs_opt orelse not (can dest_funT (fastype_of (the rhs_opt))) orelse
missing_args_to_fun_on_lhs ctxt [eqn];
val discs = maps (map #disc) basic_ctr_specss; val sels = maps (maps #sels) basic_ctr_specss; val ctrs = maps (map #ctr) basic_ctr_specss; in if member (op =) discs head orelse
(is_some rhs_opt andalso
member (op =) (map SOME fun_names) (try (fst o dest_Free) head) andalso
member (op =) (filter (null o binder_types o fastype_of) ctrs) (the rhs_opt)) then
(dissect_coeqn_disc ctxt fun_names sequentials basic_ctr_specss eqn_pos NONE NONE prems concl
matchedsss
|>> single) elseif member (op =) sels head then
(null prems orelse error_at ctxt [eqn] "Unexpected condition in selector formula";
([dissect_coeqn_sel ctxt fun_names basic_ctr_specss eqn_pos NONE NONE eqn0 of_spec_opt
concl], matchedsss)) elseif is_some rhs_opt andalso is_Free head andalso is_free_in fun_names head then if member (op =) ctrs (head_of (unfold_lets_splits (the rhs_opt))) then
(check_num_args ();
dissect_coeqn_ctr ctxt fun_names sequentials basic_ctr_specss eqn_pos eqn0
(if null prems then
SOME (snd (HOLogic.dest_eq (HOLogic.dest_Trueprop (Logic.strip_assums_concl eqn0)))) else
NONE)
prems concl matchedsss) elseif null prems then
(check_num_args ();
dissect_coeqn_code ctxt has_call fun_names basic_ctr_specss eqn_pos eqn0 concl matchedsss
|>> flat) else
error_at ctxt [eqn] "Cannot mix constructor and code views" elseif is_some rhs_opt then
error_at ctxt [eqn] ("Ill-formed equation head: " ^ quote (Syntax.string_of_term ctxt head)) else
error_at ctxt [eqn] "Expected equation or discriminator formula" end;
fun build_corec_arg_disc (ctr_specs : corec_ctr_spec list)
({fun_args, ctr_no, prems, ...} : coeqn_data_disc) = if is_none (#pred (nth ctr_specs ctr_no)) then
I else
s_conjs prems
|> curry subst_bounds (List.rev fun_args)
|> abs_tuple_balanced fun_args
|> K |> nth_map (the (#pred (nth ctr_specs ctr_no)));
fun build_corec_arg_no_call (sel_eqns : coeqn_data_sel list) sel =
find_first (curry (op =) sel o #sel) sel_eqns
|> try (fn SOME {fun_args, rhs_term, ...} => abs_tuple_balanced fun_args rhs_term)
|> the_default undef_const
|> K;
fun build_corec_args_mutual_call ctxt has_call (sel_eqns : coeqn_data_sel list) sel =
(case find_first (curry (op =) sel o #sel) sel_eqns of
NONE => (I, I, I)
| SOME {fun_args, rhs_term, ... } => let val bound_Ts = List.rev (map fastype_of fun_args);
fun rewrite_stop _ t = if has_call t then \<^term>\<open>False\<close> else \<^term>\<open>True\<close>; fun rewrite_end _ t = if has_call t then undef_const else t; fun rewrite_cont bound_Ts t = if has_call t then mk_tuple1_balanced bound_Ts (snd (strip_comb t)) else undef_const; fun massage f _ = massage_let_if_case_corec ctxt has_call f bound_Ts rhs_term
|> abs_tuple_balanced fun_args; in
(massage rewrite_stop, massage rewrite_end, massage rewrite_cont) end);
fun build_corec_arg_nested_call ctxt has_call (sel_eqns : coeqn_data_sel list) sel =
(case find_first (curry (op =) sel o #sel) sel_eqns of
NONE => I
| SOME {fun_args, rhs_term, ...} => let fun massage_call bound_Ts U T t0 = let val U2 =
(casetry dest_sumT U of
SOME (U1, U2) => if U1 = T then U2 else invalid_map ctxt [] t0
| NONE => invalid_map ctxt [] t0);
fun rewrite bound_Ts (Abs (s, T', t')) = Abs (s, T', rewrite (T' :: bound_Ts) t')
| rewrite bound_Ts (t as _ $ _) = letval (u, vs) = strip_comb t in if is_Free u andalso has_call u then
Inr_const T U2 $ mk_tuple1_balanced bound_Ts vs elseiftry dest_Const_name u = SOME \<^const_name>\<open>case_prod\<close> then map (rewrite bound_Ts) vs |> chop 1
|>> HOLogic.mk_case_prod o the_single
|> Term.list_comb else
Term.list_comb (rewrite bound_Ts u, map (rewrite bound_Ts) vs) end
| rewrite _ t = if is_Free t andalso has_call t then Inr_const T U2 $ HOLogic.unit else t; in
rewrite bound_Ts t0 end;
fun massage_noncall U T t =
build_map ctxt [] [] (uncurry Inl_const o dest_sumT o snd) (T, U) $ t;
val bound_Ts = List.rev (map fastype_of fun_args); in
fn t =>
rhs_term
|> massage_nested_corec_call ctxt has_call massage_call (K massage_noncall) bound_Ts
(range_type (fastype_of t)) (fastype_of1 (bound_Ts, rhs_term))
|> abs_tuple_balanced fun_args end);
fun build_corec_args_sel ctxt has_call (all_sel_eqns : coeqn_data_sel list)
(ctr_spec : corec_ctr_spec) =
(casefilter (curry (op =) (#ctr ctr_spec) o #ctr) all_sel_eqns of
[] => I
| sel_eqns => let val sel_call_list = #sels ctr_spec ~~ #calls ctr_spec; val no_calls' = map_filter (try (apsnd (fn No_Corec n => n))) sel_call_list; val mutual_calls' = map_filter (try (apsnd (fn Mutual_Corec n => n))) sel_call_list; val nested_calls' = map_filter (try (apsnd (fn Nested_Corec n => n))) sel_call_list; in
I
#> fold (fn (sel, n) => nth_map n (build_corec_arg_no_call sel_eqns sel)) no_calls'
#> fold (fn (sel, (q, g, h)) => letval (fq, fg, fh) = build_corec_args_mutual_call ctxt has_call sel_eqns sel in
nth_map q fq o nth_map g fg o nth_map h fh end) mutual_calls'
#> fold (fn (sel, n) => nth_map n
(build_corec_arg_nested_call ctxt has_call sel_eqns sel)) nested_calls' end);
fun build_defs ctxt bs mxs has_call arg_Tss (corec_specs : corec_spec list)
(disc_eqnss : coeqn_data_disc listlist) (sel_eqnss : coeqn_data_sel listlist) = let val corecs = map #corec corec_specs; val ctr_specss = map #ctr_specs corec_specs; val corec_args = hd corecs
|> fst o split_last o binder_types o fastype_of
|> map (fn T => if range_type T = HOLogic.boolT then Abs (Name.uu_, domain_type T, \<^term>\<open>False\<close>) elseConst (\<^const_name>\<open>undefined\<close>, T))
|> fold2 (fold o build_corec_arg_disc) ctr_specss disc_eqnss
|> fold2 (fold o build_corec_args_sel ctxt has_call) sel_eqnss ctr_specss;
val bad = fold (add_extra_frees ctxt [] []) corec_args []; val _ = null bad orelse
(ifexists has_call corec_args then nonprimitive_corec ctxt [] else extra_variable_in_rhs ctxt [] (hd bad));
fun primcorec_ursive int auto opts fixes specs of_specs_opt lthy = let val (bs, mxs) = map_split (apfst fst) fixes; val (arg_Ts, res_Ts) = map (strip_type o snd o fst #>> mk_tupleT_balanced) fixes |> split_list; val primcorec_types = map dest_Type_name res_Ts;
val _ = check_duplicate_const_names bs; val _ = List.app (uncurry (check_top_sort lthy)) (bs ~~ arg_Ts);
val actual_nn = length bs;
val plugins = get_first (fn Plugins_Option f => SOME (f lthy) | _ => NONE) (rev opts)
|> the_default Plugin_Name.default_filter; val sequentials = replicate actual_nn (exists (can (fn Sequential_Option => ())) opts); val exhaustives = replicate actual_nn (exists (can (fn Exhaustive_Option => ())) opts); val transfers = replicate actual_nn (exists (can (fn Transfer_Option => ())) opts);
val fun_names = map Binding.name_of bs; val qualifys = map (fold_rev (uncurry Binding.qualify o swap) o Binding.path_of) bs; val basic_ctr_specss = map (basic_corec_specs_of lthy) res_Ts; val frees = map (fst #>> Binding.name_of #> Free) fixes; val has_call = Term.exists_subterm (member (op =) frees); val eqns_data =
@{fold_map 2} (dissect_coeqn lthy has_call fun_names sequentials basic_ctr_specss)
(tag_list 0 (map snd specs)) of_specs_opt []
|> flat o fst;
val missing = fun_names
|> filter (map (fn Disc x => #fun_name x | Sel x => #fun_name x) eqns_data
|> not oo member (op =)); val _ = null missing orelse missing_equations_for_const (hd missing);
val callssss =
map_filter (try (fn Sel x => x)) eqns_data
|> partition_eq (op = o apply2 #fun_name)
|> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names
|> map (flat o snd)
|> map2 (fold o find_corec_calls lthy has_call) basic_ctr_specss
|> map2 (curry (op |>)) (map (map (fn {ctr, sels, ...} =>
(ctr, map (K []) sels))) basic_ctr_specss);
val (corec_specs0, _, coinduct_thm, coinduct_strong_thm, coinduct_thms, coinduct_strong_thms,
(coinduct_attrs, common_coinduct_attrs), n2m, lthy) =
corec_specs_of bs arg_Ts res_Ts frees callssss lthy; val corec_specs = take actual_nn corec_specs0; val ctr_specss = map #ctr_specs corec_specs;
val disc_eqnss0 = map_filter (try (fn Disc x => x)) eqns_data
|> partition_eq (op = o apply2 #fun_name)
|> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names
|> map (sort (op < o apply2 #ctr_no |> make_ord) o flat o snd);
val _ = disc_eqnss0 |> map (fn x => letval dups = duplicates (op = o apply2 #ctr_no) x in
null dups orelse
error_at lthy
(maps (fn t => filter (curry (op =) (#ctr_no t) o #ctr_no) x) dups
|> map (fn {ctr_rhs_opt = SOME t, ...} => t | {user_eqn, ...} => user_eqn)) "Overspecified case(s)" end);
val sel_eqnss = map_filter (try (fn Sel x => x)) eqns_data
|> partition_eq (op = o apply2 #fun_name)
|> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names
|> map (flat o snd);
val _ = sel_eqnss |> map (fn x => letval dups = duplicates (op = o apply2 ctr_sel_of) x in
null dups orelse
error_at lthy
(maps (fn t => filter (curry (op =) (ctr_sel_of t) o ctr_sel_of) x) dups
|> map (fn {ctr_rhs_opt = SOME t, ...} => t | {user_eqn, ...} => user_eqn)) "Overspecified case(s)" end);
val arg_Tss = map (binder_types o snd o fst) fixes; val disc_eqnss = @{map 6} mk_actual_disc_eqns bs arg_Tss exhaustives corec_specs sel_eqnss
disc_eqnss0; val (defs, excludess') =
build_defs lthy bs mxs has_call arg_Tss corec_specs disc_eqnss sel_eqnss;
val tac_opts = map (fn {code_rhs_opt, ...} :: _ => if auto orelse is_some code_rhs_opt then SOME (auto_tac o #context) else NONE) disc_eqnss;
fun exclude_tac tac_opt sequential (c, c', a) = if a orelse c = c' orelse sequential then
SOME (fn {context = ctxt, prems = _} => HEADGOAL (mk_primcorec_assumption_tac ctxt [])) else
tac_opt;
val taut_thmss = map (map (apsnd (the o fst)) o filter (is_some o fst o snd)) excludess''; val (goal_idxss, exclude_goalss) = excludess''
|> map (map (apsnd (rpair [] o snd)) o filter (is_none o fst o snd))
|> split_list o map split_list;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.