Skip to content

Commit 7de940e

Browse files
committed
chapter 14.1
1 parent 4998480 commit 7de940e

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

chapter13.2.idr

+66-3
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,69 @@ run (More fuel) stk (Do c f) =
6565
run fuel newStk (f res)
6666
run Dry stk p = pure ()
6767

68-
data StkInput = Number Integer | Add
68+
data StkInput = Number Integer | Add | Subtract | Multiply | Negate | Discard | Duplicate
6969

7070
strToInput : String -> Maybe StkInput
71+
strToInput "" = Nothing
72+
strToInput "add" = Just Add
73+
strToInput "subtract" = Just Subtract
74+
strToInput "multiply" = Just Multiply
75+
strToInput "negate" = Just Negate
76+
strToInput "discard" = Just Discard
77+
strToInput "duplicate" = Just Duplicate
78+
strToInput x = if all isDigit (unpack x) then Just (Number (cast x)) else Nothing
79+
80+
7181
mutual
72-
tryAdd : StackIO height
82+
duplicate : StackIO height
83+
duplicate {height = Z} =
84+
do
85+
PutStr "Fewer elements than 1"
86+
stackCalc
87+
duplicate {height = (S k)} =
88+
do
89+
val1 <- Top
90+
Push val1
91+
PutStr ("Duplicated " ++ show val1)
92+
stackCalc
93+
94+
discard : StackIO height
95+
discard {height = Z} =
96+
do
97+
PutStr "Fewer elements than 1"
98+
stackCalc
99+
discard {height = (S k)} =
100+
do
101+
val1 <- Pop
102+
PutStr ("Discarded " ++ show val1)
103+
stackCalc
104+
105+
negate : StackIO height
106+
negate {height = Z} =
107+
do
108+
PutStr "Fewer elements than 1"
109+
stackCalc
110+
negate {height = (S k)} =
111+
do
112+
val1 <- Pop
113+
Push (-val1)
114+
result <- Top
115+
PutStr (show result)
116+
stackCalc
117+
118+
tryBinary : (Integer -> Integer -> Integer) -> StackIO height
119+
tryBinary op {height = (S (S k))} =
120+
do
121+
val1 <- Pop
122+
val2 <- Pop
123+
Push (val2 `op` val1)
124+
result <- Top
125+
PutStr (show result)
126+
stackCalc
127+
tryBinary _ =
128+
do
129+
PutStr "Fewer elements than 2"
130+
stackCalc
73131

74132
stackCalc : StackIO height
75133
stackCalc =
@@ -85,7 +143,12 @@ mutual
85143
do
86144
Push x
87145
stackCalc
88-
Just Add => tryAdd
146+
Just Add => tryBinary (+)
147+
Just Subtract => tryBinary (-)
148+
Just Multiply => tryBinary (*)
149+
Just Negate => negate
150+
Just Discard => discard
151+
Just Duplicate => duplicate
89152

90153
partial
91154
main : IO ()

chapter14.1.idr

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Main
2+
import Data.List.Views
3+
import Data.Vect
4+
import Data.Vect.Views
5+
import Data.Nat.Views
6+
import Data.Bits
7+
8+
%default total
9+
10+
namespace Door
11+
data DoorState = DoorClosed | DoorOpen
12+
data DoorResult = OK | Jammed
13+
14+
data DoorCmd : (ty : Type) -> DoorState -> (ty -> DoorState) -> Type where
15+
Open : DoorCmd DoorResult DoorClosed
16+
(\res =>
17+
case res of
18+
OK => DoorOpen
19+
Jammed => DoorClosed
20+
)
21+
Close : DoorCmd () DoorOpen (const DoorClosed)
22+
RingBell : DoorCmd () s (const s)
23+
24+
Display : String -> DoorCmd () state (const state)
25+
26+
Pure : (res : ty) -> DoorCmd ty (state_fn res) state_fn
27+
(>>=) : DoorCmd a state1 state2_fn -> ((res : a) -> DoorCmd b (state2_fn res) state3_fn) -> DoorCmd b state1 state3_fn
28+
29+
doorProg : DoorCmd () DoorClosed (const DoorClosed)
30+
doorProg =
31+
do
32+
RingBell
33+
jam <- Open
34+
case jam of
35+
OK => Close
36+
Jammed => Display "Something bad happened"
37+
RingBell

0 commit comments

Comments
 (0)