Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Trying out the REPL

36 views
Skip to first unread message

Alper Çuğun

unread,
Sep 11, 2024, 4:44:26 PM9/11/24
to Picat

Hi,

I'm trying out the stuff from the user guide in the REPL, but I'm getting these kind of errors:

Picat> fib(0) = 1.
*** Undefined procedure: fib/1

Picat> fib(0, F) => F=1.
*** SYNTAX ERROR *** (0-1) token '=>' or '?=>' cannot appear in a rule body.
fib(0, F) =>  <<HERE>>
             F=1.

Is this not how this should be used?

What's the best way to try out things and iterate quickly?

Best,
Alper

Hakan Kjellerstrand

unread,
Sep 11, 2024, 6:12:28 PM9/11/24
to Picat
Hi.

The fib function is not available in Picat from the start, and it must first be defined so Picat can recognize it. There are some ways to do this:

* Generally the best way is to create a text file with the extension .pi - for example fib.pi - that contain the definition . You can then load it into the Picat REPL via the cl command:
   Picat> cl(fib)
  
and then run it:
   Picat> X=fib(10)
   X = 55

  If this does not work, see chapter 2 on how to run a Picat program.

   Having the function defined in a Picat program makes it easier of you then want to do some more experiments, for example like this program
  """
  main =>
     X = fib(10),
     println(X),
    nl.

   fib(0) = 1.
   fib(1) = 1.
   fib(N) = F, N>1 => F = fib(N-1)+fib(N-2).
  """"

* The other way is to define the fib function directly into the REPL using the cl() function without any filename and then paste the definition:
   Picat> cl
   fib(0) = 1.
   fib(1) = 1.
   fib(N) = F, N>1 => F = fib(N-1)+fib(N-2).
   <Ctrl-D to end input>
  Picat> X=fib(10)
  X = 55

  (The exact combination to end the input depends on the operating system you are on. On Linux it's Ctrl-D and Ctrl-Z on Windows. See more on this in section "2.1.2
How to Use the Command-line Editor" ).

   This works but there's no way to edit the definition in the REPL if you want to change it, but it might work to paste a corrected version using cl() again. This is why it's better to use files.

  That being said, for short definitions, for example if I just want to do a quick test, this can be quite useful.

More on the REPL is in the User Guide section "2.1.2 How to Use the Command-line Editor" .

A side note: If you find that the fib function is too slow  on larger values of N (say N=50), then look at chapter 7 on tabling (memoisation/caching) of the result of previous calls of a function/predicate.

Hope this helps.

/Hakan


Doug Edmunds

unread,
Sep 11, 2024, 6:54:38 PM9/11/24
to Picat
See also page 5 of the User Guide (Sec 1.2) which uses fib as an example. 

Alper Çuğun

unread,
Sep 12, 2024, 3:31:46 AM9/12/24
to Picat
On Thursday 12 September 2024 at 00:12:28 UTC+2 Hakan Kjellerstrand wrote:
* Generally the best way is to create a text file with the extension .pi - for example fib.pi - that contain the definition . You can then load it into the Picat REPL via the cl command:
   Picat> cl(fib)

OK. Thanks. I got this and I see in the book that everything that starts with "Picat>" can be entered into the REPL.

I think it would be nice to have an aside about that in the beginning to make it clear that this is the case and that all the code that does not start with Picat> needs to be loaded from a .pi file.
 
   This works but there's no way to edit the definition in the REPL if you want to change it, but it might work to paste a corrected version using cl() again. This is why it's better to use files.

I think loading source files in the REPL with cl() is a bit tedious so I'd need to setup my editor to run the current file with CMD-R or something.

Best,
Alper
Reply all
Reply to author
Forward
0 new messages