author | krauss |
Fri, 24 Nov 2006 13:39:22 +0100 | |
changeset 21510 | 7e72185e4b24 |
parent 21319 | cf814e36f788 |
child 21590 | ef7278f553eb |
permissions | -rw-r--r-- |
21131 | 1 |
(* Title: HOL/Tools/function_package/lexicographic_order.ML |
21201 | 2 |
ID: $Id$ |
21131 | 3 |
Author: Lukas Bulwahn, TU Muenchen |
4 |
||
5 |
Method for termination proofs with lexicographic orderings. |
|
6 |
*) |
|
7 |
||
8 |
signature LEXICOGRAPHIC_ORDER = |
|
9 |
sig |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
10 |
val lexicographic_order : Proof.context -> Method.method |
21510
7e72185e4b24
exported mk_base_funs for use by size-change tools
krauss
parents:
21319
diff
changeset
|
11 |
|
7e72185e4b24
exported mk_base_funs for use by size-change tools
krauss
parents:
21319
diff
changeset
|
12 |
(* exported for use by size-change termination prototype. |
7e72185e4b24
exported mk_base_funs for use by size-change tools
krauss
parents:
21319
diff
changeset
|
13 |
FIXME: provide a common interface later *) |
7e72185e4b24
exported mk_base_funs for use by size-change tools
krauss
parents:
21319
diff
changeset
|
14 |
val mk_base_funs : typ -> term list |
7e72185e4b24
exported mk_base_funs for use by size-change tools
krauss
parents:
21319
diff
changeset
|
15 |
|
21237 | 16 |
val setup: theory -> theory |
21131 | 17 |
end |
18 |
||
19 |
structure LexicographicOrder : LEXICOGRAPHIC_ORDER = |
|
20 |
struct |
|
21 |
||
22 |
(* Theory dependencies *) |
|
23 |
val measures = "List.measures" |
|
24 |
val wf_measures = thm "wf_measures" |
|
25 |
val measures_less = thm "measures_less" |
|
26 |
val measures_lesseq = thm "measures_lesseq" |
|
21237 | 27 |
|
21131 | 28 |
fun del_index (n, []) = [] |
29 |
| del_index (n, x :: xs) = |
|
21237 | 30 |
if n>0 then x :: del_index (n - 1, xs) else xs |
21131 | 31 |
|
32 |
fun transpose ([]::_) = [] |
|
33 |
| transpose xss = map hd xss :: transpose (map tl xss) |
|
34 |
||
35 |
fun mk_sum_case (f1, f2) = |
|
36 |
case (fastype_of f1, fastype_of f2) of |
|
21237 | 37 |
(Type("fun", [A, B]), Type("fun", [C, D])) => |
38 |
if (B = D) then |
|
39 |
Const("Datatype.sum.sum_case", (A --> B) --> (C --> D) --> Type("+", [A,C]) --> B) $ f1 $ f2 |
|
40 |
else raise TERM ("mk_sum_case: range type mismatch", [f1, f2]) |
|
41 |
| _ => raise TERM ("mk_sum_case", [f1, f2]) |
|
42 |
||
21131 | 43 |
fun dest_wf (Const ("Wellfounded_Recursion.wf", _) $ t) = t |
44 |
| dest_wf t = raise TERM ("dest_wf", [t]) |
|
21237 | 45 |
|
21131 | 46 |
datatype cell = Less of thm | LessEq of thm | None of thm | False of thm; |
21237 | 47 |
|
21131 | 48 |
fun is_Less cell = case cell of (Less _) => true | _ => false |
21237 | 49 |
|
21131 | 50 |
fun is_LessEq cell = case cell of (LessEq _) => true | _ => false |
21237 | 51 |
|
21131 | 52 |
fun thm_of_cell cell = |
53 |
case cell of |
|
21237 | 54 |
Less thm => thm |
55 |
| LessEq thm => thm |
|
56 |
| False thm => thm |
|
57 |
| None thm => thm |
|
58 |
||
21131 | 59 |
fun mk_base_fun_bodys (t : term) (tt : typ) = |
60 |
case tt of |
|
21237 | 61 |
Type("*", [ft, st]) => (mk_base_fun_bodys (Const("fst", tt --> ft) $ t) ft) @ (mk_base_fun_bodys (Const("snd", tt --> st) $ t) st) |
62 |
| _ => [(t, tt)] |
|
63 |
||
21131 | 64 |
fun mk_base_fun_header fulltyp (t, typ) = |
65 |
if typ = HOLogic.natT then |
|
21237 | 66 |
Abs ("x", fulltyp, t) |
21131 | 67 |
else Abs ("x", fulltyp, Const("Nat.size", typ --> HOLogic.natT) $ t) |
21237 | 68 |
|
21131 | 69 |
fun mk_base_funs (tt: typ) = |
70 |
mk_base_fun_bodys (Bound 0) tt |> |
|
21237 | 71 |
map (mk_base_fun_header tt) |
72 |
||
21131 | 73 |
fun mk_ext_base_funs (tt : typ) = |
74 |
case tt of |
|
21237 | 75 |
Type("+", [ft, st]) => |
76 |
product (mk_ext_base_funs ft) (mk_ext_base_funs st) |
|
77 |
|> map mk_sum_case |
|
78 |
| _ => mk_base_funs tt |
|
79 |
||
21131 | 80 |
fun dest_term (t : term) = |
81 |
let |
|
21237 | 82 |
val (vars, prop) = (FundefLib.dest_all_all t) |
83 |
val prems = Logic.strip_imp_prems prop |
|
84 |
val (tuple, rel) = Logic.strip_imp_concl prop |
|
85 |
|> HOLogic.dest_Trueprop |
|
86 |
|> HOLogic.dest_mem |
|
87 |
val (lhs, rhs) = HOLogic.dest_prod tuple |
|
21131 | 88 |
in |
21237 | 89 |
(vars, prems, lhs, rhs, rel) |
21131 | 90 |
end |
21237 | 91 |
|
21131 | 92 |
fun mk_goal (vars, prems, lhs, rhs) rel = |
93 |
let |
|
21237 | 94 |
val concl = HOLogic.mk_binrel rel (lhs, rhs) |> HOLogic.mk_Trueprop |
95 |
in |
|
96 |
Logic.list_implies (prems, concl) |> |
|
97 |
fold_rev FundefLib.mk_forall vars |
|
21131 | 98 |
end |
21237 | 99 |
|
21131 | 100 |
fun prove (thy: theory) (t: term) = |
101 |
cterm_of thy t |> Goal.init |
|
102 |
|> SINGLE (CLASIMPSET auto_tac) |> the |
|
21237 | 103 |
|
21131 | 104 |
fun mk_cell (thy : theory) (vars, prems) (lhs, rhs) = |
21237 | 105 |
let |
106 |
val goals = mk_goal (vars, prems, lhs, rhs) |
|
107 |
val less_thm = goals "Orderings.less" |> prove thy |
|
21131 | 108 |
in |
21237 | 109 |
if Thm.no_prems less_thm then |
110 |
Less (Goal.finish less_thm) |
|
111 |
else |
|
112 |
let |
|
113 |
val lesseq_thm = goals "Orderings.less_eq" |> prove thy |
|
114 |
in |
|
115 |
if Thm.no_prems lesseq_thm then |
|
116 |
LessEq (Goal.finish lesseq_thm) |
|
117 |
else |
|
118 |
if prems_of lesseq_thm = [HOLogic.Trueprop $ HOLogic.false_const] then False lesseq_thm |
|
119 |
else None lesseq_thm |
|
120 |
end |
|
21131 | 121 |
end |
21237 | 122 |
|
21131 | 123 |
fun mk_row (thy: theory) base_funs (t : term) = |
124 |
let |
|
21237 | 125 |
val (vars, prems, lhs, rhs, _) = dest_term t |
126 |
val lhs_list = map (fn x => x $ lhs) base_funs |
|
127 |
val rhs_list = map (fn x => x $ rhs) base_funs |
|
21131 | 128 |
in |
21237 | 129 |
map (mk_cell thy (vars, prems)) (lhs_list ~~ rhs_list) |
21131 | 130 |
end |
21237 | 131 |
|
21131 | 132 |
(* simple depth-first search algorithm for the table *) |
133 |
fun search_table table = |
|
134 |
case table of |
|
21237 | 135 |
[] => SOME [] |
136 |
| _ => |
|
137 |
let |
|
138 |
val check_col = forall (fn c => is_Less c orelse is_LessEq c) |
|
139 |
val col = find_index (check_col) (transpose table) |
|
140 |
in case col of |
|
141 |
~1 => NONE |
|
142 |
| _ => |
|
143 |
let |
|
144 |
val order_opt = table |> filter_out (fn x => is_Less (nth x col)) |> map (curry del_index col) |> search_table |
|
145 |
val transform_order = (fn col => map (fn x => if x>=col then x+1 else x)) |
|
146 |
in case order_opt of |
|
147 |
NONE => NONE |
|
148 |
| SOME order =>SOME (col::(transform_order col order)) |
|
149 |
end |
|
150 |
end |
|
151 |
||
21131 | 152 |
fun prove_row row (st : thm) = |
153 |
case row of |
|
21237 | 154 |
[] => sys_error "INTERNAL ERROR IN lexicographic order termination tactic - fun prove_row (row is empty)" |
155 |
| cell::tail => |
|
156 |
case cell of |
|
157 |
Less less_thm => |
|
158 |
let |
|
159 |
val next_thm = st |> SINGLE (rtac measures_less 1) |> the |
|
160 |
in |
|
161 |
implies_elim next_thm less_thm |
|
162 |
end |
|
163 |
| LessEq lesseq_thm => |
|
164 |
let |
|
165 |
val next_thm = st |> SINGLE (rtac measures_lesseq 1) |> the |
|
166 |
in |
|
167 |
implies_elim next_thm lesseq_thm |
|
168 |
|> prove_row tail |
|
169 |
end |
|
170 |
| _ => sys_error "INTERNAL ERROR IN lexicographic order termination tactic - fun prove_row (Only expecting Less or LessEq)" |
|
171 |
||
21131 | 172 |
fun pr_unprovable_subgoals table = |
173 |
filter (fn x => not (is_Less x) andalso not (is_LessEq x)) (flat table) |
|
21237 | 174 |
|> map ((fn th => Pretty.string_of (Pretty.chunks (Display.pretty_goals (Thm.nprems_of th) th))) o thm_of_cell) |
175 |
||
21131 | 176 |
fun pr_goal thy t i = |
177 |
let |
|
21237 | 178 |
val (_, prems, lhs, rhs, _) = dest_term t |
179 |
val prterm = string_of_cterm o (cterm_of thy) |
|
21131 | 180 |
in |
21237 | 181 |
(* also show prems? *) |
21131 | 182 |
i ^ ") " ^ (prterm lhs) ^ " '<' " ^ (prterm rhs) |
183 |
end |
|
21237 | 184 |
|
21131 | 185 |
fun pr_fun thy t i = |
186 |
(string_of_int i) ^ ") " ^ (string_of_cterm (cterm_of thy t)) |
|
21237 | 187 |
|
21131 | 188 |
fun pr_cell cell = case cell of Less _ => " < " |
21237 | 189 |
| LessEq _ => " <= " |
190 |
| None _ => " N " |
|
191 |
| False _ => " F " |
|
192 |
||
21131 | 193 |
(* fun pr_err: prints the table if tactic failed *) |
194 |
fun pr_err table thy tl base_funs = |
|
195 |
let |
|
21237 | 196 |
val gc = map (fn i => chr (i + 96)) (1 upto (length table)) |
197 |
val mc = 1 upto (length base_funs) |
|
198 |
val tstr = (" " ^ (concat (map (fn i => " " ^ (string_of_int i) ^ " ") mc))) :: |
|
199 |
(map2 (fn r => fn i => i ^ ": " ^ (concat (map pr_cell r))) table gc) |
|
200 |
val gstr = ("Goals:"::(map2 (pr_goal thy) tl gc)) |
|
201 |
val mstr = ("Measures:"::(map2 (pr_fun thy) base_funs mc)) |
|
202 |
val ustr = ("Unfinished subgoals:"::(pr_unprovable_subgoals table)) |
|
21131 | 203 |
in |
21237 | 204 |
tstr @ gstr @ mstr @ ustr |
21131 | 205 |
end |
21237 | 206 |
|
21131 | 207 |
(* the main function: create table, search table, create relation, |
208 |
and prove the subgoals *) |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
209 |
fun lexicographic_order_tac ctxt (st: thm) = |
21131 | 210 |
let |
21237 | 211 |
val thy = theory_of_thm st |
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
212 |
val termination_thm = the (FundefCommon.get_termination_rule ctxt) |
21237 | 213 |
val next_st = SINGLE (rtac termination_thm 1) st |> the |
214 |
val premlist = prems_of next_st |
|
21131 | 215 |
in |
21237 | 216 |
case premlist of |
21131 | 217 |
[] => error "invalid number of subgoals for this tactic - expecting at least 1 subgoal" |
218 |
| (wf::tl) => let |
|
21237 | 219 |
val (var, prop) = FundefLib.dest_all wf |
220 |
val rel = HOLogic.dest_Trueprop prop |> dest_wf |> head_of |
|
221 |
val crel = cterm_of thy rel |
|
222 |
val base_funs = mk_ext_base_funs (fastype_of var) |
|
223 |
val _ = writeln "Creating table" |
|
224 |
val table = map (mk_row thy base_funs) tl |
|
225 |
val _ = writeln "Searching for lexicographic order" |
|
226 |
val possible_order = search_table table |
|
227 |
in |
|
228 |
case possible_order of |
|
229 |
NONE => error (cat_lines ("Could not prove it by lexicographic order:"::(pr_err table thy tl base_funs))) |
|
230 |
| SOME order => let |
|
231 |
val clean_table = map (fn x => map (nth x) order) table |
|
232 |
val funs = map (nth base_funs) order |
|
233 |
val list = HOLogic.mk_list (fn x => x) (fastype_of var --> HOLogic.natT) funs |
|
234 |
val relterm = Abs ("x", fastype_of var, Const(measures, (fastype_of list) --> (range_type (fastype_of rel))) $ list) |
|
235 |
val crelterm = cterm_of thy relterm |
|
236 |
val _ = writeln ("Instantiating R with " ^ (string_of_cterm crelterm)) |
|
237 |
val _ = writeln "Proving subgoals" |
|
238 |
in |
|
239 |
next_st |> cterm_instantiate [(crel, crelterm)] |
|
240 |
|> SINGLE (rtac wf_measures 1) |> the |
|
241 |
|> fold prove_row clean_table |
|
242 |
|> Seq.single |
|
21131 | 243 |
end |
244 |
end |
|
245 |
end |
|
246 |
||
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
247 |
val lexicographic_order = Method.SIMPLE_METHOD o lexicographic_order_tac |
21201 | 248 |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
249 |
val setup = Method.add_methods [("lexicographic_order", Method.ctxt_args lexicographic_order, "termination prover for lexicographic orderings")] |
21131 | 250 |
|
251 |
end |