1 (* Title: HOLCF/domain/extender.ML |
1 (* Title: HOLCF/domain/extender.ML |
2 ID: $Id$ |
2 ID: $Id$ |
3 Author: David von Oheimb |
3 Author: David von Oheimb |
|
4 License: GPL (GNU GENERAL PUBLIC LICENSE) |
4 |
5 |
5 Theory extender for domain section, including new-style theory syntax. |
6 Theory extender for domain section, including new-style theory syntax. |
|
7 |
|
8 ###TODO: |
|
9 |
|
10 this definition |
|
11 domain empty = silly empty |
|
12 yields |
|
13 Exception- |
|
14 TERM |
|
15 ("typ_of_term: bad encoding of type", |
|
16 [Abs ("uu", "_", Const ("None", "_"))]) raised |
|
17 but this works fine: |
|
18 domain Empty = silly Empty |
|
19 |
|
20 strange syntax errors are produced for: |
|
21 domain xx = xx ("x yy") |
|
22 domain 'a foo = foo (sel::"'a") |
|
23 and bar = bar ("'a dummy") |
|
24 |
6 *) |
25 *) |
7 |
26 |
8 signature DOMAIN_EXTENDER = |
27 signature DOMAIN_EXTENDER = |
9 sig |
28 sig |
10 val add_domain: string * |
29 val add_domain: string * |
17 |
36 |
18 open Domain_Library; |
37 open Domain_Library; |
19 |
38 |
20 (* ----- general testing and preprocessing of constructor list -------------- *) |
39 (* ----- general testing and preprocessing of constructor list -------------- *) |
21 |
40 |
22 fun check_and_sort_domain (dtnvs: (string * typ list) list, cons'' : |
41 fun check_and_sort_domain (dtnvs: (string * typ list) list, |
23 ((string * mixfix * (bool*string*typ) list) list) list) sg = |
42 cons'' : ((string * mixfix * (bool*string*typ) list) list) list) sg = |
24 let |
43 let |
25 val defaultS = Sign.defaultS sg; |
44 val defaultS = Sign.defaultS sg; |
26 val test_dupl_typs = (case duplicates (map fst dtnvs) of |
45 val test_dupl_typs = (case duplicates (map fst dtnvs) of |
27 [] => false | dups => error ("Duplicate types: " ^ commas_quote dups)); |
46 [] => false | dups => error ("Duplicate types: " ^ commas_quote dups)); |
28 val test_dupl_cons = (case duplicates (map first (flat cons'')) of |
47 val test_dupl_cons = (case duplicates (map first (flat cons'')) of |
45 TFree (distinct_name n,sort)) tvars; |
64 TFree (distinct_name n,sort)) tvars; |
46 fun rm_sorts (TFree(s,_)) = TFree(s,[]) |
65 fun rm_sorts (TFree(s,_)) = TFree(s,[]) |
47 | rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts) |
66 | rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts) |
48 | rm_sorts (TVar(s,_)) = TVar(s,[]) |
67 | rm_sorts (TVar(s,_)) = TVar(s,[]) |
49 and remove_sorts l = map rm_sorts l; |
68 and remove_sorts l = map rm_sorts l; |
50 fun analyse(TFree(v,s)) = (case assoc_string(tvars,v) of |
69 fun analyse indirect (TFree(v,s)) = (case assoc_string(tvars,v) of |
51 None => error ("Free type variable " ^ quote v ^ " on rhs.") |
70 None => error ("Free type variable " ^ quote v ^ " on rhs.") |
52 | Some sort => if eq_set_string (s,defaultS) orelse |
71 | Some sort => if eq_set_string (s,defaultS) orelse |
53 eq_set_string (s,sort ) |
72 eq_set_string (s,sort ) |
54 then TFree(distinct_name v,sort) |
73 then TFree(distinct_name v,sort) |
55 else error ("Inconsistent sort constraint" ^ |
74 else error ("Inconsistent sort constraint" ^ |
56 " for type variable " ^ quote v)) |
75 " for type variable " ^ quote v)) |
57 (** BUG OR FEATURE?: mutual recursion may use different arguments **) |
76 | analyse indirect (t as Type(s,typl)) = (case assoc_string(dtnvs,s)of |
58 | analyse(Type(s,typl)) = (case assoc_string((*dtnvs*) |
77 None => Type(s,map (analyse true) typl) |
59 [(dname,typevars)],s) of |
78 | Some typevars => if indirect |
60 None => Type(s,map analyse typl) |
79 then error ("Indirect recursion of type " ^ |
61 | Some typevars => if remove_sorts typevars = remove_sorts typl |
80 quote (string_of_typ sg t)) |
62 then Type(s,map analyse typl) |
81 else if dname <> s orelse (** BUG OR FEATURE?: |
63 else error ("Recursion of type " ^ quote s ^ |
82 mutual recursion may use different arguments **) |
|
83 remove_sorts typevars = remove_sorts typl |
|
84 then Type(s,map (analyse true) typl) |
|
85 else error ("Direct recursion of type " ^ |
|
86 quote (string_of_typ sg t) ^ |
64 " with different arguments")) |
87 " with different arguments")) |
65 | analyse(TVar _) = Imposs "extender:analyse"; |
88 | analyse indirect (TVar _) = Imposs "extender:analyse"; |
66 fun check_pcpo t = (pcpo_type sg t orelse error( |
89 fun check_pcpo t = (pcpo_type sg t orelse error( |
67 "Constructor argument type is not of sort pcpo: "^string_of_typ sg t); t); |
90 "Constructor argument type is not of sort pcpo: "^string_of_typ sg t); t); |
68 val analyse_con = upd_third (map (upd_third (check_pcpo o analyse))); |
91 val analyse_con = upd_third (map (upd_third (check_pcpo o analyse false))); |
69 in ((dname,distinct_typevars), map analyse_con cons') end; |
92 in ((dname,distinct_typevars), map analyse_con cons') end; |
70 in ListPair.map analyse_equation (dtnvs,cons'') |
93 in ListPair.map analyse_equation (dtnvs,cons'') |
71 end; (* let *) |
94 end; (* let *) |
72 |
95 |
73 (* ----- calls for building new thy and thms -------------------------------- *) |
96 (* ----- calls for building new thy and thms -------------------------------- *) |