unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* prolog, pure guile scheme
@ 2010-07-06 21:52 stefan
  2010-07-08 18:21 ` Andy Wingo
  0 siblings, 1 reply; 2+ messages in thread
From: stefan @ 2010-07-06 21:52 UTC (permalink / raw)
  To: guile-devel

Hi,

I made a pure guile scheme directory under
language/prolog

at git@gitorious.org:guile-unify/guile-unify.git, unify-iso branch!

load the prolog-parser.scm in that directory and then
use for example (prolog-it <str>) e.g. 

(prolog-it "
queens(N,Qs) :- g_pk(N), rangeList(1,N,Ns),g_pk(Ns),queens3(Ns,[],Qs).

queens3(UnplacedQs, SafeQs, Qs) :-
	selectq(Q, UnplacedQs, UnplacedQs1),
	\\+ attack(Q,SafeQs),
	queens3(UnplacedQs1,[Q|SafeQs],Qs).
queens3([],Qs,Qs).

attack(X,Xs) :- attack3(X, 1, Xs).

attack3(X,N,[Y|_])  :- X =:= Y+N ; X =:= Y-N.
attack3(X,N,[_|Ys]) :- N1 is N+1, attack3(X,N1,Ys).

rangeList(M,N,[M])      :- M >= N, !.
rangeList(M,N,[M|Tail]) :- M1 is M+1, rangeList(M1,N,Tail).

selectq(X,[X|Xs],Xs).  
selectq(X,[Y|Ys],[Y|Zs]) :- selectq(X,Ys,Zs). 
")

you could run a query like

(run "queens(10,X)" #f)

Now this works but is slow as molasses. Not enteirly intentional. Part of the
slowness is due to the match algorithm mimicking the fast one in the master 
repo. Now the main reason is due to a problem with prompts that I did not 
solve.

So here is what I would like to use


(match #:tag pr
       Z
       ((a X)  (begin (do-something X pr)
                       (abort-to-prompt pr)))
       ((b X)  (b-it X)))

And this translate to something like

(call-with-prompt
   *prompt*
   (lambda () 
     (let ((pr *prompt*))
       (ma-it Z (a X) 
	      (begin (do-something X pr) ...) 
	      (abort-to-prompt pr))))
   (lambda (s) ...))

Now this works if we stay in the function, but if do-something contains
again a match constructed from the *prompt* prompt, then it get confused
so I was thinking like *prompt* beeing like a fluid let but it's not as it 
seams. At the repo unify-iso I instead construct a fresh new prompt at each 
match invocation which of cause is very costly but correct. I suspect that 
there is a quick hack to fix this and therefore I ask if anyone can help?


Interestingly in the master repo I hacked a prompt like feature that 
is not a delimited continuation but just keeps information of state to 
do a jump later on. 

Regards
Stefan













^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: prolog, pure guile scheme
  2010-07-06 21:52 prolog, pure guile scheme stefan
@ 2010-07-08 18:21 ` Andy Wingo
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Wingo @ 2010-07-08 18:21 UTC (permalink / raw)
  To: stefan; +Cc: guile-devel

Hi Stefan,

On Tue 06 Jul 2010 22:52, stefan <stefan.tampe@spray.se> writes:

> So here is what I would like to use
>
> (match #:tag pr
>        Z
>        ((a X)  (begin (do-something X pr)
>                        (abort-to-prompt pr)))
>        ((b X)  (b-it X)))
>
> And this translate to something like
>
> (call-with-prompt
>    *prompt*
>    (lambda () 
>      (let ((pr *prompt*))
>        (ma-it Z (a X) 
> 	      (begin (do-something X pr) ...) 
> 	      (abort-to-prompt pr))))
>    (lambda (s) ...))
>
> Now this works if we stay in the function, but if do-something contains
> again a match constructed from the *prompt* prompt, then it get confused
> so I was thinking like *prompt* beeing like a fluid let but it's not as it 
> seams. At the repo unify-iso I instead construct a fresh new prompt at each 
> match invocation which of cause is very costly but correct. I suspect that 
> there is a quick hack to fix this and therefore I ask if anyone can
> help?

Let me see if I understand you: you want to be able to do a match, and
if it fails, abort the match.

If that is the case, you will need N prompts, with N unique prompt
tags. Otherwise you could just use one tag, and pass some auxiliary
identifier to the abort handlers; but that is silly, unless you need to
unwind somehow, and in that case you could handle it with dynamic-wind
anyway.

So why not have the second argument to `do-something' not be a tag, but
instead a procedure, e.g. (lambda () (abort-to-prompt pr)). You generate
the prompt tag local to the invocation of `match', and pass the thunk
encapsulating that tag to nested match helper procedures. If the helper
fails, it invokes the thunk. Otherwise you could bind the thunk to a
fluid using with-fluids, as is done by `catch' et al -- but that would
make it be *the* abort handler for the dynamic extent of that invocation
to match.

That's one way to do it, anyway. Let me know if I'm misunderstanding the
issue.

Cheers,

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-07-08 18:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-06 21:52 prolog, pure guile scheme stefan
2010-07-08 18:21 ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).