Skip to content

Commit

Permalink
add is_list/1 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu authored Apr 6, 2022
1 parent 6ebf8a5 commit 25875f9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Native predicates for [ichiban/prolog](https://github.com/ichiban/prolog).

## Prolog

Filesystem predicates using [`io/fs.FS`](https://pkg.go.dev/io/fs).
Filesystem predicates use [`io/fs.FS`](https://pkg.go.dev/io/fs).

### Built-in replacements

Expand All @@ -20,6 +20,10 @@ These use strings (lists of characters) for filenames.
- `directory_exists/1`
- `file_exists/1`

### Lists

- `is_list/1`

### Package [`taujson`](https://godoc.org/github.com/guregu/predicates/taujson)

These predicates are intended to be compatible with Tau Prolog's [`library(js)`](http://tau-prolog.org/documentation#js).
Expand Down
2 changes: 1 addition & 1 deletion dynamodb/bootstrap.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:- built_in(list_tables/1).
:- built_in(scan/2).
:- built_in(get_item/3).
:- built_in(query/3).
% :- built_in(query/3).
:- built_in(put_item/2).
:- built_in(delete_item/2).
:- built_in(attribute_value/2).
2 changes: 1 addition & 1 deletion dynamodb/dynamo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (d Dynamo) Register(p *prolog.Interpreter) {
p.Register1("list_tables", d.ListTables)
p.Register2("scan", d.Scan)
p.Register3("get_item", d.GetItem)
p.Register3("query", d.Query)
// p.Register3("query", d.Query)
p.Register2("put_item", d.PutItem)
p.Register2("delete_item", d.DeleteItem)
p.Register2("attribute_value", d.AttributeValue)
Expand Down
33 changes: 33 additions & 0 deletions list.go
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)
}
}
34 changes: 34 additions & 0 deletions list_test.go
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.`))
}
4 changes: 2 additions & 2 deletions taujson/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func JSONAtom(js, atom engine.Term, k func(*engine.Env) *engine.Promise, env *en
// JSONProlog (json_prolog/2) succeeds if JS is a native JSON object that represents List.
// This is intended to be compatible with Tau Prolog's library(json).
//
// json_atom(-JS, +List).
// json_atom(+JS, -List).
// json_prolog(-JS, +List).
// json_prolog(+JS, -List).
func JSONProlog(js, value engine.Term, k func(*engine.Env) *engine.Promise, env *engine.Env) *engine.Promise {
var raw *Term
switch js := env.Resolve(js).(type) {
Expand Down

0 comments on commit 25875f9

Please sign in to comment.