author | oheimb |
Wed, 03 Apr 1996 19:27:14 +0200 | |
changeset 1637 | b8a8ae2e5de1 |
parent 1461 | 6bcb44e4d6e5 |
child 2445 | 51993fea433f |
permissions | -rw-r--r-- |
1274 | 1 |
(* extender.ML |
2 |
Author : David von Oheimb |
|
3 |
Created: 17-May-95 |
|
4 |
Updated: 31-May-95 extracted syntax.ML, theorems.ML |
|
5 |
Updated: 07-Jul-95 streamlined format of cons list |
|
6 |
Updated: 21-Jul-95 gen_by-section |
|
7 |
Updated: 28-Aug-95 simultaneous domain equations |
|
8 |
Copyright 1995 TU Muenchen |
|
9 |
*) |
|
10 |
||
11 |
||
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
12 |
structure Domain_Extender = |
1274 | 13 |
struct |
14 |
||
15 |
local |
|
16 |
||
17 |
open Domain_Library; |
|
18 |
||
19 |
(* ----- general testing and preprocessing of constructor list -------------------- *) |
|
20 |
||
21 |
fun check_domain(eqs':((string * typ list) * |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
22 |
(string * ThyOps.cmixfix * (bool*string*typ) list) list) list) = let |
1274 | 23 |
val dtnvs = map fst eqs'; |
24 |
val cons' = flat (map snd eqs'); |
|
25 |
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
|
26 |
[] => false | dups => error ("Duplicate types: " ^ commas_quote dups)); |
1274 | 27 |
val test_dupl_cons = (case duplicates (map first cons') of |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
28 |
[] => false | dups => error ("Duplicate constructors: " ^ commas_quote dups)); |
1274 | 29 |
val test_dupl_sels = (case duplicates(map second (flat(map third cons'))) of |
30 |
[] => false | dups => error ("Duplicate selectors: "^commas_quote dups)); |
|
31 |
val test_dupl_tvars = let fun vname (TFree(v,_)) = v |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
32 |
| vname _ = Imposs "extender:vname"; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
33 |
in exists (fn tvars => case duplicates (map vname tvars) of |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
34 |
[] => false | dups => error ("Duplicate type arguments: " ^commas_quote dups)) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
35 |
(map snd dtnvs) end; |
1274 | 36 |
(*test for free type variables and invalid use of recursive type*) |
37 |
val analyse_types = forall (fn ((_,typevars),cons') => |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
38 |
forall (fn con' => let |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
39 |
val types = map third (third con'); |
1274 | 40 |
fun analyse(t as TFree(v,_)) = t mem typevars orelse |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
41 |
error ("Free type variable " ^ v ^ " on rhs.") |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
42 |
| analyse(Type(s,typl)) = (case assoc (dtnvs,s) of None => analyses typl |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
43 |
| Some tvs => tvs = typl orelse |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
44 |
error ("Recursion of type " ^ s ^ " with different arguments")) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
45 |
| analyse(TVar _) = Imposs "extender:analyse" |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
46 |
and analyses ts = forall analyse ts; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
47 |
in analyses types end) cons' |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
48 |
) eqs'; |
1274 | 49 |
in true end; (* let *) |
50 |
||
51 |
fun check_gen_by thy' (typs': string list,cnstrss': string list list) = let |
|
52 |
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
|
53 |
| dups => error ("Duplicate types: " ^ commas_quote dups)); |
1274 | 54 |
val test_dupl_cnstrs = map (fn cnstrs' => (case duplicates cnstrs' of [] => false |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
55 |
| dups => error ("Duplicate constructors: " ^ commas_quote dups))) cnstrss'; |
1274 | 56 |
val tsig = #tsig(Sign.rep_sg(sign_of thy')); |
57 |
val tycons = map fst (#tycons(Type.rep_tsig tsig)); |
|
58 |
val test_types = forall(fn t=>t mem tycons orelse error("Unknown type: "^t))typs'; |
|
59 |
val cnstrss = let |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
60 |
fun type_of c = case (Sign.const_type(sign_of thy') c) of Some t => t |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
61 |
| None => error ("Unknown constructor: "^c); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
62 |
fun args_result_type (t as (Type(tn,[arg,rest]))) = |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
63 |
if tn = "->" orelse tn = "=>" |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
64 |
then let val (ts,r) = args_result_type rest in (arg::ts,r) end |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
65 |
else ([],t) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
66 |
| args_result_type t = ([],t); |
1274 | 67 |
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
|
68 |
(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
|
69 |
: (string * (* operator name of constr *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
70 |
string list * (* argument name list *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
71 |
(typ list * (* argument types *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
72 |
typ)) (* result type *) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
73 |
list list end; |
1274 | 74 |
fun test_equal_type tn (cn,_,(_,rt)) = fst (type_name_vars rt) = tn orelse |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
75 |
error("Inappropriate result type for constructor "^cn); |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
76 |
val typs = map (fn (tn, cnstrs) => (map (test_equal_type tn) cnstrs; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
77 |
snd(third(hd(cnstrs))))) (typs'~~cnstrss); |
1274 | 78 |
val test_typs = map (fn (typ,cnstrs) => |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
79 |
if not (Type.typ_instance(tsig,typ,TVar(("'a",0),["pcpo"]))) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
80 |
then error("Not a pcpo type: "^fst(type_name_vars typ)) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
81 |
else map (fn (cn,_,(_,rt)) => rt=typ |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
82 |
orelse error("Non-identical result types for constructors "^ |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
83 |
first(hd cnstrs)^" and "^ cn )) cnstrs) (typs~~cnstrss); |
1274 | 84 |
val proper_args = let |
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
85 |
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
|
86 |
| occurs _ _ = false; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
87 |
fun proper_arg cn atyp = forall (fn typ => let |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
88 |
val tn = fst(type_name_vars typ) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
89 |
in atyp=typ orelse not (occurs tn atyp) orelse |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
90 |
error("Illegal use of type "^ tn ^ |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
91 |
" as argument of constructor " ^cn)end )typs; |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
92 |
fun proper_curry (cn,_,(args,_)) = forall (proper_arg cn) args; |
1274 | 93 |
in map (map proper_curry) cnstrss end; |
94 |
in (typs, flat cnstrss) end; |
|
95 |
||
96 |
(* ----- calls for building new thy and thms -------------------------------------- *) |
|
97 |
||
98 |
in |
|
99 |
||
100 |
fun add_domain (comp_dname,eqs') thy'' = let |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
101 |
val ok = check_domain eqs'; |
1274 | 102 |
val thy' = thy'' |> Domain_Syntax.add_syntax (comp_dname,eqs'); |
103 |
val dts = map (Type o fst) eqs'; |
|
104 |
fun cons cons' = (map (fn (con,syn,args) => |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
105 |
(ThyOps.const_name con syn, |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
106 |
map (fn ((lazy,sel,tp),vn) => ((lazy, |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
107 |
find (tp,dts) handle LIST "find" => ~1), |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
108 |
sel,vn)) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
109 |
(args~~(mk_var_names(map third args))) |
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
110 |
)) cons') : cons list; |
1274 | 111 |
val eqs = map (fn (dtnvs,cons') => (dtnvs,cons cons')) eqs' : eq list; |
112 |
val thy = thy' |> Domain_Axioms.add_axioms (comp_dname,eqs); |
|
113 |
in (thy,eqs) end; |
|
114 |
||
115 |
fun add_gen_by ((tname,finite),(typs',cnstrss')) thy' = let |
|
116 |
val (typs,cnstrs) = check_gen_by thy' (typs',cnstrss'); |
|
117 |
in |
|
1637
b8a8ae2e5de1
Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents:
1461
diff
changeset
|
118 |
Domain_Axioms.add_induct ((tname,finite),(typs,cnstrs)) thy' end; |
1274 | 119 |
|
120 |
end (* local *) |
|
121 |
end (* struct *) |