Queries without free variables
Hello,
Great project!! I tried out the example and it works as expected, however I'm having some issues with queries without free variables. In particular, in the example in the readme, I would expect that the query
mortal(socrates)
Should return yes. At least that's what swi-prolog does. Are queries like these supported?
@riccardopinosio Hi, sorry for late reply! I've been staying in bed after the booster shot.
Thank you for your kind words! Of course queries without variables are supported as well! Here's an example on the Go Playground: https://go.dev/play/p/dGVoi85UVMP
package main
import (
"fmt"
"github.com/ichiban/prolog"
)
func main() {
p := prolog.New(nil, nil)
if err := p.Exec(`
human(socrates).
mortal(X) :- human(X).
`); err != nil {
panic(err)
}
sols, err := p.Query(`mortal(socrates).`)
if err != nil {
panic(err)
}
defer sols.Close()
for sols.Next() {
fmt.Printf("yes!\n")
}
}
Instead of printing yes to tell users there was a solution, this library tells the host Go program by executing for sols.Next() {} block. (Just you don't have any variables to Scan())
See how output changes after commenting out human(socrates). or adding multiple human(socrates). in Exec().
Ok clear. Would you mind if I/you added that example to the example folder? I think it would help people getting starting with the library.
It would be great if we explained this use case somewhere but I don't think /examples is a good place for it.
Maybe expand "Run the Prolog program" section in README? What do you think?
Yeh that's probably better. I can add it in a fork and then create a PR if that's ok with you. How about examples on how to use use assertz/retract and similar? I think a couple of more of those would help newbies like me. Would those be candidates for the /examples folder?
I'd appreciate it if you could make a PR for it!
Regarding assertz, retract, or any other built-in predicates, I think the best way to showcase them is to create Testable Examples.
I did it for some of the recently added predicates:
https://github.com/ichiban/prolog/blob/6d174c70bca7f45597f494dae19b9219764f8e6e/interpreter_test.go#L616 https://github.com/ichiban/prolog/blob/6d174c70bca7f45597f494dae19b9219764f8e6e/interpreter_test.go#L634
I put the example for a query without variables into "Run the Prolog program" section.
I'll close this issue as we cover this use case in README. Feel free to reopen it if we need further actions.