...
1
2
3
4
5
6
7 package typeparams
8
9 import "go/types"
10
11
12
13
14
15
16
17 type term struct {
18 tilde bool
19 typ types.Type
20 }
21
22 func (x *term) String() string {
23 switch {
24 case x == nil:
25 return "β
"
26 case x.typ == nil:
27 return "π€"
28 case x.tilde:
29 return "~" + x.typ.String()
30 default:
31 return x.typ.String()
32 }
33 }
34
35
36 func (x *term) equal(y *term) bool {
37
38 switch {
39 case x == nil || y == nil:
40 return x == y
41 case x.typ == nil || y.typ == nil:
42 return x.typ == y.typ
43 }
44
45
46 return x.tilde == y.tilde && types.Identical(x.typ, y.typ)
47 }
48
49
50 func (x *term) union(y *term) (_, _ *term) {
51
52 switch {
53 case x == nil && y == nil:
54 return nil, nil
55 case x == nil:
56 return y, nil
57 case y == nil:
58 return x, nil
59 case x.typ == nil:
60 return x, nil
61 case y.typ == nil:
62 return y, nil
63 }
64
65
66 if x.disjoint(y) {
67 return x, y
68 }
69
70
71
72
73
74
75 if x.tilde || !y.tilde {
76 return x, nil
77 }
78 return y, nil
79 }
80
81
82 func (x *term) intersect(y *term) *term {
83
84 switch {
85 case x == nil || y == nil:
86 return nil
87 case x.typ == nil:
88 return y
89 case y.typ == nil:
90 return x
91 }
92
93
94 if x.disjoint(y) {
95 return nil
96 }
97
98
99
100
101
102
103 if !x.tilde || y.tilde {
104 return x
105 }
106 return y
107 }
108
109
110 func (x *term) includes(t types.Type) bool {
111
112 switch {
113 case x == nil:
114 return false
115 case x.typ == nil:
116 return true
117 }
118
119
120 u := t
121 if x.tilde {
122 u = under(u)
123 }
124 return types.Identical(x.typ, u)
125 }
126
127
128 func (x *term) subsetOf(y *term) bool {
129
130 switch {
131 case x == nil:
132 return true
133 case y == nil:
134 return false
135 case y.typ == nil:
136 return true
137 case x.typ == nil:
138 return false
139 }
140
141
142 if x.disjoint(y) {
143 return false
144 }
145
146
147
148
149
150
151 return !x.tilde || y.tilde
152 }
153
154
155
156 func (x *term) disjoint(y *term) bool {
157 if debug && (x.typ == nil || y.typ == nil) {
158 panic("invalid argument(s)")
159 }
160 ux := x.typ
161 if y.tilde {
162 ux = under(ux)
163 }
164 uy := y.typ
165 if x.tilde {
166 uy = under(uy)
167 }
168 return !types.Identical(ux, uy)
169 }
170
View as plain text