@@ -25,6 +25,8 @@ import public Prelude.Pairs
25import public Prelude.Stream
25import public Prelude.Stream
26import public Prelude.Providers
26import public Prelude.Providers
27import public Prelude.Show
27import public Prelude.Show
28+import public Prelude.Interactive
29+import public Prelude.File
28import public Decidable.Equality
30import public Decidable.Equality
29import public Language.Reflection
31import public Language.Reflection
30import public Language.Reflection.Errors
32import public Language.Reflection.Errors
@@ -43,9 +45,6 @@ decAsBool (No _) = False
43instance Functor PrimIO where
45instance Functor PrimIO where
44 map f io = prim_io_bind io (prim_io_return . f)
46 map f io = prim_io_bind io (prim_io_return . f)
46−instance Functor (IO' ffi) where
47− map f io = io_bind io (\b => io_return (f b))
49instance Functor Maybe where
48instance Functor Maybe where
50 map f (Just x) = Just (f x)
49 map f (Just x) = Just (f x)
51 map f Nothing = Nothing
50 map f Nothing = Nothing
@@ -61,12 +60,6 @@ instance Applicative PrimIO where
62 am <*> bm = prim_io_bind am (\f => prim_io_bind bm (prim_io_return . f))
61 am <*> bm = prim_io_bind am (\f => prim_io_bind bm (prim_io_return . f))
64−instance Applicative (IO' ffi) where
66− f <*> a = io_bind f (\f' =>
70instance Applicative Maybe where
63instance Applicative Maybe where
71 pure = Just
64 pure = Just
@@ -103,9 +96,6 @@ instance Alternative List where
103instance Monad PrimIO where
96instance Monad PrimIO where
104 b >>= k = prim_io_bind b k
97 b >>= k = prim_io_bind b k
106−instance Monad (IO' ffi) where
107− b >>= k = io_bind b k
109instance Monad Maybe where
99instance Monad Maybe where
110 Nothing >>= k = Nothing
100 Nothing >>= k = Nothing
111 (Just x) >>= k = k x
101 (Just x) >>= k = k x
@@ -229,183 +219,6 @@ curry f a b = f (a, b)
229uncurry : (a -> b -> c) -> (a, b) -> c
219uncurry : (a -> b -> c) -> (a, b) -> c
230uncurry f (a, b) = f a b
220uncurry f (a, b) = f a b
234−||| Output a string to stdout without a trailing newline
235−putStr : String -> IO' ffi ()
236−putStr x = do prim_write x
239−||| Output a string to stdout with a trailing newline
240−putStrLn : String -> IO' ffi ()
241−putStrLn x = putStr (x ++ "\n")
243−||| Output something showable to stdout, without a trailing newline
245−print : Show a => a -> IO' ffi ()
246−print x = putStr (show x)
248−||| Output something showable to stdout, with a trailing newline
250−printLn : Show a => a -> IO' ffi ()
251−printLn x = putStrLn (show x)
253−||| Read one line of input from stdin, without the trailing newline
255−getLine : IO' ffi String
256−getLine = do x <- prim_read
257− return (reverse (trimNL (reverse x)))
258− where trimNL : String -> String
259− trimNL str with (strM str)
260− trimNL "" | StrNil = ""
261− trimNL (strCons '\n' xs) | StrCons _ _ = xs
262− trimNL (strCons x xs) | StrCons _ _ = strCons x xs
264−||| Write a single character to stdout
266−putChar : Char -> IO ()
267−putChar c = foreign FFI_C "putchar" (Int -> IO ()) (cast c)
269−||| Write a singel character to stdout, with a trailing newline
271−putCharLn : Char -> IO ()
272−putCharLn c = putStrLn (singleton c)
274−||| Read a single character from stdin
277−getChar = map cast $ foreign FFI_C "getchar" (IO Int)
279−---- some basic file handling
283−data File = FHandle Ptr
287−stdin = FHandle prim__stdin
291−stdout = FHandle prim__stdout
295−stderr = FHandle prim__stderr
297−||| Call the RTS's file opening function
298−do_fopen : String -> String -> IO Ptr
300− = foreign FFI_C "fileOpen" (String -> String -> IO Ptr) f m
304−||| @ m the mode as a String (`"r"`, `"w"`, or `"r+"`)
305−fopen : (f : String) -> (m : String) -> IO File
306−fopen f m = do h <- do_fopen f m
309−||| Modes for opening files
310−data Mode = Read | Write | ReadWrite
316−openFile : (f : String) -> (m : Mode) -> IO File
317−openFile f m = fopen f (modeStr m) where
320− modeStr ReadWrite = "r+"
323−do_fclose : Ptr -> IO ()
324−do_fclose h = foreign FFI_C "fileClose" (Ptr -> IO ()) h
327−closeFile : File -> IO ()
328−closeFile (FHandle h) = do_fclose h
331−do_fread : Ptr -> IO' l String
332−do_fread h = prim_fread h
334−fgetc : File -> IO Char
335−fgetc (FHandle h) = return (cast !(foreign FFI_C "fgetc" (Ptr -> IO Int) h))
337−fgetc' : File -> IO (Maybe Char)
339− = do x <- foreign FFI_C "fgetc" (Ptr -> IO Int) h
340− if (x < 0) then return Nothing
341− else return (Just (cast x))
343−fflush : File -> IO ()
344−fflush (FHandle h) = foreign FFI_C "fflush" (Ptr -> IO ()) h
346−do_popen : String -> String -> IO Ptr
347−do_popen f m = foreign FFI_C "do_popen" (String -> String -> IO Ptr) f m
349−popen : String -> Mode -> IO File
350−popen f m = do ptr <- do_popen f (modeStr m)
351− return (FHandle ptr)
355− modeStr ReadWrite = "r+"
357−pclose : File -> IO ()
358−pclose (FHandle h) = foreign FFI_C "pclose" (Ptr -> IO ()) h
360−-- mkForeign (FFun "idris_readStr" [FPtr, FPtr] (FAny String))
364−fread : File -> IO' l String
365−fread (FHandle h) = do_fread h
368−do_fwrite : Ptr -> String -> IO ()
369−do_fwrite h s = do prim_fwrite h s
373−fwrite : File -> String -> IO ()
374−fwrite (FHandle h) s = do_fwrite h s
377−do_feof : Ptr -> IO Int
378−do_feof h = foreign FFI_C "fileEOF" (Ptr -> IO Int) h
380−||| Check if a file handle has reached the end
381−feof : File -> IO Bool
382−feof (FHandle h) = do eof <- do_feof h
383− return (not (eof == 0))
386−do_ferror : Ptr -> IO Int
387−do_ferror h = foreign FFI_C "fileError" (Ptr -> IO Int) h
389−ferror : File -> IO Bool
390−ferror (FHandle h) = do err <- do_ferror h
391− return (not (err == 0))
393−fpoll : File -> IO Bool
394−fpoll (FHandle h) = do p <- foreign FFI_C "fpoll" (Ptr -> IO Int) h
397−||| Check if a foreign pointer is null
399−nullPtr : Ptr -> IO Bool
400−nullPtr p = do ok <- foreign FFI_C "isNull" (Ptr -> IO Int) p
403−||| Check if a supposed string was actually a null pointer
405−nullStr : String -> IO Bool
406−nullStr p = do ok <- foreign FFI_C "isNull" (String -> IO Int) p
409namespace JSNull
222namespace JSNull
410 ||| Check if a foreign pointer is null
223 ||| Check if a foreign pointer is null
@@ -425,12 +238,6 @@ eqPtr : Ptr -> Ptr -> IO Bool
425eqPtr x y = do eq <- foreign FFI_C "idris_eqPtr" (Ptr -> Ptr -> IO Int) x y
238eqPtr x y = do eq <- foreign FFI_C "idris_eqPtr" (Ptr -> Ptr -> IO Int) x y
426 return (eq /= 0)
239 return (eq /= 0)
428−||| Check whether a file handle is actually a null pointer
430−validFile : File -> IO Bool
431−validFile (FHandle h) = do x <- nullPtr h
434||| Loop while some test is true
241||| Loop while some test is true
436||| @ test the condition of the loop
243||| @ test the condition of the loop
@@ -442,22 +249,6 @@ while t b = do v <- t
442 while t b
249 while t b
443 else return ()
250 else return ()
445−||| Read the contents of a file into a string
446−partial -- no error checking!
447−readFile : String -> IO String
448−readFile fn = do h <- openFile fn Read
454− readFile' : File -> String -> IO String
455− readFile' h contents =
457− if not x then do l <- fread h
458− readFile' h (contents ++ l)
459− else return contents
461------- Some error rewriting
252------- Some error rewriting
463%language ErrorReflection
254%language ErrorReflection