smalltalk

#1
by h4 - opened
The introspector project org

https://huggingface.co/datasets/introspector/MicroLean4/blob/main/SimpleBool.rec_bfd55e472f250411041d0fc7d4b3b78db1b32fcdfa0a03eb9ff9178dbb3916df.json
write a smalltalk program to interpret expressions like this

"Define classes to represent the expression structure"

Object subclass: #LeanExpression
instanceVariableNames: 'kind'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

LeanExpression subclass: #ForallE
instanceVariableNames: 'forbndrTyp forbd binderName binderInfo'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

LeanExpression subclass: #Const
instanceVariableNames: 'levels declName'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

LeanExpression subclass: #App
instanceVariableNames: 'fn arg'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

LeanExpression subclass: #BVar
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

LeanExpression subclass: #Sort
instanceVariableNames: 'level'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

LeanExpression subclass: #Lam
instanceVariableNames: 'lambndrTp lambd binderName binderInfo'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

Object subclass: #LeanLevel
instanceVariableNames: 'level kind'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

Object subclass: #ConstantVal
instanceVariableNames: 'name levelParams kind type'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

Object subclass: #RecursorVal
instanceVariableNames: 'name levelParams kind numParams numMotives numMinors numIndices k isUnsafe all rules'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

Object subclass: #RecursorRule
instanceVariableNames: 'rhs nfields name kind'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

Object subclass: #AsyncConstB
instanceVariableNames: 'cnstInfB kindB'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

Object subclass: #ConstantInfoB
instanceVariableNames: 'name kind'
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

"Class methods for creation and parsing"

LeanExpression class
instanceVariableNames: ''.

LeanExpression class >> parse: aDictionary
| kind |
kind := aDictionary at: 'type' ifAbsent: [ aDictionary at: 'kind' ifAbsent: [ nil ] ].
kind = 'forallE' ifTrue: [ ^ ForallE parse: aDictionary ].
kind = 'const' ifTrue: [ ^ Const parse: aDictionary ].
kind = 'app' ifTrue: [ ^ App parse: aDictionary ].
kind = 'bvar' ifTrue: [ ^ BVar parse: aDictionary ].
kind = 'sort' ifTrue: [ ^ Sort parse: aDictionary ].
kind = 'lam' ifTrue: [ ^ Lam parse: aDictionary ].
(aDictionary includesKey: 'kind') ifTrue: [
(aDictionary at: 'kind') = 'AsyncConstB' ifTrue: [ ^ AsyncConstB parse: aDictionary ].
(aDictionary at: 'kind') = 'Lean.ConstantVal' ifTrue: [ ^ ConstantVal parse: aDictionary ].
(aDictionary at: 'kind') = 'Lean.RecursorVal' ifTrue: [ ^ RecursorVal parse: aDictionary ].
(aDictionary at: 'kind') = 'Lean.RecursorRule' ifTrue: [ ^ RecursorRule parse: aDictionary ].
(aDictionary at: 'kind') = 'Lean.Level' ifTrue: [ ^ LeanLevel parse: aDictionary ].
(aDictionary at: 'kind') = 'Lean.ConstantKind' ifTrue: [ ^ ConstantInfoB parse: aDictionary ].
].
^ nil.

ForallE >> initialize
super initialize.
kind := 'forallE'.

ForallE class >> parse: aDictionary
^ self new
forbndrTyp: (LeanExpression parse: (aDictionary at: 'forbndrTyp' ifAbsent: [ aDictionary at: 'forbndrTypB' ]));
forbd: (LeanExpression parse: (aDictionary at: 'forbd' ifAbsent: [ aDictionary at: 'forbdB' ]));
binderName: (aDictionary at: 'binderName');
binderInfo: (aDictionary at: 'binderInfo');
yourself.

Const >> initialize
super initialize.
kind := 'const'.

Const class >> parse: aDictionary
^ self new
levels: (aDictionary at: 'levels');
declName: (aDictionary at: 'declName');
yourself.

App >> initialize
super initialize.
kind := 'app'.

App class >> parse: aDictionary
^ self new
fn: (LeanExpression parse: (aDictionary at: 'fn'));
arg: (LeanExpression parse: (aDictionary at: 'arg'));
yourself.

BVar >> initialize
super initialize.
kind := 'bvar'.

BVar class >> parse: aDictionary
^ self new.

Sort >> initialize
super initialize.
kind := 'sort'.

