| author | wenzelm | 
| Sun, 23 Mar 2025 17:07:55 +0100 | |
| changeset 82322 | 94fd80f0107d | 
| parent 82299 | a0693649e9c6 | 
| child 82967 | 73af47bc277c | 
| permissions | -rw-r--r-- | 
| 31775 | 1 | (* Title: HOL/Tools/Function/scnp_reconstruct.ML | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 2 | Author: Armin Heller, TU Muenchen | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 3 | Author: Alexander Krauss, TU Muenchen | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 4 | |
| 41114 | 5 | Proof reconstruction for SCNP termination. | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 6 | *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 7 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 8 | signature SCNP_RECONSTRUCT = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 9 | sig | 
| 29877 | 10 | val sizechange_tac : Proof.context -> tactic -> tactic | 
| 11 | ||
| 36521 | 12 | val decomp_scnp_tac : ScnpSolve.label list -> Proof.context -> tactic | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 13 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 14 | datatype multiset_setup = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 15 | Multiset of | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 16 |     {
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 17 | msetT : typ -> typ, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 18 | mk_mset : typ -> term list -> term, | 
| 59625 | 19 | mset_regroup_conv : Proof.context -> int list -> conv, | 
| 60752 | 20 | mset_member_tac : Proof.context -> int -> int -> tactic, | 
| 21 | mset_nonempty_tac : Proof.context -> int -> tactic, | |
| 22 | mset_pwleq_tac : Proof.context -> int -> tactic, | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 23 | set_of_simps : thm list, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 24 | smsI' : thm, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 25 | wmsI2'' : thm, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 26 | wmsI1 : thm, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 27 | reduction_pair : thm | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 28 | } | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 29 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 30 | val multiset_setup : multiset_setup -> theory -> theory | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 31 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 32 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 33 | structure ScnpReconstruct : SCNP_RECONSTRUCT = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 34 | struct | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 35 | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: 
33063diff
changeset | 36 | val PROFILE = Function_Common.PROFILE | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 37 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 38 | open ScnpSolve | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 39 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 40 | val natT = HOLogic.natT | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 41 | val nat_pairT = HOLogic.mk_prodT (natT, natT) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 42 | |
| 58819 | 43 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 44 | (* Theory dependencies *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 45 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 46 | datatype multiset_setup = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 47 | Multiset of | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 48 |   {
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 49 | msetT : typ -> typ, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 50 | mk_mset : typ -> term list -> term, | 
| 59625 | 51 | mset_regroup_conv : Proof.context -> int list -> conv, | 
| 60752 | 52 | mset_member_tac : Proof.context -> int -> int -> tactic, | 
| 53 | mset_nonempty_tac : Proof.context -> int -> tactic, | |
| 54 | mset_pwleq_tac : Proof.context -> int -> tactic, | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 55 | set_of_simps : thm list, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 56 | smsI' : thm, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 57 | wmsI2'' : thm, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 58 | wmsI1 : thm, | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 59 | reduction_pair : thm | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 60 | } | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 61 | |
| 33522 | 62 | structure Multiset_Setup = Theory_Data | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 63 | ( | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 64 | type T = multiset_setup option | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 65 | val empty = NONE | 
| 41493 | 66 | val merge = merge_options | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 67 | ) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 68 | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: 
33063diff
changeset | 69 | val multiset_setup = Multiset_Setup.put o SOME | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 70 | |
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 71 | fun undef _ = error "undef" | 
| 58819 | 72 | |
| 59625 | 73 | fun get_multiset_setup ctxt = Multiset_Setup.get (Proof_Context.theory_of ctxt) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 74 | |> the_default (Multiset | 
| 58819 | 75 |     { msetT = undef, mk_mset=undef,
 | 
| 76 | mset_regroup_conv=undef, mset_member_tac = undef, | |
| 77 | mset_nonempty_tac = undef, mset_pwleq_tac = undef, | |
| 78 | set_of_simps = [],reduction_pair = refl, | |
| 79 | smsI'=refl, wmsI2''=refl, wmsI1=refl }) | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 80 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 81 | fun order_rpair _ MAX = @{thm max_rpair_set}
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 82 | | order_rpair msrp MS = msrp | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 83 |   | order_rpair _ MIN = @{thm min_rpair_set}
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 84 | |
| 58819 | 85 | fun ord_intros_max true = (@{thm smax_emptyI}, @{thm smax_insertI})
 | 
| 86 |   | ord_intros_max false = (@{thm wmax_emptyI}, @{thm wmax_insertI})
 | |
| 87 | ||
| 88 | fun ord_intros_min true = (@{thm smin_emptyI}, @{thm smin_insertI})
 | |
| 89 |   | ord_intros_min false = (@{thm wmin_emptyI}, @{thm wmin_insertI})
 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 90 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 91 | fun gen_probl D cs = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 92 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 93 | val n = Termination.get_num_points D | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 94 | val arity = length o Termination.get_measures D | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 95 | fun measure p i = nth (Termination.get_measures D p) i | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 96 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 97 | fun mk_graph c = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 98 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 99 | val (_, p, _, q, _, _) = Termination.dest_call D c | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 100 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 101 | fun add_edge i j = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 102 | case Termination.get_descent D c (measure p i) (measure q j) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 103 | of SOME (Termination.Less _) => cons (i, GTR, j) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 104 | | SOME (Termination.LessEq _) => cons (i, GEQ, j) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 105 | | _ => I | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 106 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 107 | val edges = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 108 | fold_product add_edge (0 upto arity p - 1) (0 upto arity q - 1) [] | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 109 | in | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 110 | G (p, q, edges) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 111 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 112 | in | 
| 33063 | 113 | GP (map_range arity n, map mk_graph cs) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 114 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 115 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 116 | (* General reduction pair application *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 117 | fun rem_inv_img ctxt = | 
| 60752 | 118 |   resolve_tac ctxt @{thms subsetI} 1
 | 
| 119 |   THEN eresolve_tac ctxt @{thms CollectE} 1
 | |
| 120 |   THEN REPEAT (eresolve_tac ctxt @{thms exE} 1)
 | |
| 63170 | 121 |   THEN Local_Defs.unfold0_tac ctxt @{thms inv_image_def}
 | 
| 60752 | 122 |   THEN resolve_tac ctxt @{thms CollectI} 1
 | 
| 123 |   THEN eresolve_tac ctxt @{thms conjE} 1
 | |
| 124 |   THEN eresolve_tac ctxt @{thms ssubst} 1
 | |
| 63170 | 125 |   THEN Local_Defs.unfold0_tac ctxt @{thms split_conv triv_forall_equality sum.case}
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 126 | |
| 58819 | 127 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 128 | (* Sets *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 129 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 130 | val setT = HOLogic.mk_setT | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 131 | |
| 60752 | 132 | fun set_member_tac ctxt m i = | 
| 133 |   if m = 0 then resolve_tac ctxt @{thms insertI1} i
 | |
| 134 |   else resolve_tac ctxt @{thms insertI2} i THEN set_member_tac ctxt (m - 1) i
 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 135 | |
| 60752 | 136 | fun set_nonempty_tac ctxt = resolve_tac ctxt @{thms insert_not_empty}
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 137 | |
| 60752 | 138 | fun set_finite_tac ctxt i = | 
| 139 |   resolve_tac ctxt @{thms finite.emptyI} i
 | |
| 140 |   ORELSE (resolve_tac ctxt @{thms finite.insertI} i THEN (fn st => set_finite_tac ctxt i st))
 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 141 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 142 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 143 | (* Reconstruction *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 144 | |
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 145 | fun reconstruct_tac ctxt D cs (GP (_, gs)) certificate = | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 146 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 147 | val Multiset | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 148 |           { msetT, mk_mset,
 | 
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 149 | mset_regroup_conv, mset_pwleq_tac, set_of_simps, | 
| 58819 | 150 | smsI', wmsI2'', wmsI1, reduction_pair=ms_rp, ...} | 
| 59625 | 151 | = get_multiset_setup ctxt | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 152 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 153 | fun measure_fn p = nth (Termination.get_measures D p) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 154 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 155 | fun get_desc_thm cidx m1 m2 bStrict = | 
| 58819 | 156 | (case Termination.get_descent D (nth cs cidx) m1 m2 of | 
| 157 | SOME (Termination.Less thm) => | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 158 | if bStrict then thm | 
| 59582 | 159 |           else (thm COMP (Thm.lift_rule (Thm.cprop_of thm) @{thm less_imp_le}))
 | 
| 58819 | 160 | | SOME (Termination.LessEq (thm, _)) => | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 161 | if not bStrict then thm | 
| 40317 
1eac228c52b3
replaced ancient sys_error by raise Fail, assuming that the latter is not handled specifically by the environment;
 wenzelm parents: 
39925diff
changeset | 162 | else raise Fail "get_desc_thm" | 
| 58819 | 163 | | _ => raise Fail "get_desc_thm") | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 164 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 165 | val (label, lev, sl, covering) = certificate | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 166 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 167 | fun prove_lev strict g = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 168 | let | 
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 169 | val G (p, q, _) = nth gs g | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 170 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 171 | fun less_proof strict (j, b) (i, a) = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 172 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 173 | val tag_flag = b < a orelse (not strict andalso b <= a) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 174 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 175 | val stored_thm = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 176 | get_desc_thm g (measure_fn p i) (measure_fn q j) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 177 | (not tag_flag) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 178 | |> Conv.fconv_rule (Thm.beta_conversion true) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 179 | |
| 58819 | 180 | val rule = | 
| 181 | if strict | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 182 |               then if b < a then @{thm pair_lessI2} else @{thm pair_lessI1}
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 183 |               else if b <= a then @{thm pair_leqI2} else @{thm pair_leqI1}
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 184 | in | 
| 60752 | 185 | resolve_tac ctxt [rule] 1 THEN PRIMITIVE (Thm.elim_implies stored_thm) | 
| 33569 | 186 | THEN (if tag_flag then Arith_Data.arith_tac ctxt 1 else all_tac) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 187 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 188 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 189 | fun steps_tac MAX strict lq lp = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 190 | let | 
| 58819 | 191 | val (empty, step) = ord_intros_max strict | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 192 | in | 
| 58819 | 193 | if length lq = 0 | 
| 60752 | 194 | then resolve_tac ctxt [empty] 1 THEN set_finite_tac ctxt 1 | 
| 195 | THEN (if strict then set_nonempty_tac ctxt 1 else all_tac) | |
| 58819 | 196 | else | 
| 197 | let | |
| 198 | val (j, b) :: rest = lq | |
| 199 | val (i, a) = the (covering g strict j) | |
| 60752 | 200 | fun choose xs = set_member_tac ctxt (find_index (curry op = (i, a)) xs) 1 | 
| 58819 | 201 | val solve_tac = choose lp THEN less_proof strict (j, b) (i, a) | 
| 202 | in | |
| 60752 | 203 | resolve_tac ctxt [step] 1 THEN solve_tac THEN steps_tac MAX strict rest lp | 
| 58819 | 204 | end | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 205 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 206 | | steps_tac MIN strict lq lp = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 207 | let | 
| 58819 | 208 | val (empty, step) = ord_intros_min strict | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 209 | in | 
| 58819 | 210 | if length lp = 0 | 
| 60752 | 211 | then resolve_tac ctxt [empty] 1 | 
| 212 | THEN (if strict then set_nonempty_tac ctxt 1 else all_tac) | |
| 58819 | 213 | else | 
| 214 | let | |
| 215 | val (i, a) :: rest = lp | |
| 216 | val (j, b) = the (covering g strict i) | |
| 60752 | 217 | fun choose xs = set_member_tac ctxt (find_index (curry op = (j, b)) xs) 1 | 
| 58819 | 218 | val solve_tac = choose lq THEN less_proof strict (j, b) (i, a) | 
| 219 | in | |
| 60752 | 220 | resolve_tac ctxt [step] 1 THEN solve_tac THEN steps_tac MIN strict lq rest | 
| 58819 | 221 | end | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 222 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 223 | | steps_tac MS strict lq lp = | 
| 58819 | 224 | let | 
| 225 | fun get_str_cover (j, b) = | |
| 226 | if is_some (covering g true j) then SOME (j, b) else NONE | |
| 227 | fun get_wk_cover (j, b) = the (covering g false j) | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 228 | |
| 58819 | 229 | val qs = subtract (op =) (map_filter get_str_cover lq) lq | 
| 230 | val ps = map get_wk_cover qs | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 231 | |
| 59584 | 232 | fun indices xs ys = map (fn y => find_index (curry op = y) xs) ys | 
| 58819 | 233 | val iqs = indices lq qs | 
| 234 | val ips = indices lp ps | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 235 | |
| 58819 | 236 | local open Conv in | 
| 237 | fun t_conv a C = | |
| 238 | params_conv ~1 (K ((concl_conv ~1 o arg_conv o arg1_conv o a) C)) ctxt | |
| 239 | val goal_rewrite = | |
| 59625 | 240 | t_conv arg1_conv (mset_regroup_conv ctxt iqs) | 
| 241 | then_conv t_conv arg_conv (mset_regroup_conv ctxt ips) | |
| 58819 | 242 | end | 
| 243 | in | |
| 244 | CONVERSION goal_rewrite 1 | |
| 60752 | 245 | THEN (if strict then resolve_tac ctxt [smsI'] 1 | 
| 246 | else if qs = lq then resolve_tac ctxt [wmsI2''] 1 | |
| 247 | else resolve_tac ctxt [wmsI1] 1) | |
| 248 | THEN mset_pwleq_tac ctxt 1 | |
| 58819 | 249 | THEN EVERY (map2 (less_proof false) qs ps) | 
| 250 | THEN (if strict orelse qs <> lq | |
| 63170 | 251 | then Local_Defs.unfold0_tac ctxt set_of_simps | 
| 58819 | 252 | THEN steps_tac MAX true | 
| 253 | (subtract (op =) qs lq) (subtract (op =) ps lp) | |
| 254 | else all_tac) | |
| 255 | end | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 256 | in | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 257 | rem_inv_img ctxt | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 258 | THEN steps_tac label strict (nth lev q) (nth lev p) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 259 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 260 | |
| 30450 | 261 | val (mk_set, setT) = if label = MS then (mk_mset, msetT) else (HOLogic.mk_set, setT) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 262 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 263 | fun tag_pair p (i, tag) = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 264 | HOLogic.pair_const natT natT $ | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 265 | (measure_fn p i $ Bound 0) $ HOLogic.mk_number natT tag | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 266 | |
| 58819 | 267 | fun pt_lev (p, lm) = | 
| 268 |       Abs ("x", Termination.get_types D p, mk_set nat_pairT (map (tag_pair p) lm))
 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 269 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 270 | val level_mapping = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 271 | map_index pt_lev lev | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 272 | |> Termination.mk_sumcases D (setT nat_pairT) | 
| 59621 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 wenzelm parents: 
59618diff
changeset | 273 | |> Thm.cterm_of ctxt | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 274 | in | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 275 | PROFILE "Proof Reconstruction" | 
| 59625 | 276 | (CONVERSION (Conv.arg_conv (Conv.arg_conv (Function_Lib.regroup_union_conv ctxt sl))) 1 | 
| 60752 | 277 |          THEN (resolve_tac ctxt @{thms reduction_pair_lemma} 1)
 | 
| 278 |          THEN (resolve_tac ctxt @{thms rp_inv_image_rp} 1)
 | |
| 279 | THEN (resolve_tac ctxt [order_rpair ms_rp label] 1) | |
| 60801 | 280 | THEN PRIMITIVE (Thm.instantiate' [] [SOME level_mapping]) | 
| 54998 | 281 |          THEN unfold_tac ctxt @{thms rp_inv_image_def}
 | 
| 63170 | 282 |          THEN Local_Defs.unfold0_tac ctxt @{thms split_conv fst_conv snd_conv}
 | 
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
59159diff
changeset | 283 |          THEN REPEAT (SOMEGOAL (resolve_tac ctxt [@{thm Un_least}, @{thm empty_subsetI}]))
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 284 | THEN EVERY (map (prove_lev true) sl) | 
| 33040 | 285 | THEN EVERY (map (prove_lev false) (subtract (op =) sl (0 upto length cs - 1)))) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 286 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 287 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 288 | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 289 | fun single_scnp_tac use_tags orders ctxt D = Termination.CALLS (fn (cs, i) => | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 290 | let | 
| 42361 | 291 | val ms_configured = is_some (Multiset_Setup.get (Proof_Context.theory_of ctxt)) | 
| 58819 | 292 | val orders' = | 
| 293 | if ms_configured then orders | |
| 294 | else filter_out (curry op = MS) orders | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 295 | val gp = gen_probl D cs | 
| 29877 | 296 | val certificate = generate_certificate use_tags orders' gp | 
| 297 | in | |
| 58819 | 298 | (case certificate of | 
| 299 | NONE => no_tac | |
| 300 | | SOME cert => | |
| 301 | SELECT_GOAL (reconstruct_tac ctxt D cs gp cert) i | |
| 82299 | 302 |         THEN TRY (resolve_tac ctxt @{thms wf_on_bot} i))
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 303 | end) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 304 | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 305 | fun gen_decomp_scnp_tac orders autom_tac ctxt = | 
| 58819 | 306 | Termination.TERMINATION ctxt autom_tac (fn D => | 
| 307 | let | |
| 59618 | 308 | val decompose = Termination.decompose_tac ctxt D | 
| 58819 | 309 | val scnp_full = single_scnp_tac true orders ctxt D | 
| 310 | in | |
| 311 | REPEAT_ALL_NEW (scnp_full ORELSE' decompose) | |
| 312 | end) | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 313 | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 314 | fun gen_sizechange_tac orders autom_tac ctxt = | 
| 59159 | 315 | TRY (Function_Common.termination_rule_tac ctxt 1) | 
| 30607 
c3d1590debd8
eliminated global SIMPSET, CLASET etc. -- refer to explicit context;
 wenzelm parents: 
30510diff
changeset | 316 | THEN TRY (Termination.wf_union_tac ctxt) | 
| 82299 | 317 |   THEN (resolve_tac ctxt @{thms wf_on_bot} 1 ORELSE gen_decomp_scnp_tac orders autom_tac ctxt 1)
 | 
| 29877 | 318 | |
| 319 | fun sizechange_tac ctxt autom_tac = | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 320 | gen_sizechange_tac [MAX, MS, MIN] autom_tac ctxt | 
| 29877 | 321 | |
| 36521 | 322 | fun decomp_scnp_tac orders ctxt = | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 323 | let | 
| 69593 | 324 | val extra_simps = Named_Theorems.get ctxt \<^named_theorems>\<open>termination_simp\<close> | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
42795diff
changeset | 325 | val autom_tac = auto_tac (ctxt addsimps extra_simps) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 326 | in | 
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 327 | gen_sizechange_tac orders autom_tac ctxt | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 328 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 329 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 330 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 331 | (* Method setup *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 332 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 333 | val orders = | 
| 31242 | 334 | Scan.repeat1 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 335 | ((Args.$$$ "max" >> K MAX) || | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 336 | (Args.$$$ "min" >> K MIN) || | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 337 | (Args.$$$ "ms" >> K MS)) | 
| 31242 | 338 | || Scan.succeed [MAX, MS, MIN] | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 339 | |
| 58819 | 340 | val _ = | 
| 341 | Theory.setup | |
| 69593 | 342 | (Method.setup \<^binding>\<open>size_change\<close> | 
| 58819 | 343 | (Scan.lift orders --| Method.sections clasimp_modifiers >> | 
| 344 | (fn orders => SIMPLE_METHOD o decomp_scnp_tac orders)) | |
| 345 | "termination prover with graph decomposition and the NP subset of size change termination") | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 346 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 347 | end |