🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #1649

Re: co-routining

#1649
From: Lars-Henrik Eriksson
Date: Fri, 07 Dec 1990 10:10
50 lines
1974 bytes
In article <TED.90Dec6145517@aiaia.nmsu.edu>, ted@nmsu (Ted Dunning) writes:
>
>
>i have two questions about prologs which contain a freeze/2 predicate
>such as sicstus prolog.
>
>1) how can one delay a goal until _either_ of two variables are
>instantiated?
>
>something like this could be done by the following,
>
>freeze(X,Y,G) :- freeze(X,G), freeze(Y,G).
>
>but this has the nasty consequence that G is called twice.

Try

freeze(X, Y, G) :- freeze(X, Flag = 1), freeze(Y, Flag = 1), freeze(Flag, G).

>
>2) how can freeze/2 be used to do co-routining?  does this require a
>modification of what is normally taken to be co-routining?

To implement coroutining in a producer-consumer context, use the
following general scheme.

start :- consume(X), produce(X).

produce([]) :- ......./* Stop condition */.
produce([X|Y]) :- ......./* Construct X*/, produce(Y).

consume(X) :- freeze(X, consume1(X)).

consume1([]).
consume1([X|Y]) :- ......./* Use X */, consume(Y).


In Sicstus prolog you can instead of the consume/consume1-pair write

:- wait consume/1.

consume([]).
consume([X|Y]) :- ......./* Use X */, consume(Y).

which is cleaner.
-- 
Lars-Henrik Eriksson				Internet: lhe@sics.se
Swedish Institute of Computer Science		Phone (intn'l): +46 8 752 15 09
Box 1263					Telefon (nat'l): 08 - 752 15 09
S-164 28  KISTA, SWEDEN
Technical Details
Message-ID: <1990Dec7.101054.5312@sics.se>
Path: arc1-ca.pugleaf.net!utzoo!attcan!telly!lethe!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!sics.se!lhe
References: <TED.90Dec6145517@aiaia.nmsu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!telly!lethe!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!sics.se!lhe
From: lhe@yang (Lars-Henrik Eriksson)
Newsgroups: comp.lang.prolog
Subject: Re: co-routining
Message-ID: <1990Dec7.101054.5312@sics.se>
Date: 7 Dec 90 10:10:54 GMT
References: <TED.90Dec6145517@aiaia.nmsu.edu>
Sender: lhe@sics.se (Lars-Henrik Eriksson)
Reply-To: lhe@yang (Lars-Henrik Eriksson)
Distribution: comp
Organization: Swedish Institute of Computer Science, Kista, Sweden
Lines: 50
In-Reply-To: ted@nmsu.edu (Ted Dunning)