1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::{fmt, iter, ops};
use crate::ty;
use crate::ty::Ty;
use arret_runtime::abitype;
use arret_runtime::boxed::{TypeTag, ALL_TYPE_TAGS};
const INNER_BITS: u8 = 32;
type Inner = u32;
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct TypeTagSet(Inner);
impl TypeTagSet {
pub fn new() -> TypeTagSet {
TypeTagSet(0)
}
pub fn all() -> TypeTagSet {
ALL_TYPE_TAGS.iter().collect()
}
pub fn is_empty(self) -> bool {
self.0 == 0
}
pub fn len(self) -> usize {
self.0.count_ones() as usize
}
pub fn insert(&mut self, type_tag: TypeTag) {
assert!((type_tag as u8) < INNER_BITS);
self.0 |= 1 << type_tag as u8;
}
pub fn is_subset(self, superset: Self) -> bool {
(self.0 & superset.0) == self.0
}
pub fn is_disjoint(self, other: Self) -> bool {
self.intersection(other).is_empty()
}
pub fn intersection(self, other: Self) -> TypeTagSet {
TypeTagSet(self.0 & other.0)
}
pub fn union(self, other: Self) -> TypeTagSet {
TypeTagSet(self.0 | other.0)
}
pub fn contains(self, type_tag: TypeTag) -> bool {
let type_tag_set: TypeTagSet = type_tag.into();
type_tag_set.is_subset(self)
}
pub fn into_iter(self) -> impl Iterator<Item = TypeTag> {
ALL_TYPE_TAGS
.iter()
.cloned()
.filter(move |type_tag| self.contains(*type_tag))
}
}
impl fmt::Debug for TypeTagSet {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
formatter.write_str("TypeTagSet(")?;
formatter.debug_list().entries(self.into_iter()).finish()?;
formatter.write_str(")")
}
}
impl From<TypeTag> for TypeTagSet {
fn from(type_tag: TypeTag) -> TypeTagSet {
let mut type_tag_set = TypeTagSet::new();
type_tag_set.insert(type_tag);
type_tag_set
}
}
impl<'a, M> From<&'a ty::Ref<M>> for TypeTagSet
where
M: ty::Pm,
{
fn from(ty_ref: &'a ty::Ref<M>) -> TypeTagSet {
match ty_ref.resolve_to_ty() {
Ty::Any => TypeTagSet::all(),
Ty::Int => TypeTag::Int.into(),
Ty::Float => TypeTag::Float.into(),
Ty::Char => TypeTag::Char.into(),
Ty::Bool => [TypeTag::True, TypeTag::False].iter().collect(),
Ty::Num => [TypeTag::Int, TypeTag::Float].iter().collect(),
Ty::LitBool(true) => TypeTag::True.into(),
Ty::LitBool(false) => TypeTag::False.into(),
Ty::Sym | Ty::LitSym(_) => TypeTag::Sym.into(),
Ty::Str => TypeTag::Str.into(),
Ty::Fun(_) | Ty::TopFun(_) | Ty::TyPred(_) | Ty::EqPred => TypeTag::FunThunk.into(),
Ty::Vector(_) | Ty::Vectorof(_) => TypeTag::Vector.into(),
Ty::Set(_) => TypeTag::Set.into(),
Ty::Map(_) => TypeTag::Map.into(),
Ty::TopRecord | Ty::RecordClass(_) | Ty::Record(_) => TypeTag::Record.into(),
Ty::List(list) => {
if list.is_empty() {
TypeTag::Nil.into()
} else if !list.fixed().is_empty() {
TypeTag::Pair.into()
} else {
[TypeTag::Nil, TypeTag::Pair].iter().collect()
}
}
Ty::Union(members) => members
.iter()
.map(TypeTagSet::from)
.fold(TypeTagSet::new(), |a, b| a | b),
Ty::Intersect(members) => members
.iter()
.map(TypeTagSet::from)
.fold(TypeTagSet::all(), |a, b| a & b),
}
}
}
impl<'a> From<&'a abitype::BoxedAbiType> for TypeTagSet {
fn from(boxed_abi_type: &'a abitype::BoxedAbiType) -> TypeTagSet {
use arret_runtime::abitype::BoxedAbiType;
match boxed_abi_type {
BoxedAbiType::Any => TypeTagSet::all(),
BoxedAbiType::UniqueTagged(type_tag) => (*type_tag).into(),
BoxedAbiType::List(_) => [TypeTag::Pair, TypeTag::Nil].iter().collect(),
BoxedAbiType::Pair(_) => TypeTag::Pair.into(),
BoxedAbiType::Vector(_) => TypeTag::Vector.into(),
BoxedAbiType::Set(_) => TypeTag::Set.into(),
BoxedAbiType::Map(_, _) => TypeTag::Map.into(),
BoxedAbiType::Union(_, type_tags) => type_tags.iter().collect(),
}
}
}
impl<'a> From<&'a abitype::AbiType> for TypeTagSet {
fn from(abi_type: &'a abitype::AbiType) -> TypeTagSet {
use arret_runtime::abitype::AbiType;
match abi_type {
AbiType::Int => TypeTag::Int.into(),
AbiType::Float => TypeTag::Float.into(),
AbiType::Char => TypeTag::Char.into(),
AbiType::Bool => [TypeTag::True, TypeTag::False].iter().collect(),
AbiType::InternedSym => TypeTag::Sym.into(),
AbiType::Boxed(boxed_abi_type) => boxed_abi_type.into(),
AbiType::Callback(_) => TypeTag::FunThunk.into(),
}
}
}
impl From<abitype::AbiType> for TypeTagSet {
fn from(abi_type: abitype::AbiType) -> TypeTagSet {
(&abi_type).into()
}
}
impl iter::FromIterator<TypeTag> for TypeTagSet {
fn from_iter<I: IntoIterator<Item = TypeTag>>(iter: I) -> TypeTagSet {
let mut type_tag_set = TypeTagSet::new();
for type_tag in iter {
type_tag_set.insert(type_tag);
}
type_tag_set
}
}
impl<'a> iter::FromIterator<&'a TypeTag> for TypeTagSet {
fn from_iter<I: IntoIterator<Item = &'a TypeTag>>(iter: I) -> TypeTagSet {
iter.into_iter().cloned().collect()
}
}
impl ops::BitOr for TypeTagSet {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
self.union(rhs)
}
}
impl ops::BitAnd for TypeTagSet {
type Output = Self;
fn bitand(self, rhs: Self) -> Self {
self.intersection(rhs)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::source::EMPTY_SPAN;
#[test]
fn basic_operations() {
let empty_set = TypeTagSet::new();
let list_set: TypeTagSet = [TypeTag::Nil, TypeTag::Pair].iter().cloned().collect();
let nil_sym_set: TypeTagSet = [TypeTag::Nil, TypeTag::Sym].iter().cloned().collect();
let pair_set: TypeTagSet = TypeTag::Pair.into();
let nil_set: TypeTagSet = TypeTag::Nil.into();
let full_set = TypeTagSet::all();
assert!(empty_set.is_empty());
assert!(!full_set.is_empty());
assert!(!nil_sym_set.is_empty());
assert!(!pair_set.is_empty());
assert!(!full_set.is_empty());
assert!(empty_set.is_subset(full_set));
assert!(empty_set.is_subset(nil_sym_set));
assert!(empty_set.is_subset(empty_set));
assert!(list_set.is_subset(full_set));
assert!(list_set.is_subset(list_set));
assert!(!list_set.is_subset(pair_set));
assert!(!list_set.is_subset(empty_set));
assert!(empty_set.is_disjoint(full_set));
assert!(nil_sym_set.is_disjoint(pair_set));
assert!(!nil_sym_set.is_disjoint(list_set));
assert_eq!(nil_set, list_set & nil_sym_set);
assert_eq!(list_set, pair_set | nil_set);
}
#[test]
fn set_into_iter() {
use std::collections::HashSet;
let empty_set = TypeTagSet::new();
let list_set: TypeTagSet = [TypeTag::Nil, TypeTag::Pair].iter().collect();
let nil_set: TypeTagSet = TypeTag::Nil.into();
let full_set = TypeTagSet::all();
assert_eq!(None, empty_set.into_iter().next());
let mut nil_set_iter = nil_set.into_iter();
assert_eq!(Some(TypeTag::Nil), nil_set_iter.next());
assert_eq!(None, nil_set_iter.next());
let list_hash_set: HashSet<TypeTag> = list_set.into_iter().collect();
assert_eq!(2, list_hash_set.len());
assert!(list_hash_set.contains(&TypeTag::Pair));
assert!(list_hash_set.contains(&TypeTag::Nil));
assert_eq!(ALL_TYPE_TAGS.len(), full_set.into_iter().count());
}
#[test]
fn from_ty_ref() {
let int_ty_ref: ty::Ref<ty::Poly> = Ty::Int.into();
assert_eq!(
TypeTagSet::from(TypeTag::Int),
TypeTagSet::from(&int_ty_ref)
);
let poly_sym_ref: ty::Ref<ty::Poly> =
ty::TVar::new(EMPTY_SPAN, "tvar1".into(), Ty::Sym.into()).into();
assert_eq!(
TypeTagSet::from(TypeTag::Sym),
TypeTagSet::from(&poly_sym_ref)
);
let num_float_intersect: ty::Ref<ty::Poly> =
Ty::Intersect(Box::new([Ty::Num.into(), Ty::Float.into()])).into();
assert_eq!(
TypeTagSet::from(TypeTag::Float),
TypeTagSet::from(&num_float_intersect)
);
}
}