Merge branch 'master' into develop

This commit is contained in:
Simon Brooke 2025-03-14 10:27:30 +00:00
commit bef9be4914
9 changed files with 307 additions and 15 deletions

View file

@ -21,6 +21,8 @@ DEBUGFLAGS := -g3
all: $(TARGET) all: $(TARGET)
Debug: $(TARGET)
$(TARGET): $(OBJS) Makefile $(TARGET): $(OBJS) Makefile
$(CC) $(DEBUGFLAGS) $(LDFLAGS) $(OBJS) -o $@ $(LDFLAGS) $(LOADLIBES) $(LDLIBS) $(CC) $(DEBUGFLAGS) $(LDFLAGS) $(OBJS) -o $@ $(LDFLAGS) $(LOADLIBES) $(LDLIBS)
@ -34,7 +36,7 @@ else
indent $(INDENT_FLAGS) $(SRCS) $(HDRS) indent $(INDENT_FLAGS) $(SRCS) $(HDRS)
endif endif
test: $(OBJS) $(TESTS) Makefile test: $(TESTS) Makefile $(TARGET)
bash ./unit-tests.sh bash ./unit-tests.sh
.PHONY: clean .PHONY: clean

157
post-scarcity.cbp Normal file
View file

@ -0,0 +1,157 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="post-scarcity" />
<Option makefile_is_custom="1" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/post-scarcity" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/post-scarcity" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="Makefile" />
<Unit filename="src/arith/integer.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/arith/integer.h" />
<Unit filename="src/arith/peano.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/arith/peano.h" />
<Unit filename="src/arith/ratio.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/arith/ratio.h" />
<Unit filename="src/arith/real.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/arith/real.h" />
<Unit filename="src/authorise.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/authorise.h" />
<Unit filename="src/debug.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/debug.h" />
<Unit filename="src/init.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/io/fopen.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/io/fopen.h" />
<Unit filename="src/io/io.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/io/io.h" />
<Unit filename="src/io/print.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/io/print.h" />
<Unit filename="src/io/read.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/io/read.h" />
<Unit filename="src/memory/conspage.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/conspage.h" />
<Unit filename="src/memory/consspaceobject.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/consspaceobject.h" />
<Unit filename="src/memory/cursor.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/cursor.h" />
<Unit filename="src/memory/dump.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/dump.h" />
<Unit filename="src/memory/hashmap.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/hashmap.h" />
<Unit filename="src/memory/lookup3.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/lookup3.h" />
<Unit filename="src/memory/stack.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/stack.h" />
<Unit filename="src/memory/vectorspace.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/memory/vectorspace.h" />
<Unit filename="src/ops/equal.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/ops/equal.h" />
<Unit filename="src/ops/intern.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/ops/intern.h" />
<Unit filename="src/ops/lispops.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/ops/lispops.h" />
<Unit filename="src/ops/loop.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/ops/loop.h" />
<Unit filename="src/ops/meta.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/ops/meta.h" />
<Unit filename="src/repl.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/repl.h" />
<Unit filename="src/time/psse_time.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/time/psse_time.h" />
<Unit filename="src/utils.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/utils.h" />
<Unit filename="src/version.h" />
<Unit filename="utils_src/debugflags/debugflags.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="utils_src/readprintwc/readprintwc.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="utils_src/tagvalcalc/tagvalcalc.c">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

View file

