vile / scheme / enumeration.v
319 lines · 285 sloc · 11.08 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// An immutable universe owns its index and canonical symbol container. Sets
4// from a declaration execution share this metadata but retain a fresh type.
5@[heap]
6struct EnumerationUniverse {
7 symbols []Value
8 indices map[string]int
9}
10
11@[heap]
12struct Enumeration {
13 universe &EnumerationUniverse
14mut:
15 full Value
16}
17
18// The usual small universe needs one box and no array. Large sets own just the
19// additional words; no mutable word view or symbol container escapes.
20@[heap]
21struct EnumerationSet {
22 enumeration &Enumeration
23 low u64
24 high []u64
25}
26
27fn (value Value) enumeration_pointer() &EnumerationSet {
28 return unsafe { &EnumerationSet(value.pointer()) }
29}
30
31fn checked_enumeration(value Value) !&EnumerationSet {
32 if value.tag != .enumeration { return failure('expected an enumeration set') }
33 return value.enumeration_pointer()
34}
35
36fn enumeration_words(universe &EnumerationUniverse) int {
37 return if universe.symbols.len == 0 { 1 } else { (universe.symbols.len - 1) / 64 + 1 }
38}
39
40fn enumeration_mask(universe &EnumerationUniverse, word int) u64 {
41 remaining := universe.symbols.len - word * 64
42 return if remaining >= 64 {
43 ~u64(0)
44 } else if remaining <= 0 {
45 u64(0)
46 } else {
47 (u64(1) << remaining) - 1
48 }
49}
50
51@[inline]
52fn enumeration_word(set &EnumerationSet, word int) u64 {
53 return if word == 0 { set.low } else { set.high[word - 1] }
54}
55
56@[inline]
57fn enumeration_contains(set &EnumerationSet, index int) bool {
58 return enumeration_word(set, index / 64) & (u64(1) << (index % 64)) != 0
59}
60
61fn new_enumeration(universe &EnumerationUniverse) Value {
62 mut enumeration := &Enumeration{ universe: universe }
63 mut high := []u64{len: enumeration_words(universe) - 1}
64 for i in 0 .. high.len { high[i] = enumeration_mask(universe, i + 1) }
65 value := payload_value(.enumeration, &EnumerationSet{ enumeration: enumeration, low: enumeration_mask(universe, 0), high: high })
66 enumeration.full = value
67 return value
68}
69
70fn enumeration_universe(symbols Value) !&EnumerationUniverse {
71 count := symbols.list_length()!
72 mut indices := map[string]int{}
73 mut cursor := symbols
74 for _ in 0 .. count {
75 pair := cursor.pair_pointer()
76 if pair.car.tag != .symbol { return failure('enumeration members must be symbols') }
77 name := pair.car.text()
78 if name !in indices {
79 indices[name] = indices.len
80 }
81 cursor = pair.cdr
82 }
83 // Symbols are already immutable, independently owned Values. Reuse their
84 // handles instead of cloning their names or retaining input list cells.
85 // A second pass allocates the exact unique length even with duplicates;
86 // ordinary map-key iteration would implicitly clone every escaping key.
87 mut canonical := []Value{cap: indices.len}
88 cursor = symbols
89 for _ in 0 .. count {
90 pair := cursor.pair_pointer()
91 index := indices[pair.car.text()] or { return failure('invalid enumeration index') }
92 if index == canonical.len { canonical << pair.car }
93 cursor = pair.cdr
94 }
95 return &EnumerationUniverse{ symbols: canonical, indices: indices }
96}
97
98const enumeration_native_descriptors = [
99 NativeDescriptor{'make-enumeration', enum_make}
100 NativeDescriptor{'enum-set-universe', enum_universe}
101 NativeDescriptor{'enum-set-indexer', enum_indexer}
102 NativeDescriptor{'enum-set-constructor', enum_constructor}
103 NativeDescriptor{'enum-set->list', enum_list}
104 NativeDescriptor{'enum-set-member?', enum_member}
105 NativeDescriptor{'enum-set-subset?', enum_subset}
106 NativeDescriptor{'enum-set=?', enum_equal}
107 NativeDescriptor{'enum-set-union', enum_union}
108 NativeDescriptor{'enum-set-intersection', enum_intersection}
109 NativeDescriptor{'enum-set-difference', enum_difference}
110 NativeDescriptor{'enum-set-complement', enum_complement}
111 NativeDescriptor{'enum-set-projection', enum_projection},
112]!
113
114fn (mut vm VM) install_enumeration_primitives() {
115 for index in 0 .. enumeration_native_descriptors.len {
116 entry := &enumeration_native_descriptors[index]
117 vm.bind_global(entry.name, procedure_value(&Procedure{ owner: vm.owner, mode: .native, name: entry.name, native: entry.callback }))
118 }
119}
120
121fn enum_make(mut _vm VM, args []Value) !Value {
122 check_arity('make-enumeration', args.len, 1, 1)!
123 return new_enumeration(enumeration_universe(args[0])!)
124}
125
126fn enum_universe(mut _vm VM, args []Value) !Value {
127 check_arity('enum-set-universe', args.len, 1, 1)!
128 return checked_enumeration(args[0])!.enumeration.full
129}
130
131fn enumeration_procedure(mut vm VM, args []Value, constructor bool) !Value {
132 name := if constructor { 'enum-set-constructor' } else { 'enum-set-indexer' }
133 check_arity(name, args.len, 1, 1)!
134 set := checked_enumeration(args[0])!
135 return procedure_value(&Procedure{
136 owner: vm.owner
137 name: name
138 mode: if constructor {
139 .enumeration_constructor
140 } else {
141 .enumeration_indexer
142 }
143 env: &Environment{ slots: [set.enumeration.full] }
144 })
145}
146
147fn enum_indexer(mut vm VM, args []Value) !Value {
148 return enumeration_procedure(mut vm, args, false)
149}
150
151fn enum_constructor(mut vm VM, args []Value) !Value {
152 return enumeration_procedure(mut vm, args, true)
153}
154
155fn enumeration_call(procedure &Procedure, args []Value) !Value {
156 check_arity(procedure.name, args.len, 1, 1)!
157 set := procedure.env.slots[0].enumeration_pointer()
158 if procedure.mode == .enumeration_indexer {
159 if args[0].tag != .symbol { return boolean(false) }
160 index := set.enumeration.universe.indices[args[0].text()] or { return boolean(false) }
161 return integer(index)
162 }
163 return enumeration_construct(set.enumeration, args[0])
164}
165
166fn enumeration_construct(enumeration &Enumeration, symbols Value) !Value {
167 count := symbols.list_length()!
168 mut low := u64(0)
169 mut high := []u64{len: enumeration_words(enumeration.universe) - 1}
170 mut cursor := symbols
171 for _ in 0 .. count {
172 pair := cursor.pair_pointer()
173 if pair.car.tag != .symbol { return failure('enumeration members must be symbols') }
174 index := enumeration.universe.indices[pair.car.text()] or { return failure('symbol is outside the enumeration universe') }
175 bit := u64(1) << (index % 64)
176 if index < 64 { low |= bit } else { high[index / 64 - 1] |= bit }
177 cursor = pair.cdr
178 }
179 if enumeration_is_full(enumeration.universe, low, high) { return enumeration.full }
180 return payload_value(.enumeration, &EnumerationSet{ enumeration: enumeration, low: low, high: high })
181}
182
183fn enumeration_is_full(universe &EnumerationUniverse, low u64, high []u64) bool {
184 if low != enumeration_mask(universe, 0) { return false }
185 for i, word in high { if word != enumeration_mask(universe, i + 1) { return false } }
186 return true
187}
188
189fn enum_list(mut _vm VM, args []Value) !Value {
190 check_arity('enum-set->list', args.len, 1, 1)!
191 set := checked_enumeration(args[0])!
192 mut result := nil_value()
193 for i := set.enumeration.universe.symbols.len - 1; i >= 0; i-- {
194 if enumeration_contains(set, i) {
195 result = cons(set.enumeration.universe.symbols[i], result)
196 }
197 }
198 return result
199}
200
201fn enum_member(mut _vm VM, args []Value) !Value {
202 check_arity('enum-set-member?', args.len, 2, 2)!
203 set := checked_enumeration(args[1])!
204 if args[0].tag != .symbol { return failure('enumeration member must be a symbol') }
205 index := set.enumeration.universe.indices[args[0].text()] or { return boolean(false) }
206 return boolean(enumeration_contains(set, index))
207}
208
209fn enumeration_subset(a &EnumerationSet, b &EnumerationSet) bool {
210 if voidptr(a.enumeration.universe) == voidptr(b.enumeration.universe) {
211 if a.low & ~b.low != 0 { return false }
212 for i, word in a.high { if word & ~b.high[i] != 0 { return false } }
213 return true
214 }
215 // Universe inclusion is required even for an empty member set. Different
216 // canonical orders are translated by the second universe's existing index.
217 for i, symbol in a.enumeration.universe.symbols {
218 index := b.enumeration.universe.indices[symbol.text()] or { return false }
219 if enumeration_contains(a, i) && !enumeration_contains(b, index) { return false }
220 }
221 return true
222}
223
224fn enum_subset(mut _vm VM, args []Value) !Value {
225 check_arity('enum-set-subset?', args.len, 2, 2)!
226 return boolean(enumeration_subset(checked_enumeration(args[0])!, checked_enumeration(args[1])!))
227}
228
229fn enum_equal(mut _vm VM, args []Value) !Value {
230 check_arity('enum-set=?', args.len, 2, 2)!
231 a := checked_enumeration(args[0])!
232 b := checked_enumeration(args[1])!
233 return boolean(enumeration_subset(a, b) && enumeration_subset(b, a))
234}
235
236enum EnumerationOperation {
237 union
238 intersection
239 difference
240 complement
241}
242
243@[inline]
244fn enumeration_operation(a u64, b u64, mask u64, operation EnumerationOperation) u64 {
245 return match operation {
246 .union { a | b }
247 .intersection { a & b }
248 .difference { a & ~b }
249 .complement { ~a & mask }
250 }
251}
252
253fn enumeration_combine(left Value, right Value, operation EnumerationOperation) !Value {
254 a := checked_enumeration(left)!
255 b := checked_enumeration(right)!
256 if voidptr(a.enumeration) != voidptr(b.enumeration) {
257 return failure('enumeration sets must have the same enumeration type')
258 }
259 universe := a.enumeration.universe
260 mut same_a := true
261 mut same_b := true
262 mut full := true
263 for i in 0 .. enumeration_words(universe) {
264 mask := enumeration_mask(universe, i)
265 word := enumeration_operation(enumeration_word(a, i), enumeration_word(b, i), mask, operation)
266 same_a = same_a && word == enumeration_word(a, i)
267 same_b = same_b && word == enumeration_word(b, i)
268 full = full && word == mask
269 }
270 // Immutable inputs/universes can be reused. Predicates, no-op set operations,
271 // and universe lookup need no result allocation or copy of a word array.
272 if same_a { return left }
273 if same_b { return right }
274 if full { return a.enumeration.full }
275 mut high := []u64{len: a.high.len}
276 for i in 0 .. high.len {
277 high[i] = enumeration_operation(a.high[i], b.high[i], enumeration_mask(universe, i + 1), operation)
278 }
279 low := enumeration_operation(a.low, b.low, enumeration_mask(universe, 0), operation)
280 return payload_value(.enumeration, &EnumerationSet{ enumeration: a.enumeration, low: low, high: high })
281}
282
283fn enum_union(mut _vm VM, args []Value) !Value {
284 check_arity('enum-set-union', args.len, 2, 2)!
285 return enumeration_combine(args[0], args[1], .union)
286}
287
288fn enum_intersection(mut _vm VM, args []Value) !Value {
289 check_arity('enum-set-intersection', args.len, 2, 2)!
290 return enumeration_combine(args[0], args[1], .intersection)
291}
292
293fn enum_difference(mut _vm VM, args []Value) !Value {
294 check_arity('enum-set-difference', args.len, 2, 2)!
295 return enumeration_combine(args[0], args[1], .difference)
296}
297
298fn enum_complement(mut _vm VM, args []Value) !Value {
299 check_arity('enum-set-complement', args.len, 1, 1)!
300 return enumeration_combine(args[0], args[0], .complement)
301}
302
303fn enum_projection(mut _vm VM, args []Value) !Value {
304 check_arity('enum-set-projection', args.len, 2, 2)!
305 a := checked_enumeration(args[0])!
306 b := checked_enumeration(args[1])!
307 if voidptr(a.enumeration) == voidptr(b.enumeration) { return args[0] }
308 mut low := u64(0)
309 mut high := []u64{len: b.high.len}
310 for i, symbol in a.enumeration.universe.symbols {
311 if !enumeration_contains(a, i) { continue }
312 if index := b.enumeration.universe.indices[symbol.text()] {
313 bit := u64(1) << (index % 64)
314 if index < 64 { low |= bit } else { high[index / 64 - 1] |= bit }
315 }
316 }
317 if enumeration_is_full(b.enumeration.universe, low, high) { return b.enumeration.full }
318 return payload_value(.enumeration, &EnumerationSet{ enumeration: b.enumeration, low: low, high: high })
319}
320