[clean-list] Really Newbie Question about loops/input

Maarten de Mol maartenm@cs.kun.nl
Tue, 2 Oct 2001 13:23:11 +0200


----- Original Message -----
From: "Jay Kint" <jean-luc-picard@ussenterprise.com>
To: <clean-list@cs.kun.nl>
Sent: Tuesday, October 02, 2001 10:37 AM
Subject: [clean-list] Really Newbie Question about loops/input


> What I'm trying to do is create a simple definition I can build on to
print
> a prompt, accept a line of text, and then act on it.
>
> Here is the source code so far:
>
> getCommand :: !*File -> String
> getCommand console = fst (freadline (fwritec '>' console))
>
> commandLoop :: !*File (*File->String) -> *File
> commandLoop console get
>     = commandLoop (fwrites ((get console) +++ " ")
>          console) get
>
> Start :: *World -> *File
> Start world
>  # (console,world) = stdio world
>  = commandLoop console getCommand
>
> Trying to compile this I get:
> "Type Error [116,commandLoop]:"get" * attribute required but not offered
at
> the indicated position: ^ File
>
> This refers to the line "commandLoop console get".  There are several
> questions I have.  First of all, get is supposed to be a higher order
> function, getCommand, that prints a prompt and reads a line of text.  (It
> works if I put it in the Start function such as Start = getCommand
console).
>
> The intent is for getCommand to read in a line, and then to do it over and
> over again.  Later, I'll add a handler function to replace the fwrites in
> the expression that will act on the commands.
>
> First of all, what does the error message mean?  What does the "*
attribute
> required" but not offered mean when in fact I have a function that takes
the
> exact parameter types its looking for (or so I think).

The "*"-attribute denotes uniqueness. Unique values may be used only once in
a program. For a detailed explanation of uniqueness see the "Clean Book"
(accessible via the Clean homepage).

In your program, the 'console' is a unique value which is used more than
once.
In fact, it is used twice in each iteration of commandLoop: once for reading
a line of input and once for writing a line of output.

If you want to you use the 'console' more than once, you have to use the
return value of 'freadline' (the second part of the tuple) and feed this
*File
back again to the 'fwrites' (which then feeds back its result value to the
recursive call).

As a rule of thumb: a function which has a unique argument, usually also has
to
return (a modified value) of that unique argument as output. If it doesn't,
this
means that the unique argument is completely discarded and will never be
used again.

>
> Second of all, what does the "^ File" at the end of the error message
mean?
> Is there an operator "^"?  I haven't seen it mentioned to date.
>

The "^" is a pointer to the word "indicated" in the error message itself.

>
> Jay the Stumped
>

Hope this helps,
    Maarten de Mol
    University of Nijmegen