@ -0,0 +1,58 @@
"/home/simon/workspace/post-scarcity/utils_src/readprintwc/readprintwc.c"
"/home/simon/workspace/post-scarcity/src/memory/vectorspace.c"
"/home/simon/workspace/post-scarcity/src/arith/peano.c"
"/home/simon/workspace/post-scarcity/src/init.c"
"/home/simon/workspace/post-scarcity/src/utils.h"
"/home/simon/workspace/post-scarcity/src/ops/intern.h"
"/home/simon/workspace/post-scarcity/src/arith/ratio.h"
"/home/simon/workspace/post-scarcity/src/io/io.c"
"/home/simon/workspace/post-scarcity/src/memory/conspage.h"
"/home/simon/workspace/post-scarcity/src/time/psse_time.h"
"/home/simon/workspace/post-scarcity/src/memory/cursor.h"
"/home/simon/workspace/post-scarcity/src/memory/dump.h"
"/home/simon/workspace/post-scarcity/src/ops/intern.c"
"/home/simon/workspace/post-scarcity/src/memory/lookup3.c"
"/home/simon/workspace/post-scarcity/src/io/fopen.h"
"/home/simon/workspace/post-scarcity/src/version.h"
"/home/simon/workspace/post-scarcity/src/memory/consspaceobject.h"
"/home/simon/workspace/post-scarcity/src/ops/meta.h"
"/home/simon/workspace/post-scarcity/src/arith/real.c"
"/home/simon/workspace/post-scarcity/src/ops/loop.c"
"/home/simon/workspace/post-scarcity/src/arith/integer.h"
"/home/simon/workspace/post-scarcity/src/time/psse_time.c"
"/home/simon/workspace/post-scarcity/src/memory/vectorspace.h"
"/home/simon/workspace/post-scarcity/src/memory/hashmap.c"
"/home/simon/workspace/post-scarcity/src/io/read.c"
"/home/simon/workspace/post-scarcity/src/ops/lispops.h"
"/home/simon/workspace/post-scarcity/src/ops/loop.h"
"/home/simon/workspace/post-scarcity/src/memory/stack.h"
"/home/simon/workspace/post-scarcity/utils_src/tagvalcalc/tagvalcalc.c"
"/home/simon/workspace/post-scarcity/src/debug.c"
"/home/simon/workspace/post-scarcity/src/io/read.h"
"/home/simon/workspace/post-scarcity/src/ops/meta.c"
"/home/simon/workspace/post-scarcity/src/memory/dump.c"
"/home/simon/workspace/post-scarcity/src/repl.c"
"/home/simon/workspace/post-scarcity/src/io/print.c"
"/home/simon/workspace/post-scarcity/src/memory/hashmap.h"
"/home/simon/workspace/post-scarcity/src/utils.c"
"/home/simon/workspace/post-scarcity/src/io/io.h"
"/home/simon/workspace/post-scarcity/src/memory/stack.c"
"/home/simon/workspace/post-scarcity/utils_src/debugflags/debugflags.c"
"/home/simon/workspace/post-scarcity/src/memory/consspaceobject.c"
"/home/simon/workspace/post-scarcity/src/memory/conspage.c"
"/home/simon/workspace/post-scarcity/src/memory/cursor.c"
"/home/simon/workspace/post-scarcity/src/arith/ratio.c"
"/home/simon/workspace/post-scarcity/Makefile"
"/home/simon/workspace/post-scarcity/src/arith/peano.h"
"/home/simon/workspace/post-scarcity/src/memory/lookup3.h"
"/home/simon/workspace/post-scarcity/src/arith/real.h"
"/home/simon/workspace/post-scarcity/src/ops/equal.c"
"/home/simon/workspace/post-scarcity/src/ops/lispops.c"
"/home/simon/workspace/post-scarcity/src/authorise.h"
"/home/simon/workspace/post-scarcity/src/io/print.h"
"/home/simon/workspace/post-scarcity/src/authorise.c"
"/home/simon/workspace/post-scarcity/src/debug.h"
"/home/simon/workspace/post-scarcity/src/arith/integer.c"
"/home/simon/workspace/post-scarcity/src/ops/equal.h"
"/home/simon/workspace/post-scarcity/src/repl.h"
"/home/simon/workspace/post-scarcity/src/io/fopen.c"

15
post-scarcity.layout Normal file
View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="Makefile" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="642" topLine="5" />
</Cursor>
</File>
<File name="src/arith/integer.c" open="1" top="1" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="3454" topLine="156" />
</Cursor>
</File>
</CodeBlocks_layout_file>

View file

