Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
intern.c
Go to the documentation of this file.
1/*
2 * intern.c
3 *
4 * For now this implements an oblist and shallow binding; local environments can
5 * be consed onto the front of the oblist. Later, this won't do; bindings will happen
6 * in namespaces, which will probably be implemented as hash tables.
7 *
8 * Doctrine is that cons cells are immutable, and life is a lot more simple if they are;
9 * so when a symbol is rebound in the master oblist, what in fact we do is construct
10 * a new oblist without the previous binding but with the new binding. Anything which,
11 * prior to this action, held a pointer to the old oblist (as all current threads'
12 * environments must do) continues to hold a pointer to the old oblist, and consequently
13 * doesn't see the change. This is probably good but does mean you cannot use bindings
14 * on the oblist to signal between threads.
15 *
16 * (c) 2017 Simon Brooke <simon@journeyman.cc>
17 * Licensed under GPL version 2.0, or, at your option, any later version.
18 */
19
20#include <stdbool.h>
21#include <string.h>
22/*
23 * wide characters
24 */
25#include <wchar.h>
26#include <wctype.h>
27
28#include "authorise.h"
29#include "debug.h"
30#include "io/io.h"
31#include "memory/conspage.h"
33#include "memory/hashmap.h"
34#include "ops/equal.h"
35#include "ops/intern.h"
36#include "ops/lispops.h"
37// #include "print.h"
38
39/**
40 * @brief The global object list/or, to put it differently, the root namespace.
41 * What is added to this during system setup is 'global', that is,
42 * visible to all sessions/threads. What is added during a session/thread is local to
43 * that session/thread (because shallow binding). There must be some way for a user to
44 * make the contents of their own environment persistent between threads but I don't
45 * know what it is yet. At some stage there must be a way to rebind deep values so
46 * they're visible to all users/threads, but again I don't yet have any idea how
47 * that will work.
48 */
50
51/**
52 * @brief the symbol `NIL`, which is special!
53 *
54 */
56
57/**
58 * Return a hash value for the structure indicated by `ptr` such that if
59 * `x`,`y` are two separate structures whose print representation is the same
60 * then `(sxhash x)` and `(sxhash y)` will always be equal.
61 */
62uint32_t sxhash( struct cons_pointer ptr ) {
63 // TODO: Not Yet Implemented
64 /* TODO: should look at the implementation of Common Lisp sxhash?
65 * My current implementation of `print` only addresses URL_FILE
66 * streams. It would be better if it also addressed strings but
67 * currently it doesn't. Creating a print string of the structure
68 * and taking the hash of that would be one simple (but not necessarily
69 * cheap) solution.
70 */
71 /* TODO: sbcl's implementation of `sxhash` is in src/compiler/sxhash.lisp
72 * and is EXTREMELY complex, and essentially has a different dispatch for
73 * every type of object. It's likely we need to do the same.
74 */
75 return 0;
76}
77
78/**
79 * Get the hash value for the cell indicated by this `ptr`; currently only
80 * implemented for string like things and integers.
81 */
82uint32_t get_hash( struct cons_pointer ptr ) {
83 struct cons_space_object *cell = &pointer2cell( ptr );
84 uint32_t result = 0;
85
86 switch ( cell->tag.value ) {
87 case INTEGERTV:
88 /* Note that we're only hashing on the least significant word of an
89 * integer. */
90 result = cell->payload.integer.value & 0xffffffff;
91 break;
92 case KEYTV:
93 case STRINGTV:
94 case SYMBOLTV:
95 result = cell->payload.string.hash;
96 break;
97 case TRUETV:
98 result = 1; // arbitrarily
99 break;
100 default:
101 result = sxhash( ptr );
102 break;
103 }
104
105 return result;
106}
107
108/**
109 * Free the hashmap indicated by this `pointer`.
110 */
111void free_hashmap( struct cons_pointer pointer ) {
112 struct cons_space_object *cell = &pointer2cell( pointer );
113
114 if ( hashmapp( pointer ) ) {
115 struct vector_space_object *vso = cell->payload.vectorp.address;
116
117 dec_ref( vso->payload.hashmap.hash_fn );
118 dec_ref( vso->payload.hashmap.write_acl );
119
120 for ( int i = 0; i < vso->payload.hashmap.n_buckets; i++ ) {
121 if ( !nilp( vso->payload.hashmap.buckets[i] ) ) {
123 L"Decrementing bucket [%d] of hashmap at 0x%lx\n",
124 i, cell->payload.vectorp.address );
125 dec_ref( vso->payload.hashmap.buckets[i] );
126 }
127 }
128 } else {
129 debug_printf( DEBUG_ALLOC, L"Non-hashmap passed to `free_hashmap`\n" );
130 }
131}
132
133
134/**
135 * Make a hashmap with this number of buckets, using this `hash_fn`. If
136 * `hash_fn` is `NIL`, use the standard hash funtion.
137 */
138struct cons_pointer make_hashmap( uint32_t n_buckets,
139 struct cons_pointer hash_fn,
140 struct cons_pointer write_acl ) {
141 struct cons_pointer result = make_vso( HASHTV,
142 ( sizeof( struct cons_pointer ) *
143 ( n_buckets + 2 ) ) +
144 ( sizeof( uint32_t ) * 2 ) );
145
146 struct hashmap_payload *payload =
147 ( struct hashmap_payload * ) &pointer_to_vso( result )->payload;
148
149 payload->hash_fn = inc_ref( hash_fn );
150 payload->write_acl = inc_ref( write_acl );
151
152 payload->n_buckets = n_buckets;
153 for ( int i = 0; i < n_buckets; i++ ) {
154 payload->buckets[i] = NIL;
155 }
156
157 return result;
158}
159
160/**
161 * return a flat list of all the keys in the hashmap indicated by `map`.
162 */
164 struct cons_pointer result = NIL;
165 if ( hashmapp( mapp ) && truep( authorised( mapp, NIL ) ) ) {
166 struct vector_space_object *map = pointer_to_vso( mapp );
167
168 for ( int i = 0; i < map->payload.hashmap.n_buckets; i++ ) {
169 for ( struct cons_pointer c = map->payload.hashmap.buckets[i];
170 !nilp( c ); c = c_cdr( c ) ) {
171 result = make_cons( c_car( c_car( c ) ), result );
172 }
173 }
174 }
175
176 return result;
177}
178
179/**
180 * Copy all key/value pairs in this association list `assoc` into this hashmap `mapp`. If
181 * current user is authorised to write to this hashmap, modifies the hashmap and
182 * returns it; if not, clones the hashmap, modifies the clone, and returns that.
183 */
185 struct cons_pointer assoc ) {
186 // TODO: if current user has write access to this hashmap
187 if ( hashmapp( mapp ) ) {
188 struct vector_space_object *map = pointer_to_vso( mapp );
189
190 if ( consp( assoc ) ) {
191 for ( struct cons_pointer pair = c_car( assoc ); !nilp( pair );
192 pair = c_car( assoc ) ) {
193 /* TODO: this is really hammering the memory management system, because
194 * it will make a new lone for every key/value pair added. Fix. */
195 if ( consp( pair ) ) {
196 mapp = hashmap_put( mapp, c_car( pair ), c_cdr( pair ) );
197 } else if ( hashmapp( pair ) ) {
198 hashmap_put_all( mapp, pair );
199 } else {
200 hashmap_put( mapp, pair, TRUE );
201 }
202 assoc = c_cdr( assoc );
203 }
204 } else if ( hashmapp( assoc ) ) {
205 for ( struct cons_pointer keys = hashmap_keys( assoc );
206 !nilp( keys ); keys = c_cdr( keys ) ) {
207 struct cons_pointer key = c_car( keys );
208 hashmap_put( mapp, key, hashmap_get( assoc, key ) );
209 }
210 }
211 }
212
213 return mapp;
214}
215
216/** Get a value from a hashmap.
217 *
218 * Note that this is here, rather than in memory/hashmap.c, because it is
219 * closely tied in with c_assoc, q.v.
220 */
222 struct cons_pointer key ) {
223 struct cons_pointer result = NIL;
224 if ( hashmapp( mapp ) && truep( authorised( mapp, NIL ) ) && !nilp( key ) ) {
225 struct vector_space_object *map = pointer_to_vso( mapp );
226 uint32_t bucket_no = get_hash( key ) % map->payload.hashmap.n_buckets;
227
228 result = c_assoc( key, map->payload.hashmap.buckets[bucket_no] );
229 }
230
231 return result;
232}
233
234/**
235 * If this `ptr` is a pointer to a hashmap, return a new identical hashmap;
236 * else return an exception.
237 */
239 struct cons_pointer result = NIL;
240
241 if ( truep( authorised( ptr, NIL ) ) ) {
242 if ( hashmapp( ptr ) ) {
243 struct vector_space_object const *from = pointer_to_vso( ptr );
244
245 if ( from != NULL ) {
246 struct hashmap_payload from_pl = from->payload.hashmap;
247 result =
248 make_hashmap( from_pl.n_buckets, from_pl.hash_fn,
249 from_pl.write_acl );
250 struct vector_space_object const *to =
251 pointer_to_vso( result );
252 struct hashmap_payload to_pl = to->payload.hashmap;
253
254 for ( int i = 0; i < to_pl.n_buckets; i++ ) {
255 to_pl.buckets[i] = from_pl.buckets[i];
256 inc_ref( to_pl.buckets[i] );
257 }
258 }
259 }
260 } else {
261 result =
263 ( L"Arg to `clone_hashmap` must "
264 L"be a readable hashmap.`" ), NIL );
265 }
266
267 return result;
268}
269
270// (keys set let quote read equal *out* *log* oblist cons source cond close meta mapcar negative? open subtract eval nλ *in* *sink* cdr set! reverse slurp try assoc eq add list time car t *prompt* absolute append apply divide exception get-hash hashmap inspect metadata multiply print put! put-all! read-char repl throw type + * - / = lambda λ nlambda progn)
271
272/**
273 * Implementation of interned? in C. The final implementation if interned? will
274 * deal with stores which can be association lists or hashtables or hybrids of
275 * the two, but that will almost certainly be implemented in lisp.
276 *
277 * If this key is lexically identical to a key in this store, return the key
278 * from the store (so that later when we want to retrieve a value, an eq test
279 * will work); otherwise return NIL.
280 */
281struct cons_pointer
282internedp( struct cons_pointer key, struct cons_pointer store ) {
283 struct cons_pointer result = NIL;
284
285 if ( symbolp( key ) || keywordp( key ) ) {
286 // TODO: I see what I was doing here and it would be the right thing to
287 // do for stores which are old-fashioned assoc lists, but it will not work
288 // for my new hybrid stores.
289 // for ( struct cons_pointer next = store;
290 // nilp( result ) && consp( next );
291 // next = pointer2cell( next ).payload.cons.cdr ) {
292 // struct cons_space_object entry =
293 // pointer2cell( pointer2cell( next ).payload.cons.car );
294
295 // debug_print( L"Internedp: checking whether `", DEBUG_BIND );
296 // debug_print_object( key, DEBUG_BIND );
297 // debug_print( L"` equals `", DEBUG_BIND );
298 // debug_print_object( entry.payload.cons.car, DEBUG_BIND );
299 // debug_print( L"`\n", DEBUG_BIND );
300
301 // if ( equal( key, entry.payload.cons.car ) ) {
302 // result = entry.payload.cons.car;
303 // }
304 if ( !nilp( c_assoc( key, store ) ) ) {
305 result = key;
306 } else if ( equal( key, privileged_symbol_nil ) ) {
307 result = privileged_symbol_nil;
308 }
309 } else {
310 debug_print( L"`", DEBUG_BIND );
312 debug_print( L"` is a ", DEBUG_BIND );
313 debug_printf( DEBUG_BIND, L"%4.4s",
314 ( char * ) pointer2cell( key ).tag.bytes );
315 debug_print( L", not a KEYW or SYMB", DEBUG_BIND );
316 }
317
318 return result;
319}
320
321/**
322 * Implementation of assoc in C. Like interned?, the final implementation will
323 * deal with stores which can be association lists or hashtables or hybrids of
324 * the two, but that will almost certainly be implemented in lisp.
325 *
326 * If this key is lexically identical to a key in this store, return the value
327 * of that key from the store; otherwise return NIL.
328 */
330 struct cons_pointer store ) {
331 struct cons_pointer result = NIL;
332
333 if ( !nilp( key ) ) {
334 if ( consp( store ) ) {
335 for ( struct cons_pointer next = store;
336 nilp( result ) && ( consp( next ) || hashmapp( next ) );
337 next = pointer2cell( next ).payload.cons.cdr ) {
338 if ( consp( next ) ) {
339// #ifdef DEBUG
340// debug_print( L"\nc_assoc; key is `", DEBUG_BIND );
341// debug_print_object( key, DEBUG_BIND );
342// debug_print( L"`\n", DEBUG_BIND );
343// #endif
344
345 struct cons_pointer entry_ptr = c_car( next );
346 struct cons_space_object entry = pointer2cell( entry_ptr );
347
348 switch ( entry.tag.value ) {
349 case CONSTV:
350 if ( equal( key, entry.payload.cons.car ) ) {
351 result = entry.payload.cons.cdr;
352 }
353 break;
354 case VECTORPOINTTV:
355 result = hashmap_get( entry_ptr, key );
356 break;
357 default:
360 ( L"Store entry is of unknown type: " ),
361 c_type( entry_ptr ) ), NIL );
362 }
363
364// #ifdef DEBUG
365// debug_print( L"c_assoc `", DEBUG_BIND );
366// debug_print_object( key, DEBUG_BIND );
367// debug_print( L"` returning: ", DEBUG_BIND );
368// debug_print_object( result, DEBUG_BIND );
369// debug_println( DEBUG_BIND );
370// #endif
371 }
372 }
373 } else if ( hashmapp( store ) ) {
374 result = hashmap_get( store, key );
375 } else if ( !nilp( store ) ) {
376// #ifdef DEBUG
377// debug_print( L"c_assoc; store is of unknown type `", DEBUG_BIND );
378// debug_printf( DEBUG_BIND, L"%4.4s", (char *)pointer2cell(key).tag.bytes);
379// debug_print( L"`\n", DEBUG_BIND );
380// #endif
381 result =
384 ( L"Store is of unknown type: " ),
385 c_type( store ) ), NIL );
386 }
387 }
388
389 return result;
390}
391
392/**
393 * Store this `val` as the value of this `key` in this hashmap `mapp`. If
394 * current user is authorised to write to this hashmap, modifies the hashmap and
395 * returns it; if not, clones the hashmap, modifies the clone, and returns that.
396 */
398 struct cons_pointer key,
399 struct cons_pointer val ) {
400 if ( hashmapp( mapp ) && !nilp( key ) ) {
401 struct vector_space_object *map = pointer_to_vso( mapp );
402
403 if ( nilp( authorised( mapp, map->payload.hashmap.write_acl ) ) ) {
404 mapp = clone_hashmap( mapp );
405 map = pointer_to_vso( mapp );
406 }
407 uint32_t bucket_no = get_hash( key ) % map->payload.hashmap.n_buckets;
408
409 // TODO: if there are too many values in the bucket, rehash the whole
410 // hashmap to a bigger number of buckets, and return that.
411
412 map->payload.hashmap.buckets[bucket_no] =
413 make_cons( make_cons( key, val ),
414 map->payload.hashmap.buckets[bucket_no] );
415 }
416
417 return mapp;
418}
419
420 /**
421 * Return a new key/value store containing all the key/value pairs in this
422 * store with this key/value pair added to the front.
423 */
424struct cons_pointer set( struct cons_pointer key, struct cons_pointer value,
425 struct cons_pointer store ) {
426 struct cons_pointer result = NIL;
427
428#ifdef DEBUG
429 bool deep = vectorpointp( store );
430 debug_print_binding( key, value, deep, DEBUG_BIND );
431
432 if ( deep ) {
433 debug_printf( DEBUG_BIND, L"\t-> %4.4s\n",
434 pointer2cell( store ).payload.vectorp.tag.bytes );
435 }
436#endif
437 if ( nilp( value ) ) {
438 result = store;
439 } else if ( nilp( store ) || consp( store ) ) {
440 result = make_cons( make_cons( key, value ), store );
441 } else if ( hashmapp( store ) ) {
442 result = hashmap_put( store, key, value );
443 }
444
445 return result;
446}
447
448/**
449 * @brief Binds this `key` to this `value` in the global oblist, and returns the `key`.
450 */
451struct cons_pointer
452deep_bind( struct cons_pointer key, struct cons_pointer value ) {
453 debug_print( L"Entering deep_bind\n", DEBUG_BIND );
454
455 struct cons_pointer old = oblist;
456
457 oblist = set( key, value, oblist );
458
459 // The oblist is not now an assoc list, and I don't think it will be again.
460 // if ( consp( oblist ) ) {
461 // inc_ref( oblist );
462 // dec_ref( old );
463 // }
464
465 debug_print( L"deep_bind returning ", DEBUG_BIND );
468
469 return key;
470}
471
472/**
473 * Ensure that a canonical copy of this key is bound in this environment, and
474 * return that canonical copy. If there is currently no such binding, create one
475 * with the value TRUE.
476 */
477struct cons_pointer
478intern( struct cons_pointer key, struct cons_pointer environment ) {
479 struct cons_pointer result = environment;
480 struct cons_pointer canonical = internedp( key, environment );
481 if ( nilp( canonical ) ) {
482 /*
483 * not currently bound
484 */
485 result = set( key, TRUE, environment );
486 }
487
488 return result;
489}
struct cons_pointer authorised(struct cons_pointer target, struct cons_pointer acl)
TODO: does nothing, yet.
Definition authorise.c:18
struct cons_pointer dec_ref(struct cons_pointer pointer)
Decrement the reference count of the object at this cons pointer.
#define KEYTV
The string KEYW, considered as an unsigned int.
#define VECTORPOINTTV
The string VECP, considered as an unsigned int.
#define truep(conspoint)
true if conspoint points to something that is truthy, i.e.
#define SYMBOLTV
The string SYMB, considered as an unsigned int.
union cons_space_object::@2 tag
union cons_space_object::@3 payload
#define NIL
a cons pointer which points to the special NIL cell
struct cons_pointer make_exception(struct cons_pointer message, struct cons_pointer frame_pointer)
Construct an exception cell.
struct cons_pointer c_cdr(struct cons_pointer arg)
Implementation of cdr in C.
#define STRINGTV
The string STRG, considered as an unsigned int.
#define INTEGERTV
The string INTR, considered as an unsigned int.
#define consp(conspoint)
true if conspoint points to a cons cell, else false
#define CONSTV
The string CONS, considered as an unsigned int.
#define nilp(conspoint)
true if conspoint points to the special cell NIL, else false (there should only be one of these so it...
#define TRUETV
The string TRUE, considered as an unsigned int.
#define symbolp(conspoint)
true if conspoint points to a symbol cell, else false
struct cons_pointer c_string_to_lisp_string(wchar_t *string)
Return a lisp string representation of this wide character string.
struct cons_pointer inc_ref(struct cons_pointer pointer)
increment the reference count of the object at this cons pointer.
#define TRUE
a cons pointer which points to the special T cell
#define keywordp(conspoint)
true if conspoint points to a keyword, else false
struct cons_pointer c_car(struct cons_pointer arg)
Implementation of car in C.
struct cons_pointer c_type(struct cons_pointer pointer)
Get the Lisp type of the single argument.
#define vectorpointp(conspoint)
true if conspoint points to a vector pointer, else false.
#define pointer2cell(pointer)
given a cons_pointer as argument, return the cell.
struct cons_pointer make_cons(struct cons_pointer car, struct cons_pointer cdr)
Construct a cons cell from this pair of pointers.
An indirect pointer to a cons cell.
an object in cons space.
void debug_print_binding(struct cons_pointer key, struct cons_pointer val, bool deep, int level)
Standardise printing of binding trace messages.
Definition debug.c:150
void debug_println(int level)
print a line feed to stderr, if verbosity matches level.
Definition debug.c:85
void debug_printf(int level, wchar_t *format,...)
wprintf adapted for the debug logging system.
Definition debug.c:101
void debug_print(wchar_t *message, int level)
print this debug message to stderr, if verbosity matches level.
Definition debug.c:41
void debug_print_object(struct cons_pointer pointer, int level)
print the object indicated by this pointer to stderr, if verbosity matches level.
Definition debug.c:119
#define DEBUG_BIND
Print messages debugging symbol binding.
Definition debug.h:38
#define DEBUG_ALLOC
Print messages debugging memory allocation.
Definition debug.h:24
bool equal(struct cons_pointer a, struct cons_pointer b)
Deep, and thus expensive, equality: true if these two objects have identical structure,...
Definition equal.c:332
struct cons_pointer hashmap_get(struct cons_pointer mapp, struct cons_pointer key)
Get a value from a hashmap.
Definition intern.c:221
struct cons_pointer hashmap_put(struct cons_pointer mapp, struct cons_pointer key, struct cons_pointer val)
Store this val as the value of this key in this hashmap mapp.
Definition intern.c:397
struct cons_pointer privileged_symbol_nil
the symbol NIL, which is special!
Definition intern.c:55
void free_hashmap(struct cons_pointer pointer)
Free the hashmap indicated by this pointer.
Definition intern.c:111
struct cons_pointer hashmap_put_all(struct cons_pointer mapp, struct cons_pointer assoc)
Copy all key/value pairs in this association list assoc into this hashmap mapp.
Definition intern.c:184
struct cons_pointer make_hashmap(uint32_t n_buckets, struct cons_pointer hash_fn, struct cons_pointer write_acl)
Make a hashmap with this number of buckets, using this hash_fn.
Definition intern.c:138
struct cons_pointer hashmap_keys(struct cons_pointer mapp)
return a flat list of all the keys in the hashmap indicated by map.
Definition intern.c:163
uint32_t get_hash(struct cons_pointer ptr)
Get the hash value for the cell indicated by this ptr; currently only implemented for string like thi...
Definition intern.c:82
struct cons_pointer internedp(struct cons_pointer key, struct cons_pointer store)
Implementation of interned? in C.
Definition intern.c:282
uint32_t sxhash(struct cons_pointer ptr)
Return a hash value for the structure indicated by ptr such that if x,y are two separate structures w...
Definition intern.c:62
struct cons_pointer c_assoc(struct cons_pointer key, struct cons_pointer store)
Implementation of assoc in C.
Definition intern.c:329
struct cons_pointer intern(struct cons_pointer key, struct cons_pointer environment)
Ensure that a canonical copy of this key is bound in this environment, and return that canonical copy...
Definition intern.c:478
struct cons_pointer oblist
The global object list/or, to put it differently, the root namespace.
Definition intern.c:49
struct cons_pointer clone_hashmap(struct cons_pointer ptr)
If this ptr is a pointer to a hashmap, return a new identical hashmap; else return an exception.
Definition intern.c:238
struct cons_pointer deep_bind(struct cons_pointer key, struct cons_pointer value)
Binds this key to this value in the global oblist, and returns the key.
Definition intern.c:452
struct cons_pointer set(struct cons_pointer key, struct cons_pointer value, struct cons_pointer store)
Return a new key/value store containing all the key/value pairs in this store with this key/value pai...
Definition intern.c:424
struct cons_pointer throw_exception(struct cons_pointer message, struct cons_pointer frame_pointer)
Throw an exception.
Definition lispops.c:1249
struct cons_pointer c_append(struct cons_pointer l1, struct cons_pointer l2)
A version of append which can conveniently be called from C.
Definition lispops.c:1452
struct cons_pointer make_vso(uint32_t tag, uint64_t payload_size)
Allocate a vector space object with this payload_size and tag, and return a cons_pointer which points...
Definition vectorspace.c:78
struct cons_pointer hash_fn
Definition vectorspace.h:89
uint32_t n_buckets
Definition vectorspace.h:95
struct cons_pointer write_acl
Definition vectorspace.h:91
union vector_space_object::@5 payload
we'll malloc size bytes for payload, payload is just the first of these.
#define pointer_to_vso(pointer)
given a pointer to a vector space object, return the object.
Definition vectorspace.h:55
#define HASHTV
Definition vectorspace.h:30
#define hashmapp(conspoint)
Definition vectorspace.h:32
struct cons_pointer buckets[]
Definition vectorspace.h:97
The payload of a hashmap.
Definition vectorspace.h:88
a vector_space_object is just a vector_space_header followed by a lump of bytes; what we deem to be i...