author | oheimb |
Thu, 30 Oct 1997 14:19:01 +0100 | |
changeset 4043 | 35766855f344 |
parent 4030 | ca44afcc259c |
child 4122 | f63c283cefaf |
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$ |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
3 |
Author : David von Oheimb |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
4 |
Copyright 1995, 1996 TU Muenchen |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
5 |
|
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
6 |
theory extender for domain section |
1274 | 7 |
*) |
8 |
||
2446
c2a9bf6c0948
The previous log message was wrong. The correct one is:
oheimb
parents:
2445
diff
changeset
|
9 |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
10 |
structure Domain_Extender = |
1274 | 11 |
struct |
12 |
||
13 |
local |
|
14 |
||
15 |
open Domain_Library; |
|
16 |
||
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
17 |
(* ----- general testing and preprocessing of constructor list -------------- *) |
1274 | 18 |
|
4008 | 19 |
fun check_and_sort_domain (dtnvs: (string * typ list) list, cons'' : |
20 |
((string * ThyOps.cmixfix * (bool*string*typ) list) list) list) sg = |
|
21 |
let |
|
22 |
val defaultS = Type.defaultS (tsig_of sg); |
|
1274 | 23 |
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
|
24 |
[] => false | dups => error ("Duplicate types: " ^ commas_quote dups)); |
4008 | 25 |
val test_dupl_cons = (case duplicates (map first (flat cons'')) of |
26 |
[] => false | dups => error ("Duplicate constructors: " |
|
27 |
^ commas_quote dups)); |
|
28 |
val test_dupl_sels = (case duplicates |
|
29 |
(map second (flat (map third (flat cons'')))) of |
|
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
30 |
[] => false | dups => error("Duplicate selectors: "^commas_quote dups)); |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
31 |
val test_dupl_tvars = exists(fn s=>case duplicates(map(fst o rep_TFree)s)of |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
32 |
[] => false | dups => error("Duplicate type arguments: " |
4008 | 33 |
^commas_quote dups)) (map snd dtnvs); |
34 |
(* test for free type variables, illegal sort constraints on rhs, |
|
35 |
non-pcpo-types and invalid use of recursive type; |
|
36 |
replace sorts in type variables on rhs *) |
|
37 |
fun analyse_equation ((dname,typevars),cons') = |
|
38 |
let |
|
39 |
val tvars = map rep_TFree typevars; |
|
4030 | 40 |
fun distinct_name s = "'"^Sign.base_name dname^"_"^s; |
41 |
val distinct_typevars = map (fn (n,sort) => |
|
42 |
TFree (distinct_name n,sort)) tvars; |
|
4008 | 43 |
fun rm_sorts (TFree(s,_)) = TFree(s,[]) |
44 |
| rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts) |
|
45 |
| rm_sorts (TVar(s,_)) = TVar(s,[]) |
|
46 |
and remove_sorts l = map rm_sorts l; |
|
47 |
fun analyse(TFree(v,s)) = (case assoc_string(tvars,v) of |
|
48 |
None => error ("Free type variable " ^ v ^ " on rhs.") |
|
49 |
| Some sort => if eq_set_string (s,defaultS) orelse |
|
4030 | 50 |
eq_set_string (s,sort ) |
51 |
then TFree(distinct_name v,sort) |
|
4008 | 52 |
else error ("Additional constraint on rhs "^ |
53 |
"for type variable "^quote v)) |
|
4030 | 54 |
| analyse(Type(s,typl)) = if s <> dname |
55 |
then Type(s,map analyse typl) |
|
56 |
else if remove_sorts typevars = remove_sorts typl |
|
4008 | 57 |
then Type(s,map analyse typl) |
58 |
else error ("Recursion of type " ^ s ^ |
|
4030 | 59 |
" with different arguments") |
4008 | 60 |
| analyse(TVar _) = Imposs "extender:analyse"; |
61 |
fun check_pcpo t = (pcpo_type sg t orelse |
|
62 |
error("Not a pcpo type: "^string_of_typ sg t); t); |
|
63 |
val analyse_con = upd_third (map (upd_third (check_pcpo o analyse))); |
|
4030 | 64 |
in ((dname,distinct_typevars), map analyse_con cons') end; |
4008 | 65 |
in ListPair.map analyse_equation (dtnvs,cons'') |
66 |
end; (* let *) |
|
67 |
||
68 |
fun check_gen_by sg' (typs': string list,cnstrss': string list list) = let |
|
1274 | 69 |
val test_dupl_typs = (case duplicates typs' of [] => false |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
70 |
| dups => error ("Duplicate types: " ^ commas_quote dups)); |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
71 |
val test_dupl_cnstrs = map (fn cs => (case duplicates cs of [] => false |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
72 |
| ds => error ("Duplicate constructors: " ^ commas_quote ds))) cnstrss'; |
4008 | 73 |
val tycons = map fst (#tycons(Type.rep_tsig (tsig_of sg'))); |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
74 |
val test_types = forall (fn t => t mem tycons orelse |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
75 |
error("Unknown type: "^t)) typs'; |
1274 | 76 |
val cnstrss = let |
4008 | 77 |
fun type_of c = case (Sign.const_type sg' c) of Some t => t |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
78 |
| None => error ("Unknown constructor: "^c); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
79 |
fun args_result_type (t as (Type(tn,[arg,rest]))) = |
4043 | 80 |
if tn = cfun_arrow orelse tn = "=>" |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
81 |
then let val (ts,r) = args_result_type rest in (arg::ts,r) end |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
82 |
else ([],t) |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
83 |
| args_result_type t = ([],t); |
1274 | 84 |
in map (map (fn cn => let val (args,res) = args_result_type (type_of cn) in |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
85 |
(cn,mk_var_names args,(args,res)) end)) cnstrss' |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
86 |
: (string * (* operator name of constr *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
87 |
string list * (* argument name list *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
88 |
(typ list * (* argument types *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
89 |
typ)) (* result type *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
90 |
list list end; |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
91 |
fun test_equal_type tn (cn,_,(_,rt)) = fst (rep_Type rt) = tn orelse |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
92 |
error("Inappropriate result type for constructor "^cn); |
4030 | 93 |
val typs = ListPair.map (fn (tn, cnstrs) =>(map (test_equal_type tn) cnstrs; |
4008 | 94 |
snd(third(hd(cnstrs))))) (typs',cnstrss); |
95 |
val test_typs = ListPair.map (fn (typ,cnstrs) => |
|
96 |
if not (pcpo_type sg' typ) |
|
97 |
then error("Not a pcpo type: "^string_of_typ sg' typ) |
|
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
98 |
else map (fn (cn,_,(_,rt)) => rt=typ orelse error( |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
99 |
"Non-identical result types for constructors "^ |
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
100 |
first(hd cnstrs)^" and "^ cn )) cnstrs) |
4008 | 101 |
(typs,cnstrss); |
1274 | 102 |
val proper_args = let |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
103 |
fun occurs tn (Type(tn',ts)) = (tn'=tn) orelse exists (occurs tn) ts |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
104 |
| occurs _ _ = false; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
105 |
fun proper_arg cn atyp = forall (fn typ => let |
4030 | 106 |
val tn = fst (rep_Type typ) |
107 |
in atyp=typ orelse not (occurs tn atyp) orelse |
|
108 |
error("Illegal use of type "^ tn ^ |
|
109 |
" as argument of constructor " ^cn)end )typs; |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
110 |
fun proper_curry (cn,_,(args,_)) = forall (proper_arg cn) args; |
1274 | 111 |
in map (map proper_curry) cnstrss end; |
112 |
in (typs, flat cnstrss) end; |
|
113 |
||
4030 | 114 |
(* ----- calls for building new thy and thms -------------------------------- *) |
1274 | 115 |
|
116 |
in |
|
117 |
||
4008 | 118 |
fun add_domain (comp_dnam,eqs''') thy''' = let |
119 |
val sg''' = sign_of thy'''; |
|
120 |
val dtnvs = map ((fn (dname,vs) => |
|
121 |
(Sign.full_name sg''' dname,map (str2typ sg''') vs)) |
|
122 |
o fst) eqs'''; |
|
123 |
val cons''' = map snd eqs'''; |
|
124 |
fun thy_type (dname,tvars) = (Sign.base_name dname, length tvars, NoSyn); |
|
125 |
fun thy_arity (dname,tvars) = (dname, map (snd o rep_TFree) tvars, pcpoS); |
|
126 |
val thy'' = thy''' |> Theory.add_types (map thy_type dtnvs) |
|
127 |
|> Theory.add_arities_i (map thy_arity dtnvs); |
|
128 |
val sg'' = sign_of thy''; |
|
129 |
val cons''=map (map (upd_third (map (upd_third (str2typ sg''))))) cons'''; |
|
130 |
val eqs' = check_and_sort_domain (dtnvs,cons'') sg''; |
|
131 |
val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dnam,eqs'); |
|
1274 | 132 |
val dts = map (Type o fst) eqs'; |
133 |
fun cons cons' = (map (fn (con,syn,args) => |
|
4008 | 134 |
((ThyOps.const_name con syn), |
135 |
ListPair.map (fn ((lazy,sel,tp),vn) => ((lazy, |
|
4030 | 136 |
find (tp,dts) handle LIST "find" => ~1), |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
137 |
sel,vn)) |
4008 | 138 |
(args,(mk_var_names(map third args))) |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
139 |
)) cons') : cons list; |
1274 | 140 |
val eqs = map (fn (dtnvs,cons') => (dtnvs,cons cons')) eqs' : eq list; |
4030 | 141 |
val thy = thy' |> Domain_Axioms.add_axioms (comp_dnam,eqs); |
4043 | 142 |
in (foldl (fn (thy0,eq) => Domain_Theorems.theorems (eq,eqs) thy0) (thy,eqs)) |
143 |
|> Domain_Theorems.comp_theorems (comp_dnam, eqs) end; |
|
1274 | 144 |
|
145 |
fun add_gen_by ((tname,finite),(typs',cnstrss')) thy' = let |
|
4008 | 146 |
val (typs,cnstrs) = check_gen_by (sign_of thy') (typs',cnstrss'); |
1274 | 147 |
in |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
148 |
Domain_Axioms.add_induct ((tname,finite),(typs,cnstrs)) thy' end; |
1274 | 149 |
|
150 |
end (* local *) |
|
151 |
end (* struct *) |