Sort class >> parse: aDictionary
^ self new
level: (LeanLevel parse: (aDictionary at: 'level'));
yourself.

Lam >> initialize
super initialize.
kind := 'lam'.

Lam class >> parse: aDictionary
^ self new
lambndrTp: (LeanExpression parse: (aDictionary at: 'lambndrTp' ifAbsent: [ aDictionary at: 'lambndrTpB' ]));
lambd: (LeanExpression parse: (aDictionary at: 'lambd' ifAbsent: [ aDictionary at: 'lambdB' ]));
binderName: (aDictionary at: 'binderName');
binderInfo: (aDictionary at: 'binderInfo');
yourself.

LeanLevel class >> parse: aDictionary
^ self new
level: (aDictionary at: 'level');
kind: (aDictionary at: 'kind');
yourself.

ConstantVal class >> parse: aDictionary
^ self new
name: (aDictionary at: 'name');
levelParams: (aDictionary at: 'levelParams');
kind: (aDictionary at: 'kind');
type: (LeanExpression parse: (aDictionary at: 'type'));
yourself.

RecursorVal class >> parse: aDictionary
^ self new
name: (aDictionary at: 'name');
levelParams: (aDictionary at: 'levelParams');
kind: (aDictionary at: 'kind');
numParams: (aDictionary at: 'numParams');
numMotives: (aDictionary at: 'numMotives');
numMinors: (aDictionary at: 'numMinors');
numIndices: (aDictionary at: 'numIndices');
k: (aDictionary at: 'k');
isUnsafe: (aDictionary at: 'isUnsafe');
all: (aDictionary at: 'all');
rules: ((aDictionary at: 'Rules') collect: [ :rule | RecursorRule parse: rule ]);
yourself.

RecursorRule class >> parse: aDictionary
^ self new
rhs: (LeanExpression parse: (aDictionary at: 'rhs'));
nfields: (aDictionary at: 'nfields');
name: (aDictionary at: 'name');
kind: (aDictionary at: 'kind');
yourself.

AsyncConstB class >> parse: aDictionary
^ self new
cnstInfB: (ConstantVal parse: (aDictionary at: 'cnstInfB'));
kindB: (ConstantInfoB parse: (aDictionary at: 'kindB'));
yourself.

ConstantInfoB class >> parse: aDictionary
^ self new
name: (aDictionary at: 'name' ifAbsent: [ nil ]);
kind: (aDictionary at: 'kind');
yourself.

"Methods to print the structure"

LeanExpression >> printOn: aStream
aStream nextPutAll: self kind.

ForallE >> printOn: aStream
aStream nextPutAll: 'forall ('; nextPutAll: binderName; nextPutAll: ' : '.
forbndrTyp printOn: aStream.
aStream nextPutAll: '), '.
forbd printOn: aStream.

