Skip to content

Instantly share code, notes, and snippets.

@okram
Last active January 28, 2020 13:37
Show Gist options
  • Save okram/fd438befe2968ba3daef26b23ae2ce36 to your computer and use it in GitHub Desktop.
Save okram/fd438befe2968ba3daef26b23ae2ce36 to your computer and use it in GitHub Desktop.
/**
This document defines a new Java object model for the mm-ADT VM.
The previous effort has Traverser functionality merged into Obj.
By separating out Traverser (the CPU) from the Obj (the RAM), there is a clean relationship between
how instructions executed on Traverser are mapped to instructions executed on Obj.
Ultimately, the Traverser's State&Model structures provide a basic internal algorithm for Traverser reasoning.
- local variable bindings
- symbol lookups
- rewrite rules
- model embeddings
- etc.
Future versions will make Traverser an Obj with State&Model encoded as [choose]-instructions.
**/
///////////////////////////////////////////////////////
//// mm-ADT VM Java Object Model ////
///////////////////////////////////////////////////////
class Traverser<O extends Obj> { // **Traverser**
O obj; // e.g. vertex::['name':'marko','age':29]
State state; // e.g. x->4, y->'hello' (variable bindings)
Model model; // e.g. pg->vertex | edge (category/schema and functors/embeddings)
}
// *** T is an impl (JVM-oriented) and <O> is the current obj type (mm-ADT).
// TObj<Obj>
// TBool<Bool>
// TInt<Int>
// TReal<Real>
// TStr<Str>
// TRec<Rec>
// TLst<Lst>
// TInst<Inst>
// *** each T-type has an mm-specific fluent instruction API (e.g. TInt.plus(), TInt.mult(), etc.)
class Obj<J extends Object> { // **Object**
String symbol; // e.g. vertex
J java; // e.g. Map.of(OStr.of('name'),OStr.of('marko'),OStr.of('age'),OInt.of(29))
Pair<WithOrderedRing> quantifier; // e.g. {?}
Inst ref; // e.g. <=[=rdb][get,'V'][...]
}
// *** O is an impl (JVM-oriented) and <J> is the wrapped Java object (JVM-oriented).
// OObj<Object>
// OBool<Boolean>
// OInt<Integer>
// OReal<Float>
// OStr<String>
// ORec<Map>
// OLst<List>
// OInst<List>
// *** each Obj-type has an mm-specific fluent instruction API (e.g. OInt.plus(), OInt.mult(), etc.)
// ********************************************************************** //
// Typically, TObj.xxx() => OObj.xxx() //
// However, via state/model instrospection, TObj.xxx() => OObj.yyy() //
// Such "rewiring" is guided by the traverser's internal insts/algorithm //
// -- ultimately, State and Model are [choose]-instructions //
// -- future versions will support mm-ADT programmable State and Model //
// -- Traversers will be mm-ADT objs w/ State/Model encoded in <= //
// ********************************************************************** //
class State { // **Bindings** (local to traverser)
Map<String,Obj> variables; // e.g. x->4, y->'hello'
}
class Model { // **Category+Functor** (global to swarm/can be localized for distribution)
String name; // e.g. pg, rdb, mongo, pg<=rdb
Map<String,Obj> objs; // e.g. person -> ['name':str,'age':int]
Map<String,Map<Inst,Inst>> arrows; // e.g. person -> ([drop,'name']->[error] | [put,'name',str]->[error])
}
// objs specify the structures of the model and (potentially) their mappings from another model via <=
// -- the embedding functor's object morphisms
// arrows specify the processes of the model and (potentially) their mappings to another model via ->
// -- the embedding functor's arrow morphisms
/**
range <= domain[obj_inst]
^[inst] -> [inst]
**/
///////////////////////////////////////////////////////
//// mm-ADT VM Java Object Fluency ////
///////////////////////////////////////////////////////
// ** example using 1-token ** //
TInt.of(1).plus(2).is(gt(5)).to('x') // TInt.of(1).obj.java == Java 1
1. 1
2. 3
3. 4{0}
4. 4{0}~x (doesn't store) ** evaluation via instances **
// ** example using int-token ** //
TInt.of().plus(2).is(gt(5)).to('x') // TInt.of().obj.java == Java null
1. int
2. int<=int[plus,2]
3. int{?}<=int[plus,2][is,bool<=int[gt,5]]
4. int{?}~x<=int[plus,2][is,bool<=int[gt,5]][to,x] ** compilation via types **
Given (4) above:
1 => int{?}~x<=int[plus,2][is,bool<=int[gt,5]][to,x]
// yields 4{0}~x (doesn't store)
///////////////////////////////////////////////////////
//// mmlang Syntax ////
///////////////////////////////////////////////////////
vertex::['id' :int~x,
'outE':edge{*} <=[=rdb][get,'E'][is,[get,'outV'][eq,x]]]~v // vertex edges in rdb E-table
<=[=rdb][get,'V'][is,[get,'id'][eq,x]][to,vrow] // rdb row as vertex
^[_[drop,'id'] -> [error] // can't drop id
|_[put,str~a,obj~b] -> [map,vrow][put,a,b] // add property, update row
|_[drop,'outE'] -> vertex <=[=rdb][get,'E']
[is,[get,'outV'][eq,x]]
[drop][map,v] // drops rows in E-table
|_[get,'outE'][is,[get,'label'][eq,str~c]]
[get,'inV'] -> vertex{*}<=[=rdb][get,'E']
[is,[and,[get,'outV'][eq,x],
[get,'label'][eq,c]]]
[map,['id':[get,'inV']]]] // vertex edges index by label
///////////////////////////////////////////////////////
//// mmlang Sugar Syntax ////
///////////////////////////////////////////////////////
x::y == y[as,x]
~x == [to,x]
=x == [map,x]
x=>y == x[as,y]
x<=y == y[as,x]
.x == [get,x]
_x == non-executing obj ('quoted')
x^y == x[attach,y]
[x|y] == [choose,x,y]
[x+y] == [branch,x,y]
x->y == [is,[a,x]][map,y]
x+y == x[plus,y]
x*y == x[mult,y]
x/y == x[div,y]
-x == x[neg]
x&y == [and,x,y]
x|y == [or,x,y]
x>y == x[gt,y]
x<y == x[lt,y]
x>=y == x[gte,y]
x=<y == x[lte,y]
x==y == x[eq,y]
// ** same example above but using sugar ** //
vertex::['id' :int~x,
'outE':edge{*} <=[=rdb].E[is.outV==x]]~v // vertex edges in rdb E-table
<=[=rdb].V[is.id==x]~vrow // rdb row as vertex
^[_[drop,'id'] -> [error] // can't drop id
|_[put,str~a,obj~b] -> [map,vrow][put,a,b] // add property, update row
|_[drop,'outE'] -> vertex <=[=rdb].E[is.outV==x][drop][map,v] // drops rows in E-table
|_.outE[is.label==str~c].inV -> vertex{*}<=[=rdb].E[is.outV==x&&.label==c]
[map,['id':.inV]]] // vertex edges index by label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment