16386
|
1 |
(* Title: HOLCF/cont_proc.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Brian Huffman
|
|
4 |
*)
|
|
5 |
|
|
6 |
signature CONT_PROC =
|
|
7 |
sig
|
|
8 |
val is_lcf_term: term -> bool
|
|
9 |
val cont_thms: term -> thm list
|
16403
|
10 |
val all_cont_thms: term -> thm list
|
16629
|
11 |
val cont_tac: int -> tactic
|
16386
|
12 |
val cont_proc: theory -> simproc
|
|
13 |
val setup: (theory -> theory) list
|
|
14 |
end;
|
|
15 |
|
|
16 |
structure ContProc: CONT_PROC =
|
|
17 |
struct
|
|
18 |
|
|
19 |
(** theory context references **)
|
|
20 |
|
|
21 |
val cont_K = thm "cont_const";
|
|
22 |
val cont_I = thm "cont_id";
|
|
23 |
val cont_A = thm "cont2cont_Rep_CFun";
|
|
24 |
val cont_L = thm "cont2cont_LAM";
|
|
25 |
val cont_R = thm "cont_Rep_CFun2";
|
|
26 |
|
|
27 |
(* checks whether a term is written entirely in the LCF sublanguage *)
|
|
28 |
fun is_lcf_term (Const("Cfun.Rep_CFun",_) $ t $ u) = is_lcf_term t andalso is_lcf_term u
|
|
29 |
| is_lcf_term (Const("Cfun.Abs_CFun",_) $ Abs (_,_,t)) = is_lcf_term t
|
|
30 |
| is_lcf_term (_ $ _) = false
|
|
31 |
| is_lcf_term (Abs _) = false
|
|
32 |
| is_lcf_term _ = true; (* Const, Free, Var, and Bound are OK *)
|
|
33 |
|
|
34 |
(*
|
|
35 |
efficiently generates a cont thm for every LAM abstraction in a term,
|
|
36 |
using forward proof and reusing common subgoals
|
|
37 |
*)
|
|
38 |
local
|
|
39 |
fun var 0 = [SOME cont_I]
|
|
40 |
| var n = NONE :: var (n-1);
|
|
41 |
|
|
42 |
fun k NONE = cont_K
|
|
43 |
| k (SOME x) = x;
|
|
44 |
|
|
45 |
fun ap NONE NONE = NONE
|
|
46 |
| ap x y = SOME (k y RS (k x RS cont_A));
|
|
47 |
|
|
48 |
fun zip [] [] = []
|
|
49 |
| zip [] (y::ys) = (ap NONE y ) :: zip [] ys
|
|
50 |
| zip (x::xs) [] = (ap x NONE) :: zip xs []
|
|
51 |
| zip (x::xs) (y::ys) = (ap x y ) :: zip xs ys
|
|
52 |
|
|
53 |
fun lam [] = ([], cont_K)
|
|
54 |
| lam (x::ys) = let
|
|
55 |
(* should use "standard" for thms that are used multiple times *)
|
|
56 |
(* it seems to allow for sharing in explicit proof objects *)
|
|
57 |
val x' = standard (k x);
|
|
58 |
val Lx = x' RS cont_L;
|
|
59 |
in (map (fn y => SOME (k y RS Lx)) ys, x') end;
|
|
60 |
|
|
61 |
(* first list: cont thm for each dangling bound variable *)
|
16403
|
62 |
(* second list: cont thm for each LAM in t *)
|
|
63 |
(* if b = false, only return cont thm for outermost LAMs *)
|
|
64 |
fun cont_thms1 b (Const _ $ f $ t) = let
|
|
65 |
val (cs1,ls1) = cont_thms1 b f;
|
|
66 |
val (cs2,ls2) = cont_thms1 b t;
|
|
67 |
in (zip cs1 cs2, if b then ls1 @ ls2 else []) end
|
|
68 |
| cont_thms1 b (Const _ $ Abs (_,_,t)) = let
|
|
69 |
val (cs,ls) = cont_thms1 b t;
|
16386
|
70 |
val (cs',l) = lam cs;
|
|
71 |
in (cs',l::ls) end
|
16403
|
72 |
| cont_thms1 _ (Bound n) = (var n, [])
|
|
73 |
| cont_thms1 _ _ = ([],[]);
|
16386
|
74 |
in
|
|
75 |
(* precondition: is_lcf_term t = true *)
|
16403
|
76 |
fun cont_thms t = snd (cont_thms1 false t);
|
|
77 |
fun all_cont_thms t = snd (cont_thms1 true t);
|
16386
|
78 |
end;
|
|
79 |
|
|
80 |
(*
|
|
81 |
Given the term "cont f", the procedure tries to construct the
|
|
82 |
theorem "cont f == True". If this theorem cannot be completely
|
|
83 |
solved by the introduction rules, then the procedure returns a
|
|
84 |
conditional rewrite rule with the unsolved subgoals as premises.
|
|
85 |
*)
|
|
86 |
|
|
87 |
local
|
16629
|
88 |
val rules = [cont_K, cont_I, cont_R, cont_A, cont_L];
|
|
89 |
|
|
90 |
fun cont_tac_of_term (Const("Cont.cont",_) $ f) =
|
16386
|
91 |
let
|
|
92 |
val f' = Const("Cfun.Abs_CFun",dummyT) $ f;
|
16629
|
93 |
in
|
|
94 |
if is_lcf_term f'
|
|
95 |
then rtac (hd (cont_thms f'))
|
|
96 |
else REPEAT_ALL_NEW (resolve_tac rules)
|
|
97 |
end
|
|
98 |
| cont_tac_of_term _ = K no_tac;
|
16386
|
99 |
in
|
16629
|
100 |
val cont_tac = SUBGOAL (fn (t,i) => cont_tac_of_term (HOLogic.dest_Trueprop t) i);
|
|
101 |
end;
|
|
102 |
|
|
103 |
local
|
|
104 |
fun solve_cont thy _ t =
|
|
105 |
let
|
|
106 |
val tr = instantiate' [] [SOME (cterm_of thy t)] Eq_TrueI;
|
|
107 |
in Option.map fst (Seq.pull (cont_tac 1 tr)) end
|
|
108 |
in
|
|
109 |
fun cont_proc thy = Simplifier.simproc thy "cont_proc" ["cont f"] solve_cont;
|
16386
|
110 |
end;
|
|
111 |
|
|
112 |
val setup =
|
|
113 |
[fn thy => Simplifier.change_simpset_of (op addsimprocs) [cont_proc thy] thy];
|
|
114 |
|
|
115 |
end;
|