author | paulson |
Mon, 09 Jan 2006 13:28:06 +0100 | |
changeset 18623 | 9a5419d5ca01 |
parent 18377 | 0e1d025d57b3 |
child 18928 | 042608ffa2ec |
permissions | -rw-r--r-- |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
1 |
(* Title: HOLCF/domain/extender.ML |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
2 |
ID: $Id$ |
14097 | 3 |
Author: David von Oheimb |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
4 |
|
12037 | 5 |
Theory extender for domain section, including new-style theory syntax. |
15187 | 6 |
|
7 |
###TODO: |
|
8 |
||
9 |
this definition |
|
10 |
domain empty = silly empty |
|
11 |
yields |
|
12 |
Exception- |
|
13 |
TERM |
|
14 |
("typ_of_term: bad encoding of type", |
|
15531 | 15 |
[Abs ("uu", "_", Const ("NONE", "_"))]) raised |
15187 | 16 |
but this works fine: |
17 |
domain Empty = silly Empty |
|
18 |
||
19 |
strange syntax errors are produced for: |
|
20 |
domain xx = xx ("x yy") |
|
21 |
domain 'a foo = foo (sel::"'a") |
|
22 |
and bar = bar ("'a dummy") |
|
23 |
||
1274 | 24 |
*) |
25 |
||
12037 | 26 |
signature DOMAIN_EXTENDER = |
27 |
sig |
|
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:
18083
diff
changeset
|
28 |
val add_domain: string * ((bstring * string list) * |
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:
18083
diff
changeset
|
29 |
(string * mixfix * (bool * string option * string) list) list) list |
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:
18083
diff
changeset
|
30 |
-> theory -> theory |
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:
18083
diff
changeset
|
31 |
val add_domain_i: string * ((bstring * string list) * |
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:
18083
diff
changeset
|
32 |
(string * mixfix * (bool * string option * typ) list) list) list |
12037 | 33 |
-> theory -> theory |
34 |
end; |
|
2446
c2a9bf6c0948
The previous log message was wrong. The correct one is:
oheimb
parents:
2445
diff
changeset
|
35 |
|
12037 | 36 |
structure Domain_Extender: DOMAIN_EXTENDER = |
1274 | 37 |
struct |
38 |
||
39 |
open Domain_Library; |
|
40 |
||
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
41 |
(* ----- general testing and preprocessing of constructor list -------------- *) |
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:
18083
diff
changeset
|
42 |
fun check_and_sort_domain (dtnvs: (string * typ list) list, |
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:
18083
diff
changeset
|
43 |
cons'' : ((string * mixfix * (bool * string option * typ) list) list) list) sg = |
4008 | 44 |
let |
7653 | 45 |
val defaultS = Sign.defaultS sg; |
1274 | 46 |
val test_dupl_typs = (case duplicates (map fst dtnvs) of |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
47 |
[] => false | dups => error ("Duplicate types: " ^ commas_quote dups)); |
15570 | 48 |
val test_dupl_cons = (case duplicates (map first (List.concat cons'')) of |
4008 | 49 |
[] => false | dups => error ("Duplicate constructors: " |
50 |
^ commas_quote dups)); |
|
16394
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16070
diff
changeset
|
51 |
val test_dupl_sels = (case duplicates (List.mapPartial second |
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16070
diff
changeset
|
52 |
(List.concat (map third (List.concat cons'')))) of |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
53 |
[] => false | dups => error("Duplicate selectors: "^commas_quote dups)); |
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:
17325
diff
changeset
|
54 |
val test_dupl_tvars = exists(fn s=>case duplicates(map(fst o dest_TFree)s)of |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
55 |
[] => false | dups => error("Duplicate type arguments: " |
4008 | 56 |
^commas_quote dups)) (map snd dtnvs); |
57 |
(* test for free type variables, illegal sort constraints on rhs, |
|
58 |
non-pcpo-types and invalid use of recursive type; |
|
59 |
replace sorts in type variables on rhs *) |
|
60 |
fun analyse_equation ((dname,typevars),cons') = |
|
61 |
let |
|
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:
17325
diff
changeset
|
62 |
val tvars = map dest_TFree typevars; |
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:
18083
diff
changeset
|
63 |
val distinct_typevars = map TFree tvars; |
4008 | 64 |
fun rm_sorts (TFree(s,_)) = TFree(s,[]) |
65 |
| rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts) |
|
66 |
| rm_sorts (TVar(s,_)) = TVar(s,[]) |
|
67 |
and remove_sorts l = map rm_sorts l; |
|
15601
2de79f493856
domain package now permits indirect recursion with these type constructors: *, ->, ++, **, u
huffman
parents:
15570
diff
changeset
|
68 |
val indirect_ok = ["*","Cfun.->","Ssum.++","Sprod.**","Up.u"] |
17325 | 69 |
fun analyse indirect (TFree(v,s)) = (case AList.lookup (op =) tvars v of |
15531 | 70 |
NONE => error ("Free type variable " ^ quote v ^ " on rhs.") |
71 |
| SOME sort => if eq_set_string (s,defaultS) orelse |
|
4030 | 72 |
eq_set_string (s,sort ) |
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:
18083
diff
changeset
|
73 |
then TFree(v,sort) |
14097 | 74 |
else error ("Inconsistent sort constraint" ^ |
75 |
" for type variable " ^ quote v)) |
|
17325 | 76 |
| analyse indirect (t as Type(s,typl)) = (case AList.lookup (op =) dtnvs s of |
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:
17325
diff
changeset
|
77 |
NONE => if s mem indirect_ok |
15601
2de79f493856
domain package now permits indirect recursion with these type constructors: *, ->, ++, **, u
huffman
parents:
15570
diff
changeset
|
78 |
then Type(s,map (analyse false) typl) |
2de79f493856
domain package now permits indirect recursion with these type constructors: *, ->, ++, **, u
huffman
parents:
15570
diff
changeset
|
79 |
else Type(s,map (analyse true) typl) |
15531 | 80 |
| SOME typevars => if indirect |
15187 | 81 |
then error ("Indirect recursion of type " ^ |
82 |
quote (string_of_typ sg t)) |
|
83 |
else if dname <> s orelse (** BUG OR FEATURE?: |
|
84 |
mutual recursion may use different arguments **) |
|
85 |
remove_sorts typevars = remove_sorts typl |
|
86 |
then Type(s,map (analyse true) typl) |
|
87 |
else error ("Direct recursion of type " ^ |
|
88 |
quote (string_of_typ sg t) ^ |
|
4753 | 89 |
" with different arguments")) |
15187 | 90 |
| analyse indirect (TVar _) = Imposs "extender:analyse"; |
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:
18083
diff
changeset
|
91 |
fun check_pcpo T = if pcpo_type sg T then T |
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:
18083
diff
changeset
|
92 |
else error("Constructor argument type is not of sort pcpo: "^string_of_typ sg T); |
15187 | 93 |
val analyse_con = upd_third (map (upd_third (check_pcpo o analyse false))); |
4030 | 94 |
in ((dname,distinct_typevars), map analyse_con cons') end; |
4008 | 95 |
in ListPair.map analyse_equation (dtnvs,cons'') |
96 |
end; (* let *) |
|
97 |
||
4030 | 98 |
(* ----- calls for building new thy and thms -------------------------------- *) |
1274 | 99 |
|
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:
18083
diff
changeset
|
100 |
fun gen_add_domain prep_typ (comp_dnam, eqs''') thy''' = |
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:
18083
diff
changeset
|
101 |
let |
4008 | 102 |
val dtnvs = map ((fn (dname,vs) => |
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:
18083
diff
changeset
|
103 |
(Sign.full_name thy''' dname, map (str2typ thy''') vs)) |
4008 | 104 |
o fst) eqs'''; |
105 |
val cons''' = map snd eqs'''; |
|
106 |
fun thy_type (dname,tvars) = (Sign.base_name dname, length tvars, NoSyn); |
|
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:
17325
diff
changeset
|
107 |
fun thy_arity (dname,tvars) = (dname, map (snd o dest_TFree) tvars, pcpoS); |
4008 | 108 |
val thy'' = thy''' |> Theory.add_types (map thy_type dtnvs) |
109 |
|> Theory.add_arities_i (map thy_arity dtnvs); |
|
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:
18083
diff
changeset
|
110 |
val cons'' = map (map (upd_third (map (upd_third (prep_typ thy''))))) cons'''; |
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:
18083
diff
changeset
|
111 |
val eqs' = check_and_sort_domain (dtnvs,cons'') thy''; |
4008 | 112 |
val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dnam,eqs'); |
1274 | 113 |
val dts = map (Type o fst) eqs'; |
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:
18083
diff
changeset
|
114 |
val new_dts = map (fn ((s,Ts),_) => (s, map (fst o dest_TFree) Ts)) eqs'; |
15570 | 115 |
fun strip ss = Library.drop (find_index_eq "'" ss +1, ss); |
11728 | 116 |
fun typid (Type (id,_)) = |
117 |
let val c = hd (Symbol.explode (Sign.base_name id)) |
|
118 |
in if Symbol.is_letter c then c else "t" end |
|
119 |
| typid (TFree (id,_) ) = hd (strip (tl (Symbol.explode id))) |
|
120 |
| typid (TVar ((id,_),_)) = hd (tl (Symbol.explode id)); |
|
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:
18083
diff
changeset
|
121 |
fun one_con (con,mx,args) = |
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:
18083
diff
changeset
|
122 |
((Syntax.const_name con mx), |
4008 | 123 |
ListPair.map (fn ((lazy,sel,tp),vn) => ((lazy, |
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:
18083
diff
changeset
|
124 |
find_index_eq tp dts, |
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:
18083
diff
changeset
|
125 |
DatatypeAux.dtyp_of_typ new_dts tp), |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
126 |
sel,vn)) |
4122 | 127 |
(args,(mk_var_names(map (typid o third) args))) |
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:
18083
diff
changeset
|
128 |
) : cons; |
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:
18083
diff
changeset
|
129 |
val eqs = map (fn (dtnvs,cons') => (dtnvs, map one_con cons')) eqs' : eq list; |
4122 | 130 |
val thy = thy' |> Domain_Axioms.add_axioms (comp_dnam,eqs); |
12037 | 131 |
val (theorems_thy, (rewss, take_rews)) = (foldl_map (fn (thy0,eq) => |
132 |
Domain_Theorems.theorems (eq,eqs) thy0) (thy,eqs)) |
|
133 |
|>>> Domain_Theorems.comp_theorems (comp_dnam, eqs); |
|
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:
18083
diff
changeset
|
134 |
in |
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:
18083
diff
changeset
|
135 |
theorems_thy |
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:
18083
diff
changeset
|
136 |
|> Theory.add_path (Sign.base_name comp_dnam) |
18377 | 137 |
|> (snd o (PureThy.add_thmss [(("rews", List.concat rewss @ take_rews), [])])) |
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:
18083
diff
changeset
|
138 |
|> Theory.parent_path |
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:
18083
diff
changeset
|
139 |
end; |
12037 | 140 |
|
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:
18083
diff
changeset
|
141 |
val add_domain_i = gen_add_domain Sign.certify_typ; |
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:
18083
diff
changeset
|
142 |
val add_domain = gen_add_domain str2typ; |
12037 | 143 |
|
144 |
||
145 |
(** outer syntax **) |
|
146 |
||
17057 | 147 |
local structure P = OuterParse and K = OuterKeyword in |
12037 | 148 |
|
149 |
val dest_decl = |
|
150 |
P.$$$ "(" |-- Scan.optional (P.$$$ "lazy" >> K true) false -- |
|
16394
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16070
diff
changeset
|
151 |
(P.name >> SOME) -- (P.$$$ "::" |-- P.typ) --| P.$$$ ")" >> P.triple1 |
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16070
diff
changeset
|
152 |
|| P.$$$ "(" |-- P.$$$ "lazy" |-- P.typ --| P.$$$ ")" |
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16070
diff
changeset
|
153 |
>> (fn t => (true,NONE,t)) |
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16070
diff
changeset
|
154 |
|| P.typ >> (fn t => (false,NONE,t)); |
1274 | 155 |
|
12037 | 156 |
val cons_decl = |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12037
diff
changeset
|
157 |
P.name -- Scan.repeat dest_decl -- P.opt_mixfix |
12037 | 158 |
>> (fn ((c, ds), mx) => (c, mx, ds)); |
159 |
||
14097 | 160 |
val type_var' = (P.type_ident ^^ |
161 |
Scan.optional (P.$$$ "::" ^^ P.!!! P.sort) ""); |
|
162 |
val type_args' = type_var' >> single || |
|
163 |
P.$$$ "(" |-- P.!!! (P.list1 type_var' --| P.$$$ ")") || |
|
164 |
Scan.succeed []; |
|
165 |
||
166 |
val domain_decl = (type_args' -- P.name >> Library.swap) -- |
|
167 |
(P.$$$ "=" |-- P.enum1 "|" cons_decl); |
|
12037 | 168 |
val domains_decl = |
169 |
Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.and_list1 domain_decl |
|
170 |
>> (fn (opt_name, doms) => |
|
15531 | 171 |
(case opt_name of NONE => space_implode "_" (map (#1 o #1) doms) | SOME s => s, doms)); |
12037 | 172 |
|
173 |
val domainP = |
|
174 |
OuterSyntax.command "domain" "define recursive domains (HOLCF)" K.thy_decl |
|
175 |
(domains_decl >> (Toplevel.theory o add_domain)); |
|
176 |
||
177 |
||
178 |
val _ = OuterSyntax.add_keywords ["lazy"]; |
|
179 |
val _ = OuterSyntax.add_parsers [domainP]; |
|
180 |
||
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:
18083
diff
changeset
|
181 |
end; (* local structure *) |
12037 | 182 |
|
183 |
end; |