author | oheimb |
Wed, 03 Apr 1996 19:27:14 +0200 | |
changeset 1637 | b8a8ae2e5de1 |
parent 1461 | 6bcb44e4d6e5 |
child 1819 | 245721624c8d |
permissions | -rw-r--r-- |
1274 | 1 |
(* library.ML |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
2 |
Author : David von Oheimb |
1274 | 3 |
Created: 18-Jul-95 extracted from syntax.ML, axioms.ML, extender.ML, interface.ML |
4 |
Updated: 30-Aug-95 |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
5 |
Updated: 20-Oct-95 small improvement for atomize |
1274 | 6 |
Copyright 1995 TU Muenchen |
7 |
*) |
|
8 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
9 |
(* ----- general support ---------------------------------------------------- *) |
1274 | 10 |
|
11 |
fun Id x = x; |
|
12 |
||
13 |
fun mapn f n [] = [] |
|
14 |
| mapn f n (x::xs) = (f n x) :: mapn f (n+1) xs; |
|
15 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
16 |
fun foldr'' f (l,f2) = let fun itr [] = raise LIST "foldr''" |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
17 |
| itr [a] = f2 a |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
18 |
| itr (a::l) = f(a, itr l) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
19 |
in itr l end; |
1274 | 20 |
fun foldr' f l = foldr'' f (l,Id); |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
21 |
fun map_cumulr f start xs = foldr (fn (x,(ys,res))=>case f(x,res) of (y,res2) => |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
22 |
(y::ys,res2)) (xs,([],start)); |
1274 | 23 |
|
24 |
||
25 |
fun first (x,_,_) = x; fun second (_,x,_) = x; fun third (_,_,x) = x; |
|
26 |
fun upd_first f (x,y,z) = (f x, y, z); |
|
27 |
fun upd_second f (x,y,z) = ( x, f y, z); |
|
28 |
fun upd_third f (x,y,z) = ( x, y, f z); |
|
29 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
30 |
fun atomize thm = let val r_inst = read_instantiate; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
31 |
fun at thm = case concl_of thm of |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
32 |
_$(Const("op &",_)$_$_) => at(thm RS conjunct1)@at(thm RS conjunct2) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
33 |
| _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (r_inst [("x","?"^s)] spec)) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
34 |
| _ => [thm]; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
35 |
in map zero_var_indexes (at thm) end; |
1274 | 36 |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
37 |
(* ----- specific support for domain ---------------------------------------- *) |
1274 | 38 |
|
39 |
structure Domain_Library = struct |
|
40 |
||
41 |
exception Impossible of string; |
|
42 |
fun Imposs msg = raise Impossible ("Domain:"^msg); |
|
43 |
||
44 |
(* ----- name handling ----- *) |
|
45 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
46 |
val strip_esc = let fun strip ("'" :: c :: cs) = c :: strip cs |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
47 |
| strip ["'"] = [] |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
48 |
| strip (c :: cs) = c :: strip cs |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
49 |
| strip [] = []; |
1274 | 50 |
in implode o strip o explode end; |
51 |
||
52 |
fun extern_name con = case explode con of |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
53 |
("o"::"p"::" "::rest) => implode rest |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
54 |
| _ => con; |
1274 | 55 |
fun dis_name con = "is_"^ (extern_name con); |
56 |
fun dis_name_ con = "is_"^ (strip_esc con); |
|
57 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
58 |
(* make distinct names out of the type list, |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
59 |
forbidding "o", "x..","f..","P.." as names *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
60 |
(* a number string is added if necessary *) |
1274 | 61 |
fun mk_var_names types : string list = let |
62 |
fun typid (Type (id,_) ) = hd (explode id) |
|
63 |
| typid (TFree (id,_) ) = hd (tl (explode id)) |
|
64 |
| typid (TVar ((id,_),_)) = hd (tl (explode id)); |
|
65 |
fun nonreserved id = let val cs = explode id in |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
66 |
if not(hd cs mem ["x","f","P"]) then id |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
67 |
else implode(chr(1+ord (hd cs))::tl cs) end; |
1274 | 68 |
fun index_vnames(vn::vns,tab) = |
69 |
(case assoc(tab,vn) of |
|
70 |
None => if vn mem vns |
|
71 |
then (vn^"1") :: index_vnames(vns,(vn,2)::tab) |
|
72 |
else vn :: index_vnames(vns, tab) |
|
73 |
| Some(i) => (vn^(string_of_int i)) :: index_vnames(vns,(vn,i+1)::tab)) |
|
74 |
| index_vnames([],tab) = []; |
|
75 |
in index_vnames(map (nonreserved o typid) types,[("o",1)]) end; |
|
76 |
||
77 |
fun type_name_vars (Type(name,typevars)) = (name,typevars) |
|
78 |
| type_name_vars _ = Imposs "library:type_name_vars"; |
|
79 |
||
80 |
(* ----- constructor list handling ----- *) |
|
81 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
82 |
type cons = (string * (* operator name of constr *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
83 |
((bool*int)* (* (lazy,recursive element or ~1) *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
84 |
string* (* selector name *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
85 |
string) (* argument name *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
86 |
list); (* argument list *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
87 |
type eq = (string * (* name of abstracted type *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
88 |
typ list) * (* arguments of abstracted type *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
89 |
cons list; (* represented type, as a constructor list *) |
1274 | 90 |
|
91 |
val rec_of = snd o first; |
|
92 |
val is_lazy = fst o first; |
|
93 |
val sel_of = second; |
|
94 |
val vname = third; |
|
95 |
val upd_vname = upd_third; |
|
96 |
fun is_rec arg = rec_of arg >=0; |
|
97 |
fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg); |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
98 |
fun nonlazy args = map vname (filter_out is_lazy args); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
99 |
fun nonlazy_rec args = map vname (filter is_nonlazy_rec args); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
100 |
|
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
101 |
(* ----- support for type and mixfix expressions ----- *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
102 |
|
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
103 |
fun mk_tvar s = TFree("'"^s,["pcpo"]); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
104 |
fun mk_typ t (S,T) = Type(t,[S,T]); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
105 |
infixr 5 -->; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
106 |
infixr 6 ~>; val op ~> = mk_typ "->"; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
107 |
val NoSyn' = ThyOps.Mixfix NoSyn; |
1274 | 108 |
|
109 |
(* ----- support for term expressions ----- *) |
|
110 |
||
111 |
fun % s = Free(s,dummyT); |
|
112 |
fun %# arg = %(vname arg); |
|
113 |
fun %% s = Const(s,dummyT); |
|
114 |
||
115 |
local open HOLogic in |
|
116 |
val mk_trp = mk_Trueprop; |
|
117 |
fun mk_conj (S,T) = conj $ S $ T; |
|
118 |
fun mk_disj (S,T) = disj $ S $ T; |
|
119 |
fun mk_imp (S,T) = imp $ S $ T; |
|
120 |
fun mk_lam (x,T) = Abs(x,dummyT,T); |
|
121 |
fun mk_all (x,P) = HOLogic.mk_all (x,dummyT,P); |
|
122 |
local |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
123 |
fun tf (Type (s,args)) = foldl (op $) (%s,map tf args) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
124 |
| tf (TFree(s,_ )) = %s |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
125 |
| tf _ = Imposs "mk_constrainall"; |
1274 | 126 |
in |
127 |
fun mk_constrain (typ,T) = %%"_constrain" $ T $ tf typ; |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
128 |
fun mk_constrainall (x,typ,P) = %%"All" $ (%%"_constrainAbs" $ mk_lam(x,P) $ tf typ); |
1274 | 129 |
end; |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
130 |
|
1274 | 131 |
fun mk_ex (x,P) = mk_exists (x,dummyT,P); |
132 |
fun mk_not P = Const("not" ,boolT --> boolT) $ P; |
|
133 |
end; |
|
134 |
||
135 |
fun mk_All (x,P) = %%"all" $ mk_lam(x,P); (* meta universal quantification *) |
|
136 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
137 |
infixr 0 ===>;fun S ===> T = %%"==>" $ S $ T; |
1274 | 138 |
infixr 0 ==>;fun S ==> T = mk_trp S ===> mk_trp T; |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
139 |
infix 0 ==; fun S == T = %%"==" $ S $ T; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
140 |
infix 1 ===; fun S === T = %%"op =" $ S $ T; |
1274 | 141 |
infix 1 ~=; fun S ~= T = mk_not (S === T); |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
142 |
infix 1 <<; fun S << T = %%"op <<" $ S $ T; |
1274 | 143 |
infix 1 ~<<; fun S ~<< T = mk_not (S << T); |
144 |
||
145 |
infix 9 ` ; fun f` x = %%"fapp" $ f $ x; |
|
146 |
infix 9 `% ; fun f`% s = f` % s; |
|
147 |
infix 9 `%%; fun f`%%s = f` %%s; |
|
148 |
fun mk_cfapp (F,As) = foldl (op `) (F,As); |
|
149 |
fun con_app2 con f args = mk_cfapp(%%con,map f args); |
|
150 |
fun con_app con = con_app2 con %#; |
|
151 |
fun if_rec arg f y = if is_rec arg then f (rec_of arg) else y; |
|
152 |
fun app_rec_arg p arg = if_rec arg (fn n => fn x => (p n)`x) Id (%# arg); |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
153 |
fun prj _ _ y 1 _ = y |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
154 |
| prj f1 _ y _ 0 = f1 y |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
155 |
| prj f1 f2 y i j = prj f1 f2 (f2 y) (i-1) (j-1); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
156 |
val cproj = prj (fn S => %%"cfst"`S) (fn S => %%"csnd"`S); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
157 |
val proj = prj (fn S => %%"fst" $S) (fn S => %%"snd" $S); |
1274 | 158 |
fun lift tfn = foldr (fn (x,t)=> (mk_trp(tfn x) ===> t)); |
159 |
||
160 |
fun /\ v T = %%"fabs" $ mk_lam(v,T); |
|
161 |
fun /\# (arg,T) = /\ (vname arg) T; |
|
162 |
infixr 9 oo; fun S oo T = %%"cfcomp"`S`T; |
|
163 |
val UU = %%"UU"; |
|
164 |
fun strict f = f`UU === UU; |
|
165 |
fun defined t = t ~= UU; |
|
166 |
fun cpair (S,T) = %%"cpair"`S`T; |
|
167 |
fun lift_defined f = lift (fn x => defined (f x)); |
|
168 |
fun bound_arg vns v = Bound(length vns-find(v,vns)-1); |
|
169 |
||
170 |
fun cont_eta_contract (Const("fabs",TT) $ Abs(a,T,body)) = |
|
171 |
(case cont_eta_contract body of |
|
172 |
body' as (Const("fapp",Ta) $ f $ Bound 0) => |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
173 |
if not (0 mem loose_bnos f) then incr_boundvars ~1 f |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
174 |
else Const("fabs",TT) $ Abs(a,T,body') |
1274 | 175 |
| body' => Const("fabs",TT) $ Abs(a,T,body')) |
176 |
| cont_eta_contract(f$t) = cont_eta_contract f $ cont_eta_contract t |
|
177 |
| cont_eta_contract t = t; |
|
178 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
179 |
fun idx_name dnames s n = s^(if length dnames = 1 then "" else string_of_int n); |
1274 | 180 |
fun when_funs cons = if length cons = 1 then ["f"] |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
181 |
else mapn (fn n => K("f"^(string_of_int n))) 1 cons; |
1274 | 182 |
fun when_body cons funarg = let |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
183 |
fun one_fun n (_,[] ) = /\ "dummy" (funarg(1,n)) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
184 |
| one_fun n (_,args) = let |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
185 |
val l2 = length args; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
186 |
fun idxs m arg = (if is_lazy arg then fn x=> %%"lift"`%%"ID"`x |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
187 |
else Id) (Bound(l2-m)); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
188 |
in cont_eta_contract (foldr'' |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
189 |
(fn (a,t) => %%"ssplit"`(/\# (a,t))) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
190 |
(args, |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
191 |
fn a=> /\#(a,(mk_cfapp(funarg(l2,n),mapn idxs 1 args)))) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
192 |
) end; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
193 |
in (if length cons = 1 andalso length(snd(hd cons)) <= 1 |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
194 |
then fn t => %%"strictify"`t else Id) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
195 |
(foldr' (fn (x,y)=> %%"sswhen"`x`y) (mapn one_fun 1 cons)) end; |
1274 | 196 |
end; (* struct *) |