jsonnet-microkanren

microKanren implementation in Jsonnet
git clone https://ccx.te2000.cz/git/jsonnet-microkanren
Log | Files | Refs

commit ef15df36ea3b11c3a01be57108d9bdc98018efa6
parent c019b4fe223ad8feb0f9078094b1d020c519d877
Author: Jan Pobříslo <ccx@te2000.cz>
Date:   Wed, 29 Nov 2023 08:48:50 +0000

WIP fixes

Diffstat:
MMakefile | 2+-
Mmicrokanren.libsonnet | 22+++++++++++++---------
Mmicrokanren_checks.libsonnet | 25+++++++++++++++++++++----
3 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,2 +1,2 @@ example: - jsonnet -J . example.jsonnet + jsonnet --max-trace 100 -J . example.jsonnet diff --git a/microkanren.libsonnet b/microkanren.libsonnet @@ -38,12 +38,17 @@ local immatureStream(func) = baseStream('immature') + { call: func, }; +local makeVariable(module, number) = { + ['µK:var']: '%d' % number, + eq(value):: module.eq(self, value), +}; + local makeGoal(callable) = { ['µK:goal']: callable, pursue(state):: assert uKc.trace('pursuing goal in state', state, true); assert uKc.State(state); - self['µK:goal'](state), + uKc.traceValue('goal returned', self['µK:goal'](state)), }; local mplus(stream1, stream2) = @@ -90,7 +95,7 @@ local bind(stream, goal) = substitution: { walk(var):: if $.type(var) == 'variable' then - local bound = std.get(self, var['µK:var']); + local bound = std.get(self, '%d' % var['µK:var']); if bound == null then var else @@ -127,7 +132,7 @@ local bind(stream, goal) = if prev_subst == null then null else - prev_subst.unify(w1[field], w2[field]), + prev_subst.unify(w1[field], w2[field]), std.objectFields(w1), self ) @@ -138,13 +143,12 @@ local bind(stream, goal) = else null, }, - freshVar():: { - variable: { - ['µK:var']: self.variableCount, - eq(value):: $.eq(self, value), + freshVar():: + local current = self; + { + variable: makeVariable($, current.variableCount), + newState: uKc.checkState(current + {variableCount: current.variableCount + 1}), }, - newState: self + {variableCount: self.variableCount + 1} - }, }, // streams diff --git a/microkanren_checks.libsonnet b/microkanren_checks.libsonnet @@ -7,7 +7,7 @@ local safeString(value) = else if t == 'object' then '{%s}' % [std.join(', ', std.map( function(f) '%s: %s' % [std.escapeStringJson(f.key), safeString(f.value)], - std.objectKeysValuesAll(value) + std.objectKeysValues(value) ))] else std.toString(value); @@ -18,6 +18,9 @@ local checkType(type) = function(value) std.assertEqual(std.type(value), type); trace(str, val, rest): std.trace("%s: %s" % [str, safeString(val)], rest), + traceValue(str, val): + std.trace("%s: %s" % [str, safeString(val)], val), + objectFields(obj, checks): assert std.assertEqual(std.type(obj), 'object'); assert std.assertEqual(std.objectFields(obj), std.objectFields(checks)); @@ -29,10 +32,13 @@ local checkType(type) = function(value) std.assertEqual(std.type(value), type); Variable(var): $.objectFields(var, { ['µK:var']: function(value) - assert std.assertEqual(std.type(value), 'number'); - assert value >= 0: "Incorrect variable number: %s" % [value]; + assert std.assertEqual(std.type(value), 'string'); + assert std.parseInt(value) >= 0: "Incorrect variable number: %s" % [value]; true, }), + checkVariable(var): + assert $.Variable(var); + var, VariableCount(vc): assert vc >= 0: "Incorrect state.variableCount: less than zero"; @@ -44,12 +50,18 @@ local checkType(type) = function(value) std.assertEqual(std.type(value), type); function(f) std.assertEqual(std.type(f.name), 'number'), std.objectKeysValues(subst) )), + checkSubstitution(subst): + assert $.Substitution(subst); + subst, State(state): $.objectFields(state, { variableCount: $.VariableCount, substitution: $.Substitution, }), + checkState(state): + assert $.State(state); + state, Stream(stream): assert std.assertEqual(std.type(stream), 'object'); @@ -69,10 +81,15 @@ local checkType(type) = function(value) std.assertEqual(std.type(value), type); }) else error "Incorrect stream type", + checkStream(stream): + assert $.Stream(stream); + stream, Goal(goal): $.objectFields(goal, { ['µK:goal']: checkType('function'), }), - + checkGoal(goal): + assert $.Goal(goal); + goal, } // vim: sts=2 ts=2 sw=2 et