[clean-list] Problems with opening a file to read [Char]

Stephen Tetley rt014i7347@blueyonder.co.uk
Sun, 05 Sep 2004 19:51:11 +0100


Hello

This should do what you are expecting, I've decomposed the imperative
part in the Start function a bit so it should be more explicit as to 
what it's
doing:

module filetest

import StdEnv


openFile :: String *env -> (File, *env) | FileSystem env
openFile inputfname filesys
    | not readok     = abort ("error")
    | otherwise        = (inputfile, touchedfs)
where
    (readok, inputfile, touchedfs) = sfopen inputfname FReadText filesys

readChars :: File -> [Char]
readChars f
    | not readok     = []
    | otherwise        = [ char : readChars alteredfile ]
where
    (readok,char,alteredfile) = sfreadc f



//open a file, read the chars and print them
Start :: *World -> *World
Start world
    # (console, world)    = stdio world
      (file, world)        = openFile "filetest.icl" world
      text_list            = readChars file
      console            = fwrites (toString text_list)  console
      (ok,world)        = fclose console world
    | not ok              = abort "Cannot close console"
    | otherwise            = world


Sander van den Berg wrote:

> Hello list.
>
> Im trying to open a text file to read its contents to a string. The 
> following code is what I have now:
>
> module filetest
>
> import StdEnv
>
>
> openFile :: String *env -> (File, *env)
> openFile inputfname filesys
>     | not readok     = abort ("error")
>     | otherwise        = (inputfile, touchedfs)
> where
>     (readok, inputfile, touchedfs) = sfopen inputfname FReadText filesys
>
> readChar :: File -> [Char]
> readChar f
>     | not readok     = []
>     | otherwise        = [ char : readChar alteredfile ]
> where
>     (readok,char,alteredfile) = sfreadc f
>
>
>
> //open a file, read the chars and print them
> Start :: *world -> *world
> Start world
>     #    (console, world)= stdio world
>         console         = fwrites ((readChar o fst) (openFile 
> "filetest.icl" world)) console
>         (ok,console)    = fclose console world
>     |    not ok            = abort "Cannot close console"
>     |    otherwise        = world
>
>
> This code is inspired by the code in the Clean book. The problem I 
> have with it are as follows:
>
> -How do I get a File to pass to readChar. 

See the line:
text_list            = readChars file

Applying (readChar o fst) (openFile "filetest.icl" world) isn't really a 
good idea when you are using
Clean's imperative notation, the second part of the tuple returned by 
openFile has modified the
"world" so you ought to account for it. That's why I do, this in the 
precedding line:

(file, world)        = openFile "filetest.icl" world


>
> -The error I get compiling is: internal overloading of "sfopen" could 
> not be resolved. I dont overload it? 

When you get this type of error it means you have to qualify a type 
variable in the type declaration
with the type class that includes the function thats overloading cannot 
be deduced.

In this case "env" is the type variable and "sfopen" in a member of the 
FileSystem type class, so the
type signature now becomes:

openFile :: String *env -> (File, *env) | FileSystem env

>
> -How do you construct a string, as in Clean String != [Char] according 
> to the compiler messages.

There is an instance of the the type class "toString" for lists of chars 
so just do:

toString char_list

Best wishes

Stephen Tetley

>
> Regards,
>
> Sander.
>
>
>
> _______________________________________________
> clean-list mailing list
> clean-list@cs.kun.nl
> http://www.cs.kun.nl/mailman/listinfo/clean-list
>
>
>