-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
65 lines (55 loc) · 2.22 KB
/
Main.hs
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
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
{-# OPTIONS_GHC -Wno-unused-matches #-}
-- | Here are some examples from the report.
module Main where
import Prelude hiding (readFile)
import System.IO.Continuation
import System.IO.Dialogue
-- | A small example using the stream-based I/O. Note the irrefutable patterns!
example1 :: Dialogue
example1 ~(Success: ~((Str userInput) : ~(Success : ~(r4 : _)))) =
[ AppendChan stdout "please type a filename\n"
, ReadChan stdin
, AppendChan stdout name
, ReadFile name
, AppendChan stdout (case r4 of Str contents -> contents
Failure ioerror -> "can't open file")
, AppendChan stdout "\nfinished"
] where (name : _) = lines userInput
-- | Same as 'example1', but using the continuation-based I/O.
example2 :: Dialogue
example2 =
appendChan stdout "please type a filename\n" exit (
readChan stdin exit (\userInput ->
let (name : _) = lines userInput in
appendChan stdout name exit (
readFile name (\ioerror -> appendChan stdout
"can't open file" exit done)
(\contents ->
appendChan stdout contents exit done))))
------------------------------------------------------------
-- | An example involving synchronisation.
program :: Dialogue
program = readChan stdin exit (\userInput -> readNums (lines userInput))
readNums :: [String] -> Dialogue
readNums inputLines =
readInt "Enter first number: " inputLines
(\num1 inputLines1 ->
readInt "Enter second number: " inputLines1
(\num2 _ -> reportResult num1 num2))
reportResult :: Int -> Int -> Dialogue
reportResult num1 num2 =
appendChan stdout ("Their sum is: " ++ show (num1 + num2)) exit done
readInt :: String -> [String] -> (Int -> [String] -> Dialogue) -> Dialogue
readInt prompt inputLines succ =
appendChan stdout prompt exit
(case inputLines of
(l1 : rest) -> case reads l1 of
[(x,"")] -> succ x rest
_ -> appendChan stdout
"Error - retype the number\n" exit
(readInt prompt rest succ)
_ -> appendChan stdout "Early EOF" exit done)
main :: IO ()
main = runDialogue program