Refactor vm to use MapReduce and Thunks

The new code is much simpler, and is able to handle more cases than
before, such as the `if` operation.
rust
Brian Picciano 2 years ago
parent 223b7f93a5
commit 3a2423a937
  1. 113
      vm/op.go
  2. 130
      vm/scope.go
  3. 10
      vm/vm.go

@ -2,70 +2,73 @@ package vm
import (
"github.com/mediocregopher/ginger/gg"
"github.com/mediocregopher/ginger/graph"
)
var (
inVal = nameVal("in")
outVal = nameVal("out")
)
// Operation is an entity which can accept a single argument (the OpenEdge),
// perform some internal processing on that argument, and return a resultant
// Value.
//
// The Scope passed into Perform can be used to Evaluate the OpenEdge, as
// needed.
type Operation interface {
Perform(*gg.OpenEdge, Scope) (Value, error)
// Thunk is returned from the performance of an Operation. When called it will
// return the result of that Operation having been called with the particular
// arguments which were passed in.
type Thunk func() (Value, error)
func valThunk(val Value) Thunk {
return func() (Value, error) { return val, nil }
}
func preEvalValOp(fn func(Value) (Value, error)) Operation {
// evalThunks is used to coalesce the results of multiple Thunks into a single
// Thunk which will return a tuple Value. As a special case, if only one Thunk
// is given then it is returned directly (1-tuple is equivalent to its only
// element).
func evalThunks(args []Thunk) Thunk {
return OperationFunc(func(edge *gg.OpenEdge, scope Scope) (Value, error) {
if len(args) == 1 {
return args[0]
}
return func() (Value, error) {
edgeVal, err := EvaluateEdge(edge, scope)
var (
err error
tupVals = make([]Value, len(args))
)
if err != nil {
return Value{}, err
for i := range args {
if tupVals[i], err = args[i](); err != nil {
return ZeroValue, err
}
}
return fn(edgeVal)
})
return Value{Tuple: tupVals}, nil
}
}
// NOTE this is a giant hack to get around the fact that we're not yet
// using a genericized Graph implementation, so when we do AddValueIn
// on a gg.Graph we can't use a Tuple value (because gg has no Tuple
// value), we have to use a Tuple vertex instead.
//
// This also doesn't yet support passing an operation as a value to another
// operation.
func preEvalEdgeOp(fn func(*gg.OpenEdge) (Value, error)) Operation {
return preEvalValOp(func(val Value) (Value, error) {
var edge *gg.OpenEdge
// Operation is an entity which can accept one or more arguments (each not
// having been evaluated yet) and return a Thunk which will perform some internal processing on those
// arguments and return a resultant Value.
type Operation interface {
Perform([]Thunk) (Thunk, error)
}
if len(val.Tuple) > 0 {
func preEvalValOp(fn func(Value) (Value, error)) Operation {
tupEdges := make([]*gg.OpenEdge, len(val.Tuple))
return OperationFunc(func(args []Thunk) (Thunk, error) {
for i := range val.Tuple {
tupEdges[i] = graph.ValueOut[gg.Value](gg.ZeroValue, val.Tuple[i].Value)
}
return func() (Value, error) {
edge = graph.TupleOut[gg.Value](gg.ZeroValue, tupEdges...)
val, err := evalThunks(args)()
} else {
if err != nil {
return ZeroValue, err
}
edge = graph.ValueOut[gg.Value](gg.ZeroValue, val.Value)
return fn(val)
}
}, nil
return fn(edge)
})
}
type graphOp struct {
@ -76,10 +79,9 @@ type graphOp struct {
// OperationFromGraph wraps the given Graph such that it can be used as an
// operation.
//
// When Perform is called the passed in OpenEdge is set to the "in" name value
// of the given Graph, then that resultant graph and the given parent Scope are
// used to construct a new Scope. The "out" name value is Evaluated on that
// Scope to obtain a resultant Value.
// The Thunk returned by Perform will evaluate the passed in Thunks, and set
// them to the "in" name value of the given Graph. The "out" name value is
// Evaluated using the given Scope to obtain a resultant Value.
func OperationFromGraph(g *gg.Graph, scope Scope) Operation {
return &graphOp{
Graph: g,
@ -87,25 +89,18 @@ func OperationFromGraph(g *gg.Graph, scope Scope) Operation {
}
}
func (g *graphOp) Perform(edge *gg.OpenEdge, scope Scope) (Value, error) {
return preEvalEdgeOp(func(edge *gg.OpenEdge) (Value, error) {
scope = ScopeFromGraph(
g.Graph.AddValueIn(inVal.Value, edge),
g.scope,
)
return scope.Evaluate(outVal)
}).Perform(edge, scope)
func (g *graphOp) Perform(args []Thunk) (Thunk, error) {
return ScopeFromGraph(
g.Graph,
evalThunks(args),
g.scope,
).Evaluate(outVal)
}
// OperationFunc is a function which implements the Operation interface.
type OperationFunc func(*gg.OpenEdge, Scope) (Value, error)
type OperationFunc func([]Thunk) (Thunk, error)
// Perform calls the underlying OperationFunc directly.
func (f OperationFunc) Perform(edge *gg.OpenEdge, scope Scope) (Value, error) {
return f(edge, scope)
func (f OperationFunc) Perform(args []Thunk) (Thunk, error) {
return f(args)
}

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/mediocregopher/ginger/gg"
"github.com/mediocregopher/ginger/graph"
)
// Scope encapsulates a set of names and the values they indicate, or the means
@ -11,86 +12,76 @@ import (
// its value.
type Scope interface {
// Evaluate accepts a name Value and returns the real Value which that name
// points to.
Evaluate(Value) (Value, error)
// Evaluate accepts a name Value and returns a Thunk which will return the
// real Value which that name points to.
Evaluate(Value) (Thunk, error)
// NewScope returns a new Scope which sub-operations within this Scope
// should use for themselves.
NewScope() Scope
}
// edgeToValue ignores the edgeValue, it only evaluates the edge's vertex as a
// Value.
func edgeToValue(edge *gg.OpenEdge, scope Scope) (Value, error) {
if ggVal, ok := edge.FromValue(); ok {
val := Value{Value: ggVal}
if val.Name != nil {
return scope.Evaluate(val)
}
return val, nil
}
var tupVal Value
tup, _ := edge.FromTuple()
// EvaluateEdge will use the given Scope to evaluate the edge's ultimate Value,
// after passing all leaf vertices up the tree through all Operations found on
// edge values.
func EvaluateEdge(edge *gg.OpenEdge, scope Scope) (Value, error) {
for _, tupEdge := range tup {
thunk, err := graph.MapReduce[gg.Value, gg.Value, Thunk](
edge,
func(ggVal gg.Value) (Thunk, error) {
val, err := EvaluateEdge(tupEdge, scope)
val := Value{Value: ggVal}
if err != nil {
return Value{}, err
}
if val.Name != nil {
return scope.Evaluate(val)
}
tupVal.Tuple = append(tupVal.Tuple, val)
}
return valThunk(val), nil
if len(tupVal.Tuple) == 1 {
return tupVal.Tuple[0], nil
}
},
func(ggEdgeVal gg.Value, args []Thunk) (Thunk, error) {
return tupVal, nil
}
if ggEdgeVal.IsZero() {
return evalThunks(args), nil
}
// EvaluateEdge will use the given Scope to evaluate the edge's ultimate Value,
// after passing all leaf vertices up the tree through all Operations found on
// edge values.
func EvaluateEdge(edge *gg.OpenEdge, scope Scope) (Value, error) {
edgeVal := Value{Value: ggEdgeVal}
edgeVal := Value{Value: edge.EdgeValue()}
if edgeVal.Name != nil {
if edgeVal.IsZero() {
return edgeToValue(edge, scope)
}
nameThunk, err := scope.Evaluate(edgeVal)
edge = edge.WithEdgeValue(gg.ZeroValue)
if err != nil {
return nil, err
if edgeVal.Name != nil {
} else if edgeVal, err = nameThunk(); err != nil {
return nil, err
}
}
var err error
if edgeVal.Graph != nil {
if edgeVal, err = scope.Evaluate(edgeVal); err != nil {
return Value{}, err
}
}
edgeVal = Value{
Operation: OperationFromGraph(
edgeVal.Graph,
scope.NewScope(),
),
}
}
if edgeVal.Graph != nil {
if edgeVal.Operation == nil {
return nil, fmt.Errorf("edge value must be an operation")
}
edgeVal = Value{
Operation: OperationFromGraph(edgeVal.Graph, scope.NewScope()),
}
}
return edgeVal.Operation.Perform(args)
},
)
if edgeVal.Operation == nil {
return Value{}, fmt.Errorf("edge value must be an operation")
if err != nil {
return ZeroValue, err
}
return edgeVal.Operation.Perform(edge, scope)
return thunk()
}
// ScopeMap implements the Scope interface.
@ -100,19 +91,19 @@ var _ Scope = ScopeMap{}
// Evaluate uses the given name Value as a key into the ScopeMap map, and
// returns the Value held there for the key, if any.
func (m ScopeMap) Evaluate(nameVal Value) (Value, error) {
func (m ScopeMap) Evaluate(nameVal Value) (Thunk, error) {
if nameVal.Name == nil {
return Value{}, fmt.Errorf("value %v is not a name value", nameVal)
return nil, fmt.Errorf("value %v is not a name value", nameVal)
}
val, ok := m[*nameVal.Name]
if !ok {
return Value{}, fmt.Errorf("%q not defined", *nameVal.Name)
return nil, fmt.Errorf("%q not defined", *nameVal.Name)
}
return val, nil
return valThunk(val), nil
}
// NewScope returns the ScopeMap as-is.
@ -122,6 +113,7 @@ func (m ScopeMap) NewScope() Scope {
type graphScope struct {
*gg.Graph
in Thunk
parent Scope
}
@ -132,23 +124,31 @@ type graphScope struct {
// be followed, with edge values being evaluated to Operations, until a Value
// can be obtained.
//
// As a special case, if the name "in" is evaluated, either directly or as part
// of an outer evaluation, then the given Thunk is used to evaluate the Value.
//
// If a name does not appear in the Graph, then the given parent Scope will be
// used to evaluate that name. If the parent Scope is nil then an error is
// returned.
//
// NewScope will return the parent scope, if one is given, or an empty ScopeMap
// if not.
func ScopeFromGraph(g *gg.Graph, parent Scope) Scope {
func ScopeFromGraph(g *gg.Graph, in Thunk, parent Scope) Scope {
return &graphScope{
Graph: g,
in: in,
parent: parent,
}
}
func (g *graphScope) Evaluate(nameVal Value) (Value, error) {
func (g *graphScope) Evaluate(nameVal Value) (Thunk, error) {
if nameVal.Name == nil {
return Value{}, fmt.Errorf("value %v is not a name value", nameVal)
return nil, fmt.Errorf("value %v is not a name value", nameVal)
}
if *nameVal.Name == "in" {
return g.in, nil
}
edgesIn := g.ValueIns(nameVal.Value)
@ -159,13 +159,13 @@ func (g *graphScope) Evaluate(nameVal Value) (Value, error) {
} else if l != 1 {
return Value{}, fmt.Errorf(
return nil, fmt.Errorf(
"%q must have exactly one input edge, found %d input edges",
*nameVal.Name, l,
)
}
return EvaluateEdge(edgesIn[0], g)
return func() (Value, error) { return EvaluateEdge(edgesIn[0], g) }, nil
}
func (g *graphScope) NewScope() Scope {

@ -115,5 +115,13 @@ func EvaluateSource(opSrc io.Reader, input gg.Value, scope Scope) (Value, error)
op := OperationFromGraph(g, scope.NewScope())
return op.Perform(graph.ValueOut[gg.Value](gg.ZeroValue, input), scope)
thunk, err := op.Perform([]Thunk{
func() (Value, error) { return Value{Value: input}, nil },
})
if err != nil {
return ZeroValue, err
}
return thunk()
}

Loading…
Cancel
Save