39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* NAME/ARITY PAIRS *)
|
39349
|
3 |
(* Copyright (c) 2004-2006 Joe Hurd, distributed under the BSD License *)
|
39348
|
4 |
(* ========================================================================= *)
|
|
5 |
|
|
6 |
structure NameArity :> NameArity =
|
|
7 |
struct
|
|
8 |
|
|
9 |
(* ------------------------------------------------------------------------- *)
|
|
10 |
(* A type of name/arity pairs. *)
|
|
11 |
(* ------------------------------------------------------------------------- *)
|
|
12 |
|
|
13 |
type nameArity = Name.name * int;
|
|
14 |
|
|
15 |
fun name ((n,_) : nameArity) = n;
|
|
16 |
|
|
17 |
fun arity ((_,i) : nameArity) = i;
|
|
18 |
|
|
19 |
(* ------------------------------------------------------------------------- *)
|
|
20 |
(* Testing for different arities. *)
|
|
21 |
(* ------------------------------------------------------------------------- *)
|
|
22 |
|
|
23 |
fun nary i n_i = arity n_i = i;
|
|
24 |
|
|
25 |
val nullary = nary 0
|
|
26 |
and unary = nary 1
|
|
27 |
and binary = nary 2
|
|
28 |
and ternary = nary 3;
|
|
29 |
|
|
30 |
(* ------------------------------------------------------------------------- *)
|
|
31 |
(* A total ordering. *)
|
|
32 |
(* ------------------------------------------------------------------------- *)
|
|
33 |
|
|
34 |
fun compare ((n1,i1),(n2,i2)) =
|
|
35 |
case Name.compare (n1,n2) of
|
|
36 |
LESS => LESS
|
|
37 |
| EQUAL => Int.compare (i1,i2)
|
|
38 |
| GREATER => GREATER;
|
|
39 |
|
|
40 |
fun equal (n1,i1) (n2,i2) = i1 = i2 andalso Name.equal n1 n2;
|
|
41 |
|
|
42 |
(* ------------------------------------------------------------------------- *)
|
|
43 |
(* Parsing and pretty printing. *)
|
|
44 |
(* ------------------------------------------------------------------------- *)
|
|
45 |
|
|
46 |
fun pp (n,i) =
|
|
47 |
Print.blockProgram Print.Inconsistent 0
|
|
48 |
[Name.pp n,
|
|
49 |
Print.addString "/",
|
|
50 |
Print.ppInt i];
|
|
51 |
|
|
52 |
end
|
|
53 |
|
|
54 |
structure NameArityOrdered =
|
|
55 |
struct type t = NameArity.nameArity val compare = NameArity.compare end
|
|
56 |
|
|
57 |
structure NameArityMap =
|
|
58 |
struct
|
|
59 |
|
|
60 |
local
|
|
61 |
structure S = KeyMap (NameArityOrdered);
|
|
62 |
in
|
|
63 |
open S;
|
|
64 |
end;
|
|
65 |
|
|
66 |
fun compose m1 m2 =
|
|
67 |
let
|
|
68 |
fun pk ((_,a),n) = peek m2 (n,a)
|
|
69 |
in
|
|
70 |
mapPartial pk m1
|
|
71 |
end;
|
|
72 |
|
|
73 |
end
|
|
74 |
|
|
75 |
structure NameAritySet =
|
|
76 |
struct
|
|
77 |
|
|
78 |
local
|
|
79 |
structure S = ElementSet (NameArityMap);
|
|
80 |
in
|
|
81 |
open S;
|
|
82 |
end;
|
|
83 |
|
|
84 |
val allNullary = all NameArity.nullary;
|
|
85 |
|
|
86 |
val pp =
|
|
87 |
Print.ppMap
|
|
88 |
toList
|
|
89 |
(Print.ppBracket "{" "}" (Print.ppOpList "," NameArity.pp));
|
|
90 |
|
|
91 |
end
|