forked from idris-lang/Idris-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest014.idr
69 lines (38 loc) · 1.36 KB
/
test014.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Main
import Resimp
data Purpose = Reading | Writing
pstring : Purpose -> String
pstring Reading = "r"
pstring Writing = "w"
data FILE : Purpose -> Type where
OpenH : File -> FILE p
syntax "ifM" [test] "then" [t] "else" [e]
= test >>= (\b => if b then t else e)
open : String -> (p:Purpose) -> Creator (Either () (FILE p))
open fn p = ioc (do Right h <- fopen fn (pstring p)
| Left err => return (Left ())
return (Right (OpenH h)))
close : FILE p -> Updater ()
close (OpenH h) = iou (closeFile h)
readLine : FILE Reading -> Reader String
readLine (OpenH h) = ior (do Right str <- fGetLine h
| return ""
return str)
eof : FILE Reading -> Reader Bool
eof (OpenH h) = ior (fEOF h)
syntax rclose [h] = Update close h
syntax rreadLine [h] = Use readLine h
syntax reof [h] = Use eof h
syntax rputStrLn [s] = Lift (putStrLn s)
syntax rputStr [s] = Lift (putStr s)
syntax "if" "opened" [f] "then" [e] "else" [t] = Check f t e
--------
readH : String -> RES ()
readH fn = res (do let x = open fn Reading
if opened x then
do str <- rreadLine x
rputStr str
rclose x
else rputStrLn "Error")
main : IO ()
main = run (readH "test")