-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter11.2.idr
59 lines (48 loc) · 1.33 KB
/
chapter11.2.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
module Main
import Data.List.Views
import Data.Vect
import Data.Vect.Views
import Data.Nat.Views
import Data.Bits
%default total
data InfIO : Type where
Do : IO a -> (a -> Inf InfIO) -> InfIO
(>>=) : IO a -> (a -> Inf InfIO) -> InfIO
(>>=) = Do
data Fuel = Dry | More (Lazy Fuel)
loopPrint : String -> InfIO
loopPrint msg = Do (putStrLn msg) (\_ => loopPrint msg)
run : Fuel -> InfIO -> IO ()
run (More fuel) (Do c f) =
do
res <- c
run fuel (f res)
run Dry p = putStrLn "Out of fuel"
quiz : Stream Int -> (score : Nat) -> InfIO
quiz (num1 :: num2 :: nums) score =
do
putStrLn ("Score so far: " ++ show score)
putStr (show num1 ++ "*" ++ show num2 ++ "? ")
answer <- getLine
if (cast answer == num1 * num2)
then
do
putStrLn "Correct!"
quiz nums (score + 1)
else
do
putStrLn ("Wrong, the answer is " ++ show (num1 * num2))
quiz nums score
covering
forever : Fuel
forever = More forever
totalREPL : (prompt : String) -> (action : String -> String) -> InfIO
totalREPL prompt action =
do
putStr prompt
input <- getLine
putStr $ action input
totalREPL prompt action
covering
main : IO ()
main = run forever (quiz [1..] 0)