prolog icon indicating copy to clipboard operation
prolog copied to clipboard

Queries without free variables

Open riccardopinosio opened this issue 3 years ago • 6 comments

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 avatar Mar 10 '22 18:03 riccardopinosio

@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().

ichiban avatar Mar 13 '22 07:03 ichiban

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.

riccardopinosio avatar Mar 14 '22 11:03 riccardopinosio

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?

ichiban avatar Mar 15 '22 01:03 ichiban

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?

riccardopinosio avatar Mar 15 '22 16:03 riccardopinosio

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

ichiban avatar Mar 16 '22 01:03 ichiban

I put the example for a query without variables into "Run the Prolog program" section.

ichiban avatar Mar 30 '22 23:03 ichiban

I'll close this issue as we cover this use case in README. Feel free to reopen it if we need further actions.

ichiban avatar Dec 15 '22 12:12 ichiban