ichiban / prolog

The only reasonable scripting engine for Go.
MIT License
564 stars 27 forks source link

read/1 reads/waits too long #300

Closed UWN closed 1 year ago

UWN commented 1 year ago
?- read(X),X.
|: write(ok),nl.
|: 
|: 
|: 
|: 
ok
X = write(ok),nl.
?- 

in this case read/1 does not succeed immediately after receiving the first return, but wait for further input, after a couple of returns it finalle succeeded. read/1 must succeed immediately after reading the first return, actually after reading .return since this is unambiguously the end token.

UWN commented 1 year ago

... and sometimes I have to wait even longer...

ichiban commented 1 year ago

It's not simply slow but requires 4 extra lines to complete the read. It could be a bug in 1pl.

?- read(X), read(Y).
|: foo.
|: bar.
|: 
|: 
|: 
|: 
X = foo,
Y = bar.
ichiban commented 1 year ago

I found the cause of the bug. This Peek(5) to detect end_of_stream blocks and requires 4 extra bytes in input.

https://github.com/ichiban/prolog/blob/b77736e7da6b040582bbb2aa5946d04fb6ade010/engine/stream.go#L165-L177

UWN commented 1 year ago

Currently:


ulrich@p0:/opt/gupu/ichiban-prolog$ git log -1
commit b77736e7da6b040582bbb2aa5946d04fb6ade010
Merge: b8ca24c 5970281
Author: Yutaka Ichibangase <yichiban@gmail.com>
Date:   Sat Jul 22 18:20:08 2023 +0900
ichiban commented 1 year ago

I fixed the bug and it doesn't wait for extra bytes in v1.1.3.

$ go install github.com/ichiban/prolog/cmd/1pl@latest
go: downloading github.com/ichiban/prolog v1.1.3
$ $(go env GOPATH)/bin/1pl
Top level for ichiban/prolog v1.1.3
This is for testing purposes only!
See https://github.com/ichiban/prolog for more details.
Type Ctrl-C or 'halt.' to exit.
?- read(X),X.
|: write(ok),nl.
ok
X = write(ok),nl.
?-