-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package predicates | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
) | ||
|
||
// IsList (is_list/1) succeeds if the given term is a list. | ||
// | ||
// is_list(@Term). | ||
func IsList(t engine.Term, k func(*engine.Env) *engine.Promise, env *engine.Env) *engine.Promise { | ||
switch t := env.Resolve(t).(type) { | ||
case engine.Variable: | ||
return engine.Bool(false) | ||
case engine.Atom: | ||
if t != "[]" { | ||
return engine.Bool(false) | ||
} | ||
return k(env) | ||
case *engine.Compound: | ||
iter := engine.ListIterator{List: t, Env: env} | ||
for iter.Next() { | ||
} | ||
if iter.Err() != nil { | ||
log.Println("ITER ERR", iter.Err()) | ||
return engine.Bool(false) | ||
} | ||
return k(env) | ||
default: | ||
return engine.Bool(false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package predicates | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/guregu/predicates/internal" | ||
) | ||
|
||
func TestIsList(t *testing.T) { | ||
p := internal.NewTestProlog() | ||
p.Register1("is_list", IsList) | ||
|
||
t.Run("term is empty list", p.Expect(internal.TestOK, | ||
`is_list([]), OK = true.`)) | ||
|
||
t.Run("term is list", p.Expect(internal.TestOK, | ||
`is_list([a, b, c]), OK = true.`)) | ||
|
||
t.Run("term is unground list", p.Expect(internal.TestOK, | ||
`is_list([_]), OK = true.`)) | ||
|
||
// this is true on SWI and false on Tau, so let's side with Tau for now | ||
t.Run("term is [_|_]", p.Expect(internal.TestFail, | ||
`is_list([_|_]), OK = true.`)) | ||
|
||
t.Run("term is atom", p.Expect(internal.TestFail, | ||
`is_list(foo), OK = true.`)) | ||
|
||
t.Run("term is number", p.Expect(internal.TestFail, | ||
`is_list(555), OK = true.`)) | ||
|
||
t.Run("term is variable", p.Expect(internal.TestFail, | ||
`is_list(X), OK = true.`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters