author | haftmann |
Tue, 06 Dec 2005 09:04:09 +0100 | |
changeset 18358 | 0a733e11021a |
parent 18293 | 4eaa654c92f2 |
child 18377 | 0e1d025d57b3 |
permissions | -rw-r--r-- |
2453 | 1 |
(* Title: HOLCF/domain/axioms.ML |
2 |
ID: $Id$ |
|
12030 | 3 |
Author: David von Oheimb |
2453 | 4 |
|
12030 | 5 |
Syntax generator for domain section. |
1274 | 6 |
*) |
7 |
||
8 |
structure Domain_Axioms = struct |
|
9 |
||
10 |
local |
|
11 |
||
12 |
open Domain_Library; |
|
13 |
infixr 0 ===>;infixr 0 ==>;infix 0 == ; |
|
14 |
infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<; |
|
15 |
infix 9 ` ; infix 9 `% ; infix 9 `%%; infixr 9 oo; |
|
16 |
||
17 |
fun infer_types thy' = map (inferT_axm (sign_of thy')); |
|
18 |
||
19 |
fun calc_axioms comp_dname (eqs : eq list) n (((dname,_),cons) : eq)= |
|
20 |
let |
|
21 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
22 |
(* ----- axioms and definitions concerning the isomorphism ------------------ *) |
1274 | 23 |
|
11531
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
24 |
val dc_abs = %%:(dname^"_abs"); |
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
25 |
val dc_rep = %%:(dname^"_rep"); |
1274 | 26 |
val x_name'= "x"; |
27 |
val x_name = idx_name eqs x_name' (n+1); |
|
4008 | 28 |
val dnam = Sign.base_name dname; |
1274 | 29 |
|
18083
cf7669049df5
cleaned up; renamed library function mk_cRep_CFun to list_ccomb; replaced rep_TFree with dest_TFree; added functions spair, mk_stuple
huffman
parents:
18075
diff
changeset
|
30 |
val abs_iso_ax = ("abs_iso", mk_trp(dc_rep`(dc_abs`%x_name') === %:x_name')); |
cf7669049df5
cleaned up; renamed library function mk_cRep_CFun to list_ccomb; replaced rep_TFree with dest_TFree; added functions spair, mk_stuple
huffman
parents:
18075
diff
changeset
|
31 |
val rep_iso_ax = ("rep_iso", mk_trp(dc_abs`(dc_rep`%x_name') === %:x_name')); |
1274 | 32 |
|
11531
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
33 |
val when_def = ("when_def",%%:(dname^"_when") == |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
34 |
foldr (uncurry /\ ) (/\x_name'((when_body cons (fn (x,y) => |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
35 |
Bound(1+length cons+x-y)))`(dc_rep`Bound 0))) (when_funs cons)); |
18084
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
36 |
|
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
37 |
val copy_def = let |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
38 |
fun idxs z x arg = if is_rec arg |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
39 |
then (cproj (Bound z) eqs (rec_of arg))`Bound(z-x) |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
40 |
else Bound(z-x); |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
41 |
fun one_con (con,args) = |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
42 |
foldr /\# (list_ccomb (%%:con, mapn (idxs (length args)) 1 args)) args; |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
43 |
in ("copy_def", %%:(dname^"_copy") == |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
44 |
/\"f" (list_ccomb (%%:(dname^"_when"), map one_con cons))) end; |
1274 | 45 |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
46 |
(* -- definitions concerning the constructors, discriminators and selectors - *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
47 |
|
18084
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
48 |
fun con_def m n (_,args) = let |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
49 |
fun idxs z x arg = (if is_lazy arg then fn t => %%:upN`t else I) (Bound(z-x)); |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
50 |
fun parms vs = mk_stuple (mapn (idxs(length vs)) 1 vs); |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
51 |
fun inj y 1 _ = y |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
52 |
| inj y _ 0 = %%:sinlN`y |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
53 |
| inj y i j = %%:sinrN`(inj y (i-1) (j-1)); |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
54 |
in foldr /\# (dc_abs`(inj (parms args) m n)) args end; |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
55 |
|
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
56 |
val con_defs = mapn (fn n => fn (con,args) => |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
57 |
(extern_name con ^"_def", %%:con == con_def (length cons) n (con,args))) 0 cons; |
3f767ed1f7ae
reimplement copy_def to use data constructor constants
huffman
parents:
18083
diff
changeset
|
58 |
|
4043 | 59 |
val dis_defs = let |
11531
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
60 |
fun ddef (con,_) = (dis_name con ^"_def",%%:(dis_name con) == |
18083
cf7669049df5
cleaned up; renamed library function mk_cRep_CFun to list_ccomb; replaced rep_TFree with dest_TFree; added functions spair, mk_stuple
huffman
parents:
18075
diff
changeset
|
61 |
list_ccomb(%%:(dname^"_when"),map |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
62 |
(fn (con',args) => (foldr /\# |
16778 | 63 |
(if con'=con then %%:TT_N else %%:FF_N) args)) cons)) |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
64 |
in map ddef cons end; |
1274 | 65 |
|
16224
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
66 |
val mat_defs = let |
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
67 |
fun mdef (con,_) = (mat_name con ^"_def",%%:(mat_name con) == |
18083
cf7669049df5
cleaned up; renamed library function mk_cRep_CFun to list_ccomb; replaced rep_TFree with dest_TFree; added functions spair, mk_stuple
huffman
parents:
18075
diff
changeset
|
68 |
list_ccomb(%%:(dname^"_when"),map |
16224
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
69 |
(fn (con',args) => (foldr /\# |
16778 | 70 |
(if con'=con |
71 |
then %%:returnN`(mk_ctuple (map (bound_arg args) args)) |
|
72 |
else %%:failN) args)) cons)) |
|
16224
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
73 |
in map mdef cons end; |
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
74 |
|
18113 | 75 |
val pat_defs = |
76 |
let |
|
77 |
fun pdef (con,args) = |
|
78 |
let |
|
79 |
val ps = mapn (fn n => fn _ => %:("pat" ^ string_of_int n)) 1 args; |
|
80 |
val xs = map (bound_arg args) args; |
|
81 |
val r = Bound (length args); |
|
18293
4eaa654c92f2
reimplement Case expression pattern matching to support lazy patterns
huffman
parents:
18292
diff
changeset
|
82 |
val rhs = case args of [] => %%:returnN ` HOLogic.unit |
4eaa654c92f2
reimplement Case expression pattern matching to support lazy patterns
huffman
parents:
18292
diff
changeset
|
83 |
| _ => foldr1 cpair_pat ps ` mk_ctuple xs; |
18113 | 84 |
fun one_con (con',args') = foldr /\# (if con'=con then rhs else %%:failN) args'; |
85 |
in (pat_name con ^"_def", list_comb (%%:(pat_name con), ps) == |
|
18293
4eaa654c92f2
reimplement Case expression pattern matching to support lazy patterns
huffman
parents:
18292
diff
changeset
|
86 |
list_ccomb(%%:(dname^"_when"), map one_con cons)) |
18113 | 87 |
end |
88 |
in map pdef cons end; |
|
18293
4eaa654c92f2
reimplement Case expression pattern matching to support lazy patterns
huffman
parents:
18292
diff
changeset
|
89 |
|
4043 | 90 |
val sel_defs = let |
16394
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16332
diff
changeset
|
91 |
fun sdef con n arg = Option.map (fn sel => (sel^"_def",%%:sel == |
18083
cf7669049df5
cleaned up; renamed library function mk_cRep_CFun to list_ccomb; replaced rep_TFree with dest_TFree; added functions spair, mk_stuple
huffman
parents:
18075
diff
changeset
|
92 |
list_ccomb(%%:(dname^"_when"),map |
16778 | 93 |
(fn (con',args) => if con'<>con then UU else |
16394
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16332
diff
changeset
|
94 |
foldr /\# (Bound (length args - n)) args) cons))) (sel_of arg); |
16842 | 95 |
in List.mapPartial I (List.concat(map (fn (con,args) => mapn (sdef con) 1 args) cons)) end; |
1274 | 96 |
|
97 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
98 |
(* ----- axiom and definitions concerning induction ------------------------- *) |
1274 | 99 |
|
16778 | 100 |
val reach_ax = ("reach", mk_trp(cproj (%%:fixN`%%(comp_dname^"_copy")) eqs n |
11531
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
101 |
`%x_name === %:x_name)); |
18085
ec9e36ece6bb
improve support for mutual recursion: now generates correct copy constant and induction theorem for mutually-recursive types; initial support for indirect recursion
huffman
parents:
18084
diff
changeset
|
102 |
val take_def = ("take_def",%%:(dname^"_take") == mk_lam("n",cproj |
18075 | 103 |
(%%:iterateN $ Bound 0 ` %%:(comp_dname^"_copy") ` UU) eqs n)); |
11531
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
104 |
val finite_def = ("finite_def",%%:(dname^"_finite") == mk_lam(x_name, |
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
105 |
mk_ex("n",(%%:(dname^"_take") $ Bound 0)`Bound 1 === Bound 1))); |
1274 | 106 |
|
4043 | 107 |
in (dnam, |
108 |
[abs_iso_ax, rep_iso_ax, reach_ax], |
|
109 |
[when_def, copy_def] @ |
|
18113 | 110 |
con_defs @ dis_defs @ mat_defs @ pat_defs @ sel_defs @ |
4043 | 111 |
[take_def, finite_def]) |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
112 |
end; (* let *) |
1274 | 113 |
|
8438 | 114 |
fun add_axioms_i x = #1 o PureThy.add_axioms_i (map Thm.no_attributes x); |
16332 | 115 |
fun add_axioms_infer axms thy = add_axioms_i (infer_types thy axms) thy; |
1274 | 116 |
|
18358 | 117 |
fun add_defs_i x = snd o (PureThy.add_defs_i false) (map Thm.no_attributes x); |
18292 | 118 |
fun add_defs_infer defs thy = add_defs_i (infer_types thy defs) thy; |
119 |
||
1274 | 120 |
in (* local *) |
121 |
||
4008 | 122 |
fun add_axioms (comp_dnam, eqs : eq list) thy' = let |
123 |
val comp_dname = Sign.full_name (sign_of thy') comp_dnam; |
|
1274 | 124 |
val dnames = map (fst o fst) eqs; |
125 |
val x_name = idx_name dnames "x"; |
|
11531
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
126 |
fun copy_app dname = %%:(dname^"_copy")`Bound 0; |
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
127 |
val copy_def = ("copy_def" , %%:(comp_dname^"_copy") == |
17811 | 128 |
/\"f"(foldr1 cpair (map copy_app dnames))); |
11531
d038246a62f2
Renamed functions % and %% to avoid clash with syntax for proof terms.
berghofe
parents:
8438
diff
changeset
|
129 |
val bisim_def = ("bisim_def",%%:(comp_dname^"_bisim")==mk_lam("R", |
1274 | 130 |
let |
131 |
fun one_con (con,args) = let |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
132 |
val nonrec_args = filter_out is_rec args; |
15570 | 133 |
val rec_args = List.filter is_rec args; |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
134 |
val recs_cnt = length rec_args; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
135 |
val allargs = nonrec_args @ rec_args |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
136 |
@ map (upd_vname (fn s=> s^"'")) rec_args; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
137 |
val allvns = map vname allargs; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
138 |
fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
139 |
val vns1 = map (vname_arg "" ) args; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
140 |
val vns2 = map (vname_arg "'") args; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
141 |
val allargs_cnt = length nonrec_args + 2*recs_cnt; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
142 |
val rec_idxs = (recs_cnt-1) downto 0; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
143 |
val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
144 |
(allargs~~((allargs_cnt-1) downto 0))); |
4755 | 145 |
fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
146 |
Bound (2*recs_cnt-i) $ Bound (recs_cnt-i); |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
147 |
val capps = foldr mk_conj (mk_conj( |
18083
cf7669049df5
cleaned up; renamed library function mk_cRep_CFun to list_ccomb; replaced rep_TFree with dest_TFree; added functions spair, mk_stuple
huffman
parents:
18075
diff
changeset
|
148 |
Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1), |
cf7669049df5
cleaned up; renamed library function mk_cRep_CFun to list_ccomb; replaced rep_TFree with dest_TFree; added functions spair, mk_stuple
huffman
parents:
18075
diff
changeset
|
149 |
Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2))) |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
150 |
(mapn rel_app 1 rec_args); |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
151 |
in foldr mk_ex (Library.foldr mk_conj |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
152 |
(map (defined o Bound) nonlazy_idxs,capps)) allvns end; |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
153 |
fun one_comp n (_,cons) =mk_all(x_name(n+1),mk_all(x_name(n+1)^"'",mk_imp( |
4755 | 154 |
proj (Bound 2) eqs n $ Bound 1 $ Bound 0, |
17811 | 155 |
foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU) |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
156 |
::map one_con cons)))); |
17811 | 157 |
in foldr1 mk_conj (mapn one_comp 0 eqs)end )); |
16332 | 158 |
fun add_one (thy,(dnam,axs,dfs)) = thy |
4043 | 159 |
|> Theory.add_path dnam |
18292 | 160 |
|> add_defs_infer dfs |
16332 | 161 |
|> add_axioms_infer axs |
4862 | 162 |
|> Theory.parent_path; |
15570 | 163 |
val thy = Library.foldl add_one (thy', mapn (calc_axioms comp_dname eqs) 0 eqs); |
4043 | 164 |
in thy |> Theory.add_path comp_dnam |
18292 | 165 |
|> add_defs_infer (bisim_def::(if length eqs>1 then [copy_def] else [])) |
4862 | 166 |
|> Theory.parent_path |
4043 | 167 |
end; |
1274 | 168 |
|
169 |
end; (* local *) |
|
170 |
end; (* struct *) |