457 |
457 |
458 val abs_tuple = HOLogic.tupled_lambda o HOLogic.mk_tuple; |
458 val abs_tuple = HOLogic.tupled_lambda o HOLogic.mk_tuple; |
459 fun abstract vs = |
459 fun abstract vs = |
460 let fun a n (t $ u) = a n t $ a n u |
460 let fun a n (t $ u) = a n t $ a n u |
461 | a n (Abs (v, T, b)) = Abs (v, T, a (n + 1) b) |
461 | a n (Abs (v, T, b)) = Abs (v, T, a (n + 1) b) |
462 | a n t = let val idx = find_index (equal t) vs in |
462 | a n t = let val idx = find_index (curry (op =) t) vs in |
463 if idx < 0 then t else Bound (n + idx) end |
463 if idx < 0 then t else Bound (n + idx) end |
464 in a 0 end; |
464 in a 0 end; |
465 |
465 |
466 fun mk_prod1 bound_Ts (t, u) = |
466 fun mk_prod1 bound_Ts (t, u) = |
467 HOLogic.pair_const (fastype_of1 (bound_Ts, t)) (fastype_of1 (bound_Ts, u)) $ t $ u; |
467 HOLogic.pair_const (fastype_of1 (bound_Ts, t)) (fastype_of1 (bound_Ts, u)) $ t $ u; |
519 val not_disc = head_of concl = @{term Not}; |
519 val not_disc = head_of concl = @{term Not}; |
520 val _ = not_disc andalso length ctrs <> 2 andalso |
520 val _ = not_disc andalso length ctrs <> 2 andalso |
521 primcorec_error_eqn "negated discriminator for a type with \<noteq> 2 constructors" concl; |
521 primcorec_error_eqn "negated discriminator for a type with \<noteq> 2 constructors" concl; |
522 val disc' = find_subterm (member (op =) discs o head_of) concl; |
522 val disc' = find_subterm (member (op =) discs o head_of) concl; |
523 val eq_ctr0 = concl |> perhaps (try HOLogic.dest_not) |> try (HOLogic.dest_eq #> snd) |
523 val eq_ctr0 = concl |> perhaps (try HOLogic.dest_not) |> try (HOLogic.dest_eq #> snd) |
524 |> (fn SOME t => let val n = find_index (equal t) ctrs in |
524 |> (fn SOME t => let val n = find_index (curry (op =) t) ctrs in |
525 if n >= 0 then SOME n else NONE end | _ => NONE); |
525 if n >= 0 then SOME n else NONE end | _ => NONE); |
526 val _ = is_some disc' orelse is_some eq_ctr0 orelse |
526 val _ = is_some disc' orelse is_some eq_ctr0 orelse |
527 primcorec_error_eqn "no discriminator in equation" concl; |
527 primcorec_error_eqn "no discriminator in equation" concl; |
528 val ctr_no' = |
528 val ctr_no' = |
529 if is_none disc' then the eq_ctr0 else find_index (equal (head_of (the disc'))) discs; |
529 if is_none disc' then the eq_ctr0 else find_index (curry (op =) (head_of (the disc'))) discs; |
530 val ctr_no = if not_disc then 1 - ctr_no' else ctr_no'; |
530 val ctr_no = if not_disc then 1 - ctr_no' else ctr_no'; |
531 val {ctr, disc, ...} = nth basic_ctr_specs ctr_no; |
531 val {ctr, disc, ...} = nth basic_ctr_specs ctr_no; |
532 |
532 |
533 val catch_all = try (fst o dest_Free o the_single) prems' = SOME Name.uu_; |
533 val catch_all = try (fst o dest_Free o the_single) prems' = SOME Name.uu_; |
534 val matchedss = AList.lookup (op =) matchedsss fun_name |> the_default []; |
534 val matchedss = AList.lookup (op =) matchedsss fun_name |> the_default []; |
573 val basic_ctr_specs = the (AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name) |
573 val basic_ctr_specs = the (AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name) |
574 handle Option.Option => |
574 handle Option.Option => |
575 primcorec_error_eqn "malformed selector argument in left-hand side" eqn; |
575 primcorec_error_eqn "malformed selector argument in left-hand side" eqn; |
576 val {ctr, ...} = |
576 val {ctr, ...} = |
577 (case maybe_of_spec of |
577 (case maybe_of_spec of |
578 SOME of_spec => the (find_first (equal of_spec o #ctr) basic_ctr_specs) |
578 SOME of_spec => the (find_first (curry (op =) of_spec o #ctr) basic_ctr_specs) |
579 | NONE => filter (exists (equal sel) o #sels) basic_ctr_specs |> the_single |
579 | NONE => filter (exists (curry (op =) sel) o #sels) basic_ctr_specs |> the_single |
580 handle List.Empty => primcorec_error_eqn "ambiguous selector - use \"of\"" eqn); |
580 handle List.Empty => primcorec_error_eqn "ambiguous selector - use \"of\"" eqn); |
581 val user_eqn = drop_All eqn'; |
581 val user_eqn = drop_All eqn'; |
582 in |
582 in |
583 Sel { |
583 Sel { |
584 fun_name = fun_name, |
584 fun_name = fun_name, |
598 let |
598 let |
599 val (lhs, rhs) = HOLogic.dest_eq concl; |
599 val (lhs, rhs) = HOLogic.dest_eq concl; |
600 val (fun_name, fun_args) = strip_comb lhs |>> fst o dest_Free; |
600 val (fun_name, fun_args) = strip_comb lhs |>> fst o dest_Free; |
601 val SOME basic_ctr_specs = AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name; |
601 val SOME basic_ctr_specs = AList.lookup (op =) (fun_names ~~ basic_ctr_specss) fun_name; |
602 val (ctr, ctr_args) = strip_comb (unfold_let rhs); |
602 val (ctr, ctr_args) = strip_comb (unfold_let rhs); |
603 val {disc, sels, ...} = the (find_first (equal ctr o #ctr) basic_ctr_specs) |
603 val {disc, sels, ...} = the (find_first (curry (op =) ctr o #ctr) basic_ctr_specs) |
604 handle Option.Option => primcorec_error_eqn "not a constructor" ctr; |
604 handle Option.Option => primcorec_error_eqn "not a constructor" ctr; |
605 |
605 |
606 val disc_concl = betapply (disc, lhs); |
606 val disc_concl = betapply (disc, lhs); |
607 val (maybe_eqn_data_disc, matchedsss') = if length basic_ctr_specs = 1 |
607 val (maybe_eqn_data_disc, matchedsss') = if length basic_ctr_specs = 1 |
608 then (NONE, matchedsss) |
608 then (NONE, matchedsss) |
698 |> curry subst_bounds (List.rev fun_args) |
698 |> curry subst_bounds (List.rev fun_args) |
699 |> HOLogic.tupled_lambda (HOLogic.mk_tuple fun_args) |
699 |> HOLogic.tupled_lambda (HOLogic.mk_tuple fun_args) |
700 |> K |> nth_map (the (#pred (nth ctr_specs ctr_no))); |
700 |> K |> nth_map (the (#pred (nth ctr_specs ctr_no))); |
701 |
701 |
702 fun build_corec_arg_no_call (sel_eqns : coeqn_data_sel list) sel = |
702 fun build_corec_arg_no_call (sel_eqns : coeqn_data_sel list) sel = |
703 find_first (equal sel o #sel) sel_eqns |
703 find_first (curry (op =) sel o #sel) sel_eqns |
704 |> try (fn SOME {fun_args, rhs_term, ...} => abs_tuple fun_args rhs_term) |
704 |> try (fn SOME {fun_args, rhs_term, ...} => abs_tuple fun_args rhs_term) |
705 |> the_default undef_const |
705 |> the_default undef_const |
706 |> K; |
706 |> K; |
707 |
707 |
708 fun build_corec_args_mutual_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
708 fun build_corec_args_mutual_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
709 (case find_first (equal sel o #sel) sel_eqns of |
709 (case find_first (curry (op =) sel o #sel) sel_eqns of |
710 NONE => (I, I, I) |
710 NONE => (I, I, I) |
711 | SOME {fun_args, rhs_term, ... } => |
711 | SOME {fun_args, rhs_term, ... } => |
712 let |
712 let |
713 val bound_Ts = List.rev (map fastype_of fun_args); |
713 val bound_Ts = List.rev (map fastype_of fun_args); |
714 fun rewrite_stop _ t = if has_call t then @{term False} else @{term True}; |
714 fun rewrite_stop _ t = if has_call t then @{term False} else @{term True}; |
720 in |
720 in |
721 (massage rewrite_stop, massage rewrite_end, massage rewrite_cont) |
721 (massage rewrite_stop, massage rewrite_end, massage rewrite_cont) |
722 end); |
722 end); |
723 |
723 |
724 fun build_corec_arg_nested_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
724 fun build_corec_arg_nested_call lthy has_call (sel_eqns : coeqn_data_sel list) sel = |
725 (case find_first (equal sel o #sel) sel_eqns of |
725 (case find_first (curry (op =) sel o #sel) sel_eqns of |
726 NONE => I |
726 NONE => I |
727 | SOME {fun_args, rhs_term, ...} => |
727 | SOME {fun_args, rhs_term, ...} => |
728 let |
728 let |
729 val bound_Ts = List.rev (map fastype_of fun_args); |
729 val bound_Ts = List.rev (map fastype_of fun_args); |
730 fun rewrite bound_Ts U T (Abs (v, V, b)) = Abs (v, V, rewrite (V :: bound_Ts) U T b) |
730 fun rewrite bound_Ts U T (Abs (v, V, b)) = Abs (v, V, rewrite (V :: bound_Ts) U T b) |
814 if (exhaustive andalso num_disc_eqns <> 0) orelse num_disc_eqns <> length ctr_specs - 1 then |
814 if (exhaustive andalso num_disc_eqns <> 0) orelse num_disc_eqns <> length ctr_specs - 1 then |
815 disc_eqns |
815 disc_eqns |
816 else |
816 else |
817 let |
817 let |
818 val n = 0 upto length ctr_specs |
818 val n = 0 upto length ctr_specs |
819 |> the o find_first (fn idx => not (exists (equal idx o #ctr_no) disc_eqns)); |
819 |> the o find_first (fn idx => not (exists (curry (op =) idx o #ctr_no) disc_eqns)); |
820 val fun_args = (try (#fun_args o hd) disc_eqns, try (#fun_args o hd) sel_eqns) |
820 val fun_args = (try (#fun_args o hd) disc_eqns, try (#fun_args o hd) sel_eqns) |
821 |> the_default (map (curry Free Name.uu) arg_Ts) o merge_options; |
821 |> the_default (map (curry Free Name.uu) arg_Ts) o merge_options; |
822 val maybe_sel_eqn = find_first (equal (Binding.name_of fun_binding) o #fun_name) sel_eqns; |
822 val maybe_sel_eqn = |
|
823 find_first (curry (op =) (Binding.name_of fun_binding) o #fun_name) sel_eqns; |
823 val extra_disc_eqn = { |
824 val extra_disc_eqn = { |
824 fun_name = Binding.name_of fun_binding, |
825 fun_name = Binding.name_of fun_binding, |
825 fun_T = arg_Ts ---> body_type (fastype_of (#ctr (hd ctr_specs))), |
826 fun_T = arg_Ts ---> body_type (fastype_of (#ctr (hd ctr_specs))), |
826 fun_args = fun_args, |
827 fun_args = fun_args, |
827 ctr = #ctr (nth ctr_specs n), |
828 ctr = #ctr (nth ctr_specs n), |
837 end |
838 end |
838 end; |
839 end; |
839 |
840 |
840 fun find_corec_calls ctxt has_call basic_ctr_specs ({ctr, sel, rhs_term, ...} : coeqn_data_sel) = |
841 fun find_corec_calls ctxt has_call basic_ctr_specs ({ctr, sel, rhs_term, ...} : coeqn_data_sel) = |
841 let |
842 let |
842 val sel_no = find_first (equal ctr o #ctr) basic_ctr_specs |
843 val sel_no = find_first (curry (op =) ctr o #ctr) basic_ctr_specs |
843 |> find_index (equal sel) o #sels o the; |
844 |> find_index (curry (op =) sel) o #sels o the; |
844 fun find t = if has_call t then snd (fold_rev_corec_call ctxt (K cons) [] t []) else []; |
845 fun find t = if has_call t then snd (fold_rev_corec_call ctxt (K cons) [] t []) else []; |
845 in |
846 in |
846 find rhs_term |
847 find rhs_term |
847 |> K |> nth_map sel_no |> AList.map_entry (op =) ctr |
848 |> K |> nth_map sel_no |> AList.map_entry (op =) ctr |
848 end; |
849 end; |
902 |> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
903 |> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
903 |> map (sort ((op <) o pairself #ctr_no |> make_ord) o flat o snd); |
904 |> map (sort ((op <) o pairself #ctr_no |> make_ord) o flat o snd); |
904 val _ = disc_eqnss' |> map (fn x => |
905 val _ = disc_eqnss' |> map (fn x => |
905 let val d = duplicates ((op =) o pairself #ctr_no) x in null d orelse |
906 let val d = duplicates ((op =) o pairself #ctr_no) x in null d orelse |
906 primcorec_error_eqns "excess discriminator formula in definition" |
907 primcorec_error_eqns "excess discriminator formula in definition" |
907 (maps (fn t => filter (equal (#ctr_no t) o #ctr_no) x) d |> map #user_eqn) end); |
908 (maps (fn t => filter (curry (op =) (#ctr_no t) o #ctr_no) x) d |> map #user_eqn) end); |
908 |
909 |
909 val sel_eqnss = map_filter (try (fn Sel x => x)) eqns_data |
910 val sel_eqnss = map_filter (try (fn Sel x => x)) eqns_data |
910 |> partition_eq ((op =) o pairself #fun_name) |
911 |> partition_eq ((op =) o pairself #fun_name) |
911 |> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
912 |> fst o finds (fn (x, ({fun_name, ...} :: _)) => x = fun_name) fun_names |
912 |> map (flat o snd); |
913 |> map (flat o snd); |
990 in |
991 in |
991 [mk_imp_p (map (mk_imp_p o map HOLogic.mk_Trueprop o #prems) disc_eqns)] |
992 [mk_imp_p (map (mk_imp_p o map HOLogic.mk_Trueprop o #prems) disc_eqns)] |
992 end) |
993 end) |
993 de_facto_exhaustives disc_eqnss |
994 de_facto_exhaustives disc_eqnss |
994 |> list_all_fun_args [("P", HOLogic.boolT)] |
995 |> list_all_fun_args [("P", HOLogic.boolT)] |
995 |> map3 (fn disc_eqns => fn [] => K [] |
996 |> map3 (fn disc_eqns as {fun_args, ...} :: _ => fn [] => K [] |
996 | [nchotomy_thm] => fn [goal] => |
997 | [nchotomy_thm] => fn [goal] => |
997 [mk_primcorec_exhaust_tac (length disc_eqns) nchotomy_thm |
998 [mk_primcorec_exhaust_tac lthy ("" (* for "P" *) :: map (fst o dest_Free) fun_args) |
|
999 (length disc_eqns) nchotomy_thm |
998 |> K |> Goal.prove lthy [] [] goal |
1000 |> K |> Goal.prove lthy [] [] goal |
999 |> Thm.close_derivation]) |
1001 |> Thm.close_derivation]) |
1000 disc_eqnss nchotomy_thmss; |
1002 disc_eqnss nchotomy_thmss; |
1001 val nontriv_exhaust_thmss = map (filter_out is_trivial_implies) exhaust_thmss; |
1003 val nontriv_exhaust_thmss = map (filter_out is_trivial_implies) exhaust_thmss; |
1002 |
1004 |
1036 |
1038 |
1037 fun prove_sel ({nested_map_idents, nested_map_comps, ctr_specs, ...} : corec_spec) |
1039 fun prove_sel ({nested_map_idents, nested_map_comps, ctr_specs, ...} : corec_spec) |
1038 (disc_eqns : coeqn_data_disc list) excludesss |
1040 (disc_eqns : coeqn_data_disc list) excludesss |
1039 ({fun_name, fun_T, fun_args, ctr, sel, rhs_term, ...} : coeqn_data_sel) = |
1041 ({fun_name, fun_T, fun_args, ctr, sel, rhs_term, ...} : coeqn_data_sel) = |
1040 let |
1042 let |
1041 val SOME ctr_spec = find_first (equal ctr o #ctr) ctr_specs; |
1043 val SOME ctr_spec = find_first (curry (op =) ctr o #ctr) ctr_specs; |
1042 val ctr_no = find_index (equal ctr o #ctr) ctr_specs; |
1044 val ctr_no = find_index (curry (op =) ctr o #ctr) ctr_specs; |
1043 val prems = the_default (maps (s_not_conj o #prems) disc_eqns) |
1045 val prems = the_default (maps (s_not_conj o #prems) disc_eqns) |
1044 (find_first (equal ctr_no o #ctr_no) disc_eqns |> Option.map #prems); |
1046 (find_first (curry (op =) ctr_no o #ctr_no) disc_eqns |> Option.map #prems); |
1045 val sel_corec = find_index (equal sel) (#sels ctr_spec) |
1047 val sel_corec = find_index (curry (op =) sel) (#sels ctr_spec) |
1046 |> nth (#sel_corecs ctr_spec); |
1048 |> nth (#sel_corecs ctr_spec); |
1047 val k = 1 + ctr_no; |
1049 val k = 1 + ctr_no; |
1048 val m = length prems; |
1050 val m = length prems; |
1049 val goal = |
1051 val goal = |
1050 applied_fun_of fun_name fun_T fun_args |
1052 applied_fun_of fun_name fun_T fun_args |
1063 end; |
1065 end; |
1064 |
1066 |
1065 fun prove_ctr disc_alist sel_alist (disc_eqns : coeqn_data_disc list) |
1067 fun prove_ctr disc_alist sel_alist (disc_eqns : coeqn_data_disc list) |
1066 (sel_eqns : coeqn_data_sel list) ({ctr, disc, sels, collapse, ...} : corec_ctr_spec) = |
1068 (sel_eqns : coeqn_data_sel list) ({ctr, disc, sels, collapse, ...} : corec_ctr_spec) = |
1067 (* don't try to prove theorems when some sel_eqns are missing *) |
1069 (* don't try to prove theorems when some sel_eqns are missing *) |
1068 if not (exists (equal ctr o #ctr) disc_eqns) |
1070 if not (exists (curry (op =) ctr o #ctr) disc_eqns) |
1069 andalso not (exists (equal ctr o #ctr) sel_eqns) |
1071 andalso not (exists (curry (op =) ctr o #ctr) sel_eqns) |
1070 orelse |
1072 orelse |
1071 filter (equal ctr o #ctr) sel_eqns |
1073 filter (curry (op =) ctr o #ctr) sel_eqns |
1072 |> fst o finds ((op =) o apsnd #sel) sels |
1074 |> fst o finds ((op =) o apsnd #sel) sels |
1073 |> exists (null o snd) |
1075 |> exists (null o snd) |
1074 then [] else |
1076 then [] else |
1075 let |
1077 let |
1076 val (fun_name, fun_T, fun_args, prems, maybe_rhs) = |
1078 val (fun_name, fun_T, fun_args, prems, maybe_rhs) = |
1077 (find_first (equal ctr o #ctr) disc_eqns, find_first (equal ctr o #ctr) sel_eqns) |
1079 (find_first (curry (op =) ctr o #ctr) disc_eqns, |
|
1080 find_first (curry (op =) ctr o #ctr) sel_eqns) |
1078 |>> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #prems x, |
1081 |>> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, #prems x, |
1079 #maybe_ctr_rhs x)) |
1082 #maybe_ctr_rhs x)) |
1080 ||> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, [], #maybe_ctr_rhs x)) |
1083 ||> Option.map (fn x => (#fun_name x, #fun_T x, #fun_args x, [], #maybe_ctr_rhs x)) |
1081 |> the o merge_options; |
1084 |> the o merge_options; |
1082 val m = length prems; |
1085 val m = length prems; |
1083 val goal = |
1086 val goal = |
1084 (if is_some maybe_rhs then |
1087 (if is_some maybe_rhs then |
1085 the maybe_rhs |
1088 the maybe_rhs |
1086 else |
1089 else |
1087 filter (equal ctr o #ctr) sel_eqns |
1090 filter (curry (op =) ctr o #ctr) sel_eqns |
1088 |> fst o finds ((op =) o apsnd #sel) sels |
1091 |> fst o finds ((op =) o apsnd #sel) sels |
1089 |> map (snd #> (fn [x] => (List.rev (#fun_args x), #rhs_term x)) #-> abstract) |
1092 |> map (snd #> (fn [x] => (List.rev (#fun_args x), #rhs_term x)) #-> abstract) |
1090 |> curry list_comb ctr) |
1093 |> curry list_comb ctr) |
1091 |> curry mk_Trueprop_eq (applied_fun_of fun_name fun_T fun_args) |
1094 |> curry mk_Trueprop_eq (applied_fun_of fun_name fun_T fun_args) |
1092 |> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
1095 |> curry Logic.list_implies (map HOLogic.mk_Trueprop prems) |
1128 val ctr_thms = map (the o AList.lookup (op =) ctr_alist o snd) cond_ctrs; |
1131 val ctr_thms = map (the o AList.lookup (op =) ctr_alist o snd) cond_ctrs; |
1129 in SOME (false, rhs, raw_rhs, ctr_thms) end |
1132 in SOME (false, rhs, raw_rhs, ctr_thms) end |
1130 | NONE => |
1133 | NONE => |
1131 let |
1134 let |
1132 fun prove_code_ctr {ctr, sels, ...} = |
1135 fun prove_code_ctr {ctr, sels, ...} = |
1133 if not (exists (equal ctr o fst) ctr_alist) then NONE else |
1136 if not (exists (curry (op =) ctr o fst) ctr_alist) then NONE else |
1134 let |
1137 let |
1135 val prems = find_first (equal ctr o #ctr) disc_eqns |
1138 val prems = find_first (curry (op =) ctr o #ctr) disc_eqns |
1136 |> Option.map #prems |> the_default []; |
1139 |> Option.map #prems |> the_default []; |
1137 val t = |
1140 val t = |
1138 filter (equal ctr o #ctr) sel_eqns |
1141 filter (curry (op =) ctr o #ctr) sel_eqns |
1139 |> fst o finds ((op =) o apsnd #sel) sels |
1142 |> fst o finds ((op =) o apsnd #sel) sels |
1140 |> map (snd #> (fn [x] => (List.rev (#fun_args x), #rhs_term x)) |
1143 |> map (snd #> (fn [x] => (List.rev (#fun_args x), #rhs_term x)) |
1141 #-> abstract) |
1144 #-> abstract) |
1142 |> curry list_comb ctr; |
1145 |> curry list_comb ctr; |
1143 in |
1146 in |