Const >> printOn: aStream
aStream nextPutAll: declName.
levels ifNotEmpty: [
aStream nextPutAll: '['.
levels do: [ :level | aStream nextPutAll: level ] separatedBy: [ aStream nextPutAll: ', ' ].
aStream nextPutAll: ']'.

App >> printOn: aStream
aStream nextPutAll: '('.
fn printOn: aStream.
aStream nextPutAll: ' '.
arg printOn: aStream.
aStream nextPutAll: ')'.

BVar >> printOn: aStream
aStream nextPutAll: ''.

Sort >> printOn: aStream
aStream nextPutAll: 'Sort['.
level printOn: aStream.
aStream nextPutAll: ']'.

LeanLevel >> printOn: aStream
aStream nextPutAll: level.

Lam >> printOn: aStream
aStream nextPutAll: 'fun ('; nextPutAll: binderName; nextPutAll: ' : '.
lambndrTp printOn: aStream.
aStream nextPutAll: '), '.
lambd printOn: aStream.

ConstantVal >> printOn: aStream
aStream nextPutAll: 'ConstantVal '; nextPutAll: name; nextPutAll: ' : '.
type printOn: aStream.

RecursorVal >> printOn: aStream
aStream nextPutAll: 'RecursorVal '; nextPutAll: name; nextPutAll: ' { '.
aStream nextPutAll: 'numMotives: '; nextPutAll: numMotives printString; nextPutAll: ', '.
aStream nextPutAll: 'numMinors: '; nextPutAll: numMinors printString; nextPutAll: ', '.
aStream nextPutAll: 'Rules: ['.
rules do: [ :rule | rule printOn: aStream ] separatedBy: [ aStream nextPutAll: ', ' ].
aStream nextPutAll: '] }'.

RecursorRule >> printOn: aStream
aStream nextPutAll: 'Rule for '; nextPutAll: name; nextPutAll: ' => '.
rhs printOn: aStream.

AsyncConstB >> printOn: aStream
aStream nextPutAll: 'AsyncConstB '.
cnstInfB printOn: aStream.

ConstantInfoB >> printOn: aStream
aStream nextPutAll: kind; nextPutAll: ' '.
name ifNotNil: [ aStream nextPutAll: name ].

"Main interpreter class"

Object subclass: #LeanInterpreter
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LeanInterpreter'.

LeanInterpreter class >> interpret: aJsonString
| dict expr |
dict := STON fromString: aJsonString. "Parse JSON using STON"
expr := LeanExpression parse: dict.
^ expr printString.

"Example usage"
| json expr |
json := '{"kind": "AsyncConstB", "cnstInfB": {"sig": {"type": {"type": "forallE", "forbndrTypB": {"type": "forallE", "forbndrTyp": {"type": "const", "levels": [], "declName": "SimpleBool"}, "forbdB": {"type": "sort", "level": {"level": "u", "kind": "Lean.Level"}}, "binderName": "t", "binderInfo": "default"}, "forbd": {"type": "forallE", "forbndrTyp": {"type": "app", "fn": {"type": "bvar"}, "arg": {"type": "const", "levels": [], "declName": "SimpleBool.false"}}, "forbdB": {"type": "forallE", "forbndrTyp": {"type": "app", "fn": {"type": "bvar"}, "arg": {"type": "const", "levels": [], "declName": "SimpleBool.true"}}, "forbdB": {"type": "forallE", "forbndrTyp": {"type": "const", "levels": [], "declName": "SimpleBool"}, "forbdB": {"type": "app", "fn": {"type": "bvar"}, "arg": {"type": "bvar"}}, "binderName": "t", "binderInfo": "default"}, "binderName": "true", "binderInfo": "default"}, "binderName": "false", "binderInfo": "default"}, "binderName": "motive", "binderInfo": "implicit"}, "name": "SimpleBool.rec", "levelParams": ["u"], "kind": "Lean.ConstantVal"}, "name": "SimpleBool.rec", "kindB": "AsyncConstInfoB", "kind": {"value": "recursor", "kind": "Lean.ConstantKind"}, "cnstInf": {"type": {"type": "forallE", "forbndrTypB": {"type": "forallE", "forbndrTyp": {"type": "const", "levels": [], "declName": "SimpleBool"}, "forbdB": {"type": "sort", "level": {"level": "u", "kind": "Lean.Level"}}, "binderName": "t", "binderInfo": "default"}, "forbd": {"type": "forallE", "forbndrTyp": {"type": "app", "fn": {"type": "bvar"}, "arg": {"type": "const", "levels": [], "declName": "SimpleBool.false"}}, "forbdB": {"type": "forallE", "forbndrTyp": {"type": "app", "fn": {"type": "bvar"}, "arg": {"type": "const", "levels": [], "declName": "SimpleBool.true"}}, "forbdB": {"type": "forallE", "forbndrTyp": {"type": "const", "levels": [], "declName": "SimpleBool"}, "forbdB": {"type": "app", "fn": {"type": "bvar"}, "arg": {"type": "bvar"}}, "binderName": "t", "binderInfo": "default"}, "binderName": "true", "binderInfo": "default"}, "binderName": "false", "binderInfo": "default"}, "binderName": "motive", "binderInfo": "implicit"}, "numParams": 0, "numMotives": 1, "numMinors": 2, "numIndices": 0, "name": "SimpleBool.rec", "levelParams": ["u"], "kind": "Lean.RecursorVal", "k": false, "isUnsafe": false, "all": ["SimpleBool"], "Rules": [{"rhs": {"type": "lam", "lambndrTpB": {"type": "forallE", "forbndrTyp": {"type": "const", "levels": [], "declName": "SimpleBool"}, "forbdB": {"type": "sort", "level": {"level": "u", "kind": "Lean.Level"}}, "binderName": "t", "binderInfo": "default"}, "lambd": {"type": "lam", "lambndrTp": {"type": "app", "fn": {"type": "bvar"}, "arg": {"type": "const", "levels": [], "declName": "SimpleBool.false"}}, "lambdB

Sign up or log in to comment