Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

As for ListIterator's TypeErrorList, the culprit must be the entire list #193

Merged
merged 1 commit into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions engine/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2807,21 +2807,19 @@ func Nth1(n, list, elem Term, k func(*Env) *Promise, env *Env) *Promise {
func nth(base Integer, n, list, elem Term, k func(*Env) *Promise, env *Env) *Promise {
switch n := env.Resolve(n).(type) {
case Variable:
const idx = Atom("$idx")
var ks []func(context.Context) *Promise
iter := ListIterator{List: list, Env: env}
if !iter.Next() {
if err := iter.Err(); err != nil {
return Error(err)
}
return Bool(false)
for i := base; iter.Next(); i++ {
i, e := i, iter.Current()
ks = append(ks, func(context.Context) *Promise {
return Unify(idx.Apply(n, elem), idx.Apply(i, e), k, env)
})
}
car, cdr := iter.Current(), iter.List

const idx = Atom("$idx")
return Delay(func(context.Context) *Promise {
return Unify(idx.Apply(n, elem), idx.Apply(base, car), k, env)
}, func(context.Context) *Promise {
return nth(base+1, n, cdr, elem, k, env)
})
if err := iter.Err(); err != nil {
return Error(err)
}
return Delay(ks...)
case Integer:
if n < base {
return Bool(false)
Expand Down
18 changes: 10 additions & 8 deletions engine/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ type ListIterator struct {
List Term
Env *Env

current Term
err error
current, rest Term
err error
}

// Next proceeds to the next element of the list and returns true if there's such an element.
func (i *ListIterator) Next() bool {
switch l := i.Env.Resolve(i.List).(type) {
if i.rest == nil {
i.rest = i.List
}
switch l := i.Env.Resolve(i.rest).(type) {
case Variable:
i.err = ErrInstantiation
return false
case Atom:
if l != "[]" {
i.err = TypeErrorList(l)
i.err = TypeErrorList(i.List)
}
return false
case *Compound:
if l.Functor != "." || len(l.Args) != 2 {
i.err = TypeErrorList(l)
i.err = TypeErrorList(i.List)
return false
}
i.List = l.Args[1]
i.current = l.Args[0]
i.current, i.rest = l.Args[0], l.Args[1]
return true
default:
i.err = TypeErrorList(l)
i.err = TypeErrorList(i.List)
return false
}
}
Expand Down
10 changes: 5 additions & 5 deletions engine/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestListIterator_Next(t *testing.T) {
assert.True(t, iter.Next())
assert.Equal(t, Atom("b"), iter.Current())
assert.False(t, iter.Next())
assert.Equal(t, TypeErrorList(Atom("foo")), iter.Err())
assert.Equal(t, TypeErrorList(ListRest(Atom("foo"), Atom("a"), Atom("b"))), iter.Err())
})

t.Run("compound", func(t *testing.T) {
Expand All @@ -47,7 +47,7 @@ func TestListIterator_Next(t *testing.T) {
assert.True(t, iter.Next())
assert.Equal(t, Atom("b"), iter.Current())
assert.False(t, iter.Next())
assert.Equal(t, TypeErrorList(Atom("f").Apply(Integer(0))), iter.Err())
assert.Equal(t, TypeErrorList(ListRest(Atom("f").Apply(Integer(0)), Atom("a"), Atom("b"))), iter.Err())
})

t.Run("other", func(t *testing.T) {
Expand All @@ -57,7 +57,7 @@ func TestListIterator_Next(t *testing.T) {
assert.True(t, iter.Next())
assert.Equal(t, Atom("b"), iter.Current())
assert.False(t, iter.Next())
assert.Equal(t, TypeErrorList(&mockTerm{}), iter.Err())
assert.Equal(t, TypeErrorList(ListRest(&mockTerm{}, Atom("a"), Atom("b"))), iter.Err())
})
})
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestAltIterator_Next(t *testing.T) {
})
}

func TestPIIterator_Next(t *testing.T) {
func TestAnyIterator_Next(t *testing.T) {
t.Run("proper list", func(t *testing.T) {
iter := AnyIterator{Any: List(Atom("a"), Atom("b"), Atom("c"))}
assert.True(t, iter.Next())
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestPIIterator_Next(t *testing.T) {
assert.True(t, iter.Next())
assert.Equal(t, Atom("b"), iter.Current())
assert.False(t, iter.Next())
assert.Equal(t, TypeErrorList(Atom("foo")), iter.Err())
assert.Equal(t, TypeErrorList(ListRest(Atom("foo"), Atom("a"), Atom("b"))), iter.Err())
})
})

Expand Down