| author | wenzelm | 
| Sat, 03 Oct 2020 21:54:53 +0200 | |
| changeset 72371 | 3e84f4e9651a | 
| parent 69593 | 3dda49e08b9d | 
| child 74561 | 8e6c973003c8 | 
| 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 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 66 | val extend = I; | 
| 41493 | 67 | val merge = merge_options | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 68 | ) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 69 | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: 
33063diff
changeset | 70 | 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 | 71 | |
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 72 | fun undef _ = error "undef" | 
| 58819 | 73 | |
| 59625 | 74 | 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 | 75 | |> the_default (Multiset | 
| 58819 | 76 |     { msetT = undef, mk_mset=undef,
 | 
| 77 | mset_regroup_conv=undef, mset_member_tac = undef, | |
| 78 | mset_nonempty_tac = undef, mset_pwleq_tac = undef, | |
| 79 | set_of_simps = [],reduction_pair = refl, | |
| 80 | smsI'=refl, wmsI2''=refl, wmsI1=refl }) | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 81 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 82 | 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 | 83 | | order_rpair msrp MS = msrp | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 84 |   | order_rpair _ MIN = @{thm min_rpair_set}
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 85 | |
| 58819 | 86 | fun ord_intros_max true = (@{thm smax_emptyI}, @{thm smax_insertI})
 | 
| 87 |   | ord_intros_max false = (@{thm wmax_emptyI}, @{thm wmax_insertI})
 | |
| 88 | ||
| 89 | fun ord_intros_min true = (@{thm smin_emptyI}, @{thm smin_insertI})
 | |
| 90 |   | 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 | 91 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 92 | fun gen_probl D cs = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 93 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 94 | val n = Termination.get_num_points D | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 95 | 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 | 96 | 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 | 97 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 98 | fun mk_graph c = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 99 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 100 | 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 | 101 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 102 | fun add_edge i j = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 103 | 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 | 104 | 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 | 105 | | SOME (Termination.LessEq _) => cons (i, GEQ, j) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 106 | | _ => I | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 107 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 108 | val edges = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 109 | 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 | 110 | in | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 111 | G (p, q, edges) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 112 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 113 | in | 
| 33063 | 114 | 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 | 115 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 116 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 117 | (* General reduction pair application *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 118 | fun rem_inv_img ctxt = | 
| 60752 | 119 |   resolve_tac ctxt @{thms subsetI} 1
 | 
| 120 |   THEN eresolve_tac ctxt @{thms CollectE} 1
 | |
| 121 |   THEN REPEAT (eresolve_tac ctxt @{thms exE} 1)
 | |
| 63170 | 122 |   THEN Local_Defs.unfold0_tac ctxt @{thms inv_image_def}
 | 
| 60752 | 123 |   THEN resolve_tac ctxt @{thms CollectI} 1
 | 
| 124 |   THEN eresolve_tac ctxt @{thms conjE} 1
 | |
| 125 |   THEN eresolve_tac ctxt @{thms ssubst} 1
 | |
| 63170 | 126 |   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 | 127 | |
| 58819 | 128 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 129 | (* Sets *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 130 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 131 | val setT = HOLogic.mk_setT | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 132 | |
| 60752 | 133 | fun set_member_tac ctxt m i = | 
| 134 |   if m = 0 then resolve_tac ctxt @{thms insertI1} i
 | |
| 135 |   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 | 136 | |
| 60752 | 137 | 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 | 138 | |
| 60752 | 139 | fun set_finite_tac ctxt i = | 
| 140 |   resolve_tac ctxt @{thms finite.emptyI} i
 | |
| 141 |   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 | 142 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 143 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 144 | (* Reconstruction *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 145 | |
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 146 | 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 | 147 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 148 | val Multiset | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 149 |           { msetT, mk_mset,
 | 
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 150 | mset_regroup_conv, mset_pwleq_tac, set_of_simps, | 
| 58819 | 151 | smsI', wmsI2'', wmsI1, reduction_pair=ms_rp, ...} | 
| 59625 | 152 | = get_multiset_setup ctxt | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 153 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 154 | 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 | 155 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 156 | fun get_desc_thm cidx m1 m2 bStrict = | 
| 58819 | 157 | (case Termination.get_descent D (nth cs cidx) m1 m2 of | 
| 158 | SOME (Termination.Less thm) => | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 159 | if bStrict then thm | 
| 59582 | 160 |           else (thm COMP (Thm.lift_rule (Thm.cprop_of thm) @{thm less_imp_le}))
 | 
| 58819 | 161 | | SOME (Termination.LessEq (thm, _)) => | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 162 | 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 | 163 | else raise Fail "get_desc_thm" | 
| 58819 | 164 | | _ => raise Fail "get_desc_thm") | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 165 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 166 | val (label, lev, sl, covering) = certificate | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 167 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 168 | fun prove_lev strict g = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 169 | let | 
| 33855 
cd8acf137c9c
eliminated dead code and some unused bindings, reported by polyml
 krauss parents: 
33583diff
changeset | 170 | 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 | 171 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 172 | 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 | 173 | let | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 174 | 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 | 175 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 176 | val stored_thm = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 177 | 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 | 178 | (not tag_flag) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 179 | |> Conv.fconv_rule (Thm.beta_conversion true) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 180 | |
| 58819 | 181 | val rule = | 
| 182 | if strict | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 183 |               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 | 184 |               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 | 185 | in | 
| 60752 | 186 | resolve_tac ctxt [rule] 1 THEN PRIMITIVE (Thm.elim_implies stored_thm) | 
| 33569 | 187 | 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 | 188 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 189 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 190 | fun steps_tac MAX strict lq lp = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 191 | let | 
| 58819 | 192 | 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 | 193 | in | 
| 58819 | 194 | if length lq = 0 | 
| 60752 | 195 | then resolve_tac ctxt [empty] 1 THEN set_finite_tac ctxt 1 | 
| 196 | THEN (if strict then set_nonempty_tac ctxt 1 else all_tac) | |
| 58819 | 197 | else | 
| 198 | let | |
| 199 | val (j, b) :: rest = lq | |
| 200 | val (i, a) = the (covering g strict j) | |
| 60752 | 201 | fun choose xs = set_member_tac ctxt (find_index (curry op = (i, a)) xs) 1 | 
| 58819 | 202 | val solve_tac = choose lp THEN less_proof strict (j, b) (i, a) | 
| 203 | in | |
| 60752 | 204 | resolve_tac ctxt [step] 1 THEN solve_tac THEN steps_tac MAX strict rest lp | 
| 58819 | 205 | end | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 206 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 207 | | steps_tac MIN strict lq lp = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 208 | let | 
| 58819 | 209 | 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 | 210 | in | 
| 58819 | 211 | if length lp = 0 | 
| 60752 | 212 | then resolve_tac ctxt [empty] 1 | 
| 213 | THEN (if strict then set_nonempty_tac ctxt 1 else all_tac) | |
| 58819 | 214 | else | 
| 215 | let | |
| 216 | val (i, a) :: rest = lp | |
| 217 | val (j, b) = the (covering g strict i) | |
| 60752 | 218 | fun choose xs = set_member_tac ctxt (find_index (curry op = (j, b)) xs) 1 | 
| 58819 | 219 | val solve_tac = choose lq THEN less_proof strict (j, b) (i, a) | 
| 220 | in | |
| 60752 | 221 | resolve_tac ctxt [step] 1 THEN solve_tac THEN steps_tac MIN strict lq rest | 
| 58819 | 222 | end | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 223 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 224 | | steps_tac MS strict lq lp = | 
| 58819 | 225 | let | 
| 226 | fun get_str_cover (j, b) = | |
| 227 | if is_some (covering g true j) then SOME (j, b) else NONE | |
| 228 | 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 | 229 | |
| 58819 | 230 | val qs = subtract (op =) (map_filter get_str_cover lq) lq | 
| 231 | 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 | 232 | |
| 59584 | 233 | fun indices xs ys = map (fn y => find_index (curry op = y) xs) ys | 
| 58819 | 234 | val iqs = indices lq qs | 
| 235 | val ips = indices lp ps | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 236 | |
| 58819 | 237 | local open Conv in | 
| 238 | fun t_conv a C = | |
| 239 | params_conv ~1 (K ((concl_conv ~1 o arg_conv o arg1_conv o a) C)) ctxt | |
| 240 | val goal_rewrite = | |
| 59625 | 241 | t_conv arg1_conv (mset_regroup_conv ctxt iqs) | 
| 242 | then_conv t_conv arg_conv (mset_regroup_conv ctxt ips) | |
| 58819 | 243 | end | 
| 244 | in | |
| 245 | CONVERSION goal_rewrite 1 | |
| 60752 | 246 | THEN (if strict then resolve_tac ctxt [smsI'] 1 | 
| 247 | else if qs = lq then resolve_tac ctxt [wmsI2''] 1 | |
| 248 | else resolve_tac ctxt [wmsI1] 1) | |
| 249 | THEN mset_pwleq_tac ctxt 1 | |
| 58819 | 250 | THEN EVERY (map2 (less_proof false) qs ps) | 
| 251 | THEN (if strict orelse qs <> lq | |
| 63170 | 252 | then Local_Defs.unfold0_tac ctxt set_of_simps | 
| 58819 | 253 | THEN steps_tac MAX true | 
| 254 | (subtract (op =) qs lq) (subtract (op =) ps lp) | |
| 255 | else all_tac) | |
| 256 | end | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 257 | in | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 258 | rem_inv_img ctxt | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 259 | 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 | 260 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 261 | |
| 30450 | 262 | 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 | 263 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 264 | fun tag_pair p (i, tag) = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 265 | HOLogic.pair_const natT natT $ | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 266 | (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 | 267 | |
| 58819 | 268 | fun pt_lev (p, lm) = | 
| 269 |       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 | 270 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 271 | val level_mapping = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 272 | map_index pt_lev lev | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 273 | |> Termination.mk_sumcases D (setT nat_pairT) | 
| 59621 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 wenzelm parents: 
59618diff
changeset | 274 | |> Thm.cterm_of ctxt | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 275 | in | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 276 | PROFILE "Proof Reconstruction" | 
| 59625 | 277 | (CONVERSION (Conv.arg_conv (Conv.arg_conv (Function_Lib.regroup_union_conv ctxt sl))) 1 | 
| 60752 | 278 |          THEN (resolve_tac ctxt @{thms reduction_pair_lemma} 1)
 | 
| 279 |          THEN (resolve_tac ctxt @{thms rp_inv_image_rp} 1)
 | |
| 280 | THEN (resolve_tac ctxt [order_rpair ms_rp label] 1) | |
| 60801 | 281 | THEN PRIMITIVE (Thm.instantiate' [] [SOME level_mapping]) | 
| 54998 | 282 |          THEN unfold_tac ctxt @{thms rp_inv_image_def}
 | 
| 63170 | 283 |          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 | 284 |          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 | 285 | THEN EVERY (map (prove_lev true) sl) | 
| 33040 | 286 | 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 | 287 | end | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 288 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 289 | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 290 | 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 | 291 | let | 
| 42361 | 292 | val ms_configured = is_some (Multiset_Setup.get (Proof_Context.theory_of ctxt)) | 
| 58819 | 293 | val orders' = | 
| 294 | if ms_configured then orders | |
| 295 | 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 | 296 | val gp = gen_probl D cs | 
| 29877 | 297 | val certificate = generate_certificate use_tags orders' gp | 
| 298 | in | |
| 58819 | 299 | (case certificate of | 
| 300 | NONE => no_tac | |
| 301 | | SOME cert => | |
| 302 | SELECT_GOAL (reconstruct_tac ctxt D cs gp cert) i | |
| 60752 | 303 |         THEN TRY (resolve_tac ctxt @{thms wf_empty} i))
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 304 | end) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 305 | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 306 | fun gen_decomp_scnp_tac orders autom_tac ctxt = | 
| 58819 | 307 | Termination.TERMINATION ctxt autom_tac (fn D => | 
| 308 | let | |
| 59618 | 309 | val decompose = Termination.decompose_tac ctxt D | 
| 58819 | 310 | val scnp_full = single_scnp_tac true orders ctxt D | 
| 311 | in | |
| 312 | REPEAT_ALL_NEW (scnp_full ORELSE' decompose) | |
| 313 | end) | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 314 | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 315 | fun gen_sizechange_tac orders autom_tac ctxt = | 
| 59159 | 316 | TRY (Function_Common.termination_rule_tac ctxt 1) | 
| 30607 
c3d1590debd8
eliminated global SIMPSET, CLASET etc. -- refer to explicit context;
 wenzelm parents: 
30510diff
changeset | 317 | THEN TRY (Termination.wf_union_tac ctxt) | 
| 60752 | 318 |   THEN (resolve_tac ctxt @{thms wf_empty} 1 ORELSE gen_decomp_scnp_tac orders autom_tac ctxt 1)
 | 
| 29877 | 319 | |
| 320 | fun sizechange_tac ctxt autom_tac = | |
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 321 | gen_sizechange_tac [MAX, MS, MIN] autom_tac ctxt | 
| 29877 | 322 | |
| 36521 | 323 | fun decomp_scnp_tac orders ctxt = | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 324 | let | 
| 69593 | 325 | 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 | 326 | 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 | 327 | in | 
| 39924 
f4d3e70ed3a8
discontinued continuations to simplify control flow; dropped optimization in scnp
 krauss parents: 
39923diff
changeset | 328 | 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 | 329 | end | 
| 
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 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 332 | (* Method setup *) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 333 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 334 | val orders = | 
| 31242 | 335 | Scan.repeat1 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 336 | ((Args.$$$ "max" >> K MAX) || | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 337 | (Args.$$$ "min" >> K MIN) || | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 338 | (Args.$$$ "ms" >> K MS)) | 
| 31242 | 339 | || Scan.succeed [MAX, MS, MIN] | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 340 | |
| 58819 | 341 | val _ = | 
| 342 | Theory.setup | |
| 69593 | 343 | (Method.setup \<^binding>\<open>size_change\<close> | 
| 58819 | 344 | (Scan.lift orders --| Method.sections clasimp_modifiers >> | 
| 345 | (fn orders => SIMPLE_METHOD o decomp_scnp_tac orders)) | |
| 346 | "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 | 347 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: diff
changeset | 348 | end |