@ -87,9 +87,10 @@ __int128_t cell_value( struct cons_pointer c, char op, bool is_first_cell ) {
/** /**
* Overwrite the value field of the integer indicated by `new` with * Overwrite the value field of the integer indicated by `new` with
* the least significant 60 bits of `val`, and return the more significant * the least significant INTEGER_BITS bits of `val`, and return the
* bits (if any) right-shifted by 60 places. Destructive, primitive, do not * more significant bits (if any) right-shifted by INTEGER_BITS places.
* use in any context except primitive operations on integers. * Destructive, primitive, do not use in any context except primitive
* operations on integers.
* *
* @param val the value to represent; * @param val the value to represent;
* @param less_significant the less significant words of this bignum, if any, * @param less_significant the less significant words of this bignum, if any,

View file

@ -22,7 +22,10 @@
* So left shifting and right shifting by 60 bits is correct. * So left shifting and right shifting by 60 bits is correct.
*/ */
#define MAX_INTEGER ((__int128_t)0x0fffffffffffffffL) #define MAX_INTEGER ((__int128_t)0x0fffffffffffffffL)
/**
* @brief Number of value bits in an integer cell
*
*/
#define INTEGER_BIT_SHIFT (60) #define INTEGER_BIT_SHIFT (60)
bool zerop( struct cons_pointer arg ); bool zerop( struct cons_pointer arg );

View file

@ -83,7 +83,7 @@ struct cons_pointer read_path( URL_FILE * input, wint_t initial,
prefix = c_string_to_lisp_symbol( L"oblist" ); prefix = c_string_to_lisp_symbol( L"oblist" );
break; break;
case '$': case '$':
case L'§': case LSESSION:
prefix = c_string_to_lisp_symbol( L"session" ); prefix = c_string_to_lisp_symbol( L"session" );
break; break;
} }
@ -245,7 +245,7 @@ struct cons_pointer read_continuation( struct stack_frame *frame,
} }
break; break;
case '$': case '$':
case L'§': case LSESSION:
result = read_path( input, c, NIL ); result = read_path( input, c, NIL );
break; break;
default: default:
@ -298,9 +298,9 @@ struct cons_pointer read_number( struct stack_frame *frame,
initial ); initial );
for ( c = initial; iswdigit( c ) for ( c = initial; iswdigit( c )
|| c == L'.' || c == L'/' || c == L','; c = url_fgetwc( input ) ) { || c == LPERIOD || c == LSLASH || c == LCOMMA; c = url_fgetwc( input ) ) {
switch ( c ) { switch ( c ) {
case L'.': case LPERIOD:
if ( seen_period || !nilp( dividend ) ) { if ( seen_period || !nilp( dividend ) ) {
return throw_exception( c_string_to_lisp_string return throw_exception( c_string_to_lisp_string
( L"Malformed number: too many periods" ), ( L"Malformed number: too many periods" ),
@ -311,7 +311,7 @@ struct cons_pointer read_number( struct stack_frame *frame,
seen_period = true; seen_period = true;
} }
break; break;
case L'/': case LSLASH:
if ( seen_period || !nilp( dividend ) ) { if ( seen_period || !nilp( dividend ) ) {
return throw_exception( c_string_to_lisp_string return throw_exception( c_string_to_lisp_string
( L"Malformed number: dividend of rational must be integer" ), ( L"Malformed number: dividend of rational must be integer" ),
@ -324,11 +324,12 @@ struct cons_pointer read_number( struct stack_frame *frame,
result = make_integer( 0, NIL ); result = make_integer( 0, NIL );
} }
break; break;
case L',': case LCOMMA:
// silently ignore it. // silently ignore it.
break; break;
default: default:
result = add_integers( multiply_integers( result, base ), result = add_integers( multiply_integers( result, base ),
/* /todo: this won't work for hex digits */
make_integer( ( int ) c - ( int ) '0', make_integer( ( int ) c - ( int ) '0',
NIL ) ); NIL ) );
@ -402,7 +403,7 @@ struct cons_pointer read_list( struct stack_frame *frame,
for ( c = url_fgetwc( input ); for ( c = url_fgetwc( input );
iswblank( c ) || iswcntrl( c ); c = url_fgetwc( input ) ); iswblank( c ) || iswcntrl( c ); c = url_fgetwc( input ) );
if ( c == L'.' ) { if ( c == LPERIOD ) {
/* might be a dotted pair; indeed, if we rule out numbers with /* might be a dotted pair; indeed, if we rule out numbers with
* initial periods, it must be a dotted pair. \todo Ought to check, * initial periods, it must be a dotted pair. \todo Ought to check,
* howerver, that there's only one form after the period. */ * howerver, that there's only one form after the period. */
@ -433,7 +434,7 @@ struct cons_pointer read_map( struct stack_frame *frame,
make_hashmap( DFLT_HASHMAP_BUCKETS, NIL, TRUE ); make_hashmap( DFLT_HASHMAP_BUCKETS, NIL, TRUE );
wint_t c = initial; wint_t c = initial;
while ( c != L'}' ) { while ( c != LCBRACE ) {
struct cons_pointer key = struct cons_pointer key =
read_continuation( frame, frame_pointer, env, input, c ); read_continuation( frame, frame_pointer, env, input, c );
@ -446,7 +447,7 @@ struct cons_pointer read_map( struct stack_frame *frame,
/* skip commaa and whitespace at this point. */ /* skip commaa and whitespace at this point. */
for ( c = url_fgetwc( input ); for ( c = url_fgetwc( input );
c == L',' || iswblank( c ) || iswcntrl( c ); c == LCOMMA || iswblank( c ) || iswcntrl( c );
c = url_fgetwc( input ) ); c = url_fgetwc( input ) );
result = result =

View file

@ -13,6 +13,15 @@
#include "memory/consspaceobject.h" #include "memory/consspaceobject.h"
/* characters (other than arabic numberals) used in number representations */
#define LCOMMA L','
#define LPERIOD L'.'
#define LSLASH L'/'
/* ... used in map representations */
#define LCBRACE L'}'
/* ... used in path representations */
#define LSESSION L'§'
/** /**
* read the next object on this input stream and return a cons_pointer to it. * read the next object on this input stream and return a cons_pointer to it.
*/ */

46
state-of-play.md Normal file
View file

@ -0,0 +1,46 @@
# State of Play
## 20250314
Thinking further about this, I think at least part of the problem is that I'm storing bignums as cons-space objects, which means that the integer representation I can store has to fit into the size of a cons pointer, which is 64 bits. Which means that to store integers larger than 64 bits I need chains of these objects.
If I stored bignums in vector space, this problem would go away (especially as I have not implemented vector space yet).
However, having bignums in vector space would cause a churn of non-standard-sized objects in vector space, which would mean much more frequent garbage collection, which has to be mark-and-sweep because unequal-sized objects, otherwise you get heap fragmentation.
So maybe I just have to put more work into debugging my cons-space bignums.
Bother, bother.
There are no perfect solutions.
However however, it's only the node that's short on vector space which has to pause to do a mark and sweep. It doesn't interrupt any other node, because their reference to the object will remain the same, even if it is the 'home node' of the object which is sweeping. So all the node has to do is set its busy flag, do GC, and clear its busy flag, The rest of the system can just be carrying on as normal.
So... maybe mark and sweep isn't the big deal I think it is?
## 20250313
OK, the 60 bit integer cell happens in `int128_to_integer` in `arith/integer.c`. It seems to be being done consistently; but there is no obvious reason. `MAX_INTEGER` is defined in `arith/peano.h`. I've changed both to use 63 bits, and this makes no change to the number of unit tests that fail.
With this change, `(fact 21)`, which was previously printing nothing, now prints a value, `11,891,611,015,076,642,816`. However, this value is definitively wrong, should be `51,090,942,171,709,440,000`. But, I hadn't fixed the shift in `integer_to_string`; have now... still no change in number of failed tests...
But `(fact 21)` gives a different wrong value, `4,974,081,987,435,560,960`. Factorial values returned by `fact` are correct (agree with SBCL running the same code) up to `(fact 20)`, with both 60 bit integer cells and 63 bit integer cells giving correct values.
Uhhhmmm... but I'd missed two other places where I'd had the number of significant bits as a numeric literal. Fixed those and now `(fact 21)` does not return a printable answer at all, although the internal representation is definitely wrong. So we may be seeing why I chose 60 bits.
Bother.
## 20250312
Printing of bignums definitely doesn't work; I'm not persuaded that reading of bignums works right either, and there are probably problems with bignum arithmetic too.
The internal memory representation of a number rolls over from one cell to two cells at 1152921504606846976, and I'm not at all certain why it does because this is neither 2<sup>63</sup> nor 2<sup>64</sup>.
| | | |
| -------------- | -------------------- | ---- |
| 2<sup>62</sup> | 4611686018427387904 | |
| 2<sup>63</sup> | 9223372036854775808 | |
| 2<sup>64</sup> | 18446744073709551616 | |
| Mystery number | 1152921504606846976 | |
In fact, our mystery number turns out (by inspection) to be 2<sup>60</sup>. But **why**?