Skip to content

Commit

Permalink
update to latest ichiban/prolog
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Sep 25, 2022
1 parent 9d9d426 commit b8dfcd8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 57 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/guregu/pengine

go 1.18

require github.com/ichiban/prolog v0.10.5
require github.com/ichiban/prolog v0.11.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/ichiban/prolog v0.10.4 h1:GL/+f0Abd7llsVnNaFkuIdQPJ+oAlHA2Z82y/5tQfCA
github.com/ichiban/prolog v0.10.4/go.mod h1:qB0m6pWsd/XghbWmS5D6GExVhEvNqsa+iBPFMpTxS+U=
github.com/ichiban/prolog v0.10.5 h1:6UpnWeSq1u4NNoxYD61z9qhTw6LR1M3OlxB51ONbnEw=
github.com/ichiban/prolog v0.10.5/go.mod h1:qB0m6pWsd/XghbWmS5D6GExVhEvNqsa+iBPFMpTxS+U=
github.com/ichiban/prolog v0.11.1 h1:8lC/qGzUB/M8LM8F+KxMpIC0fUBJQBaKgOFcxNsqvgw=
github.com/ichiban/prolog v0.11.1/go.mod h1:qB0m6pWsd/XghbWmS5D6GExVhEvNqsa+iBPFMpTxS+U=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down
2 changes: 1 addition & 1 deletion pengines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPengines(t *testing.T) {
engine.Integer(1),
engine.Float(2.1),
engine.Atom("あ"),
&engine.Compound{Functor: "b", Args: []engine.Term{engine.Atom("c")}},
engine.Atom("b").Apply(engine.Atom("c")),
engine.List(engine.Atom("d")),
}

Expand Down
28 changes: 14 additions & 14 deletions predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func rpc(p *prolog.Interpreter) func(url, query, options engine.Term, k func(*en

query = env.Simplify(query)
var q strings.Builder
if err := query.WriteTerm(&q, &engine.WriteOptions{
if err := engine.WriteTerm(&q, query, &engine.WriteOptions{
Quoted: true,
IgnoreOps: true,
}, env); err != nil {
Expand All @@ -57,36 +57,36 @@ func rpc(p *prolog.Interpreter) func(url, query, options engine.Term, k func(*en
for iter.Next() {
cur := env.Resolve(iter.Current())
switch x := cur.(type) {
case *engine.Compound:
switch x.Functor {
case engine.Compound:
switch x.Functor() {
case "application":
str := term2str(x.Args[0], env)
str := term2str(x.Arg(0), env)
if str == "" {
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Args[0], env))
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Arg(0), env))
}
client.Application = str
case "chunk":
n, ok := env.Resolve(x.Args[0]).(engine.Integer)
n, ok := env.Resolve(x.Arg(0)).(engine.Integer)
if !ok {
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Args[0], env))
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Arg(0), env))
}
client.Chunk = int(n)
case "src_text":
str := term2str(x.Args[0], env)
str := term2str(x.Arg(0), env)
if str == "" {
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Args[0], env))
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Arg(0), env))
}
client.SourceText = str
case "src_url":
str := term2str(x.Args[0], env)
str := term2str(x.Arg(0), env)
if str == "" {
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Args[0], env))
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Arg(0), env))
}
client.SourceURL = str
case "debug":
str := term2str(x.Args[0], env)
str := term2str(x.Arg(0), env)
if str == "" {
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Args[0], env))
return engine.Error(engine.TypeError(engine.ValidTypeAtom, x.Arg(0), env))
}
client.Debug = str == "true"
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func doRPC(as *prologAnswers, query engine.Term, k func(*engine.Env) *engine.Pro

func stringify(t engine.Term) string {
var q strings.Builder
_ = t.WriteTerm(&q, &engine.WriteOptions{
_ = engine.WriteTerm(&q, t, &engine.WriteOptions{
Quoted: true,
IgnoreOps: false,
}, nil)
Expand Down
47 changes: 15 additions & 32 deletions prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ func (p *prologAnswers) handle(ctx context.Context, a string) error {
return fmt.Errorf("pengines: failed to parse response: %w", err)
}

event, ok := t.(*engine.Compound)
event, ok := t.(engine.Compound)
if !ok {
return fmt.Errorf("unexpected event type: %T (value: %v)", t, t)
}
return p.handleEvent(event)
}

func (p *prologAnswers) handleEvent(t *engine.Compound) error {
func (p *prologAnswers) handleEvent(t engine.Compound) error {
/*
% Original script looked like:
Expand Down Expand Up @@ -138,25 +138,25 @@ func (p *prologAnswers) handleEvent(t *engine.Compound) error {
'$pengine_output'(ID, Term).
*/
switch t.Functor {
switch t.Functor() {
case "success": // success/5
// id, results, projection, time, more
return p.onSuccess(t.Args[0], t.Args[1], t.Args[2], t.Args[3], t.Args[4])
return p.onSuccess(t.Arg(0), t.Arg(1), t.Arg(2), t.Arg(3), t.Arg(4))
case "failure": // failure/2
// id, time
return p.onFailure(t.Args[0], t.Args[1])
return p.onFailure(t.Arg(0), t.Arg(1))
case "error": // error/2
// id, ball
return p.onError(t.Args[0], t.Args[1])
return p.onError(t.Arg(0), t.Arg(1))
case "create": // create/2
// id, list
return p.onCreate(t.Args[0], t.Args[1])
return p.onCreate(t.Arg(0), t.Arg(1))
case "destroy": // destroy/2
// id, event
return p.onDestroy(t.Args[0], t.Args[1])
return p.onDestroy(t.Arg(0), t.Arg(1))
case "output": // output/2
// TODO: unimplemented
return p.onOutput(t.Args[0], t.Args[1])
return p.onOutput(t.Arg(0), t.Arg(1))
case "prompt": // prompt/2
// TODO
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (p *prologAnswers) onOutput(id, term engine.Term) error {

func (p *prologAnswers) onDestroy(id, t engine.Term) error {
p.eng.die()
goal, ok := t.(*engine.Compound)
goal, ok := t.(engine.Compound)
if ok {
return p.handleEvent(goal)
}
Expand All @@ -227,15 +227,15 @@ func (p *prologAnswers) onCreate(id, data engine.Term) error {
for iter.Next() {
cur := iter.Current()
switch t := cur.(type) {
case *engine.Compound:
switch t.Functor {
case engine.Compound:
switch t.Functor() {
case "slave_limit":
n, ok := t.Args[0].(engine.Integer)
n, ok := t.Arg(0).(engine.Integer)
if ok {
p.eng.openLimit = int(n)
}
case "answer":
goal, ok := t.Args[0].(*engine.Compound)
goal, ok := t.Arg(0).(engine.Compound)
if ok {
defer p.handleEvent(goal)
}
Expand All @@ -247,22 +247,5 @@ func (p *prologAnswers) onCreate(id, data engine.Term) error {

// resolve is a version of Simplify that attempts to loosely occurs-check itself to prevent infinite loops
func resolve(t engine.Term, env *engine.Env, seen map[engine.Term]struct{}) engine.Term {
if seen == nil {
seen = make(map[engine.Term]struct{})
}

if _, ok := seen[t]; ok {
return t
}
seen[t] = struct{}{}

switch t := env.Resolve(t).(type) {
case *engine.Compound:
for i, arg := range t.Args {
t.Args[i] = resolve(arg, env, seen)
}
return t
default:
return t
}
return env.Resolve(t)
}
6 changes: 3 additions & 3 deletions prolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func TestProlog(t *testing.T) {

for as.Next(ctx) {
t.Logf("answer: %+v", as.Current())
cmp, ok := as.Current().(*engine.Compound)
cmp, ok := as.Current().(engine.Compound)
if !ok {
t.Fatal("not a compound", as.Current())
}
if cmp.Functor != "子" {
t.Error("unexpected functor. want: 子 got:", cmp.Functor)
if cmp.Functor() != "子" {
t.Error("unexpected functor. want: 子 got:", cmp.Functor())
}
}
if err := as.Err(); err != nil {
Expand Down
9 changes: 3 additions & 6 deletions term.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,11 @@ func (t Term) Prolog() engine.Term {
}
return engine.Integer(n)
case t.Compound != nil:
c := &engine.Compound{
Functor: engine.Atom(t.Compound.Functor),
Args: make([]engine.Term, 0, len(t.Compound.Args)),
}
args := make([]engine.Term, 0, len(t.Compound.Args))
for _, arg := range t.Compound.Args {
c.Args = append(c.Args, arg.Prolog())
args = append(args, arg.Prolog())
}
return c
return engine.Atom(t.Compound.Functor).Apply(args...)
case t.Variable != nil:
// TODO(guregu): what should this be? engine.NewVariable? Is this even useful?
return engine.Variable(*t.Variable)
Expand Down

0 comments on commit b8dfcd8

Please sign in to comment.