forked from nicolas-van/bootstrap-4-github-pages
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsolutions.gr
102 lines (69 loc) · 2.11 KB
/
solutions.gr
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- # Section 2
-- ## Q1
dub : Int → Int
dub x = 2 * x
-- ## Q2
const : forall {a b : Type} . a -> b [] -> a
const x [y] = x
ap : forall {a b c : Type}. (c -> a -> b) -> (c -> a) -> (c [] -> b)
ap f x [ctx] = f ctx (x ctx)
-- ## Q3
import Bool
copyBool : Bool -> (Bool, Bool)
copyBool False = (False, False);
copyBool True = (True, True)
-- ## Q4
andShortCircuit : Bool -> Bool [] -> Bool
andShortCircuit False [_] = False;
andShortCircuit True [x] = x
-- # Section 3
-- ## Q1
twice : forall {a : Type} . (a -> a) [2] -> a -> a
twice [f] x = f (f x)
example31 : Int
example31 = twice [\x -> x + 1] 1
-- ## Q2
twiceP : forall {a : Type, r : Semiring} . (a -> a) [(1 + 1):r] -> a -> a
twiceP [f] x = f (f x)
twice' : forall {a : Type} . (a -> a) [2] -> a -> a
twice' = twiceP
-- ## Q3
import Cake
mange : forall {n : Nat, m : Nat} . Cake [n + m] -> (Cake [n], Happy [m])
mange [c] = ([have c], [eat c])
-- ## Q4
data ABVec (a : Type) (b : Type) (n : Nat) (m : Nat) where
NilAB : ABVec a b 0 0;
ConsA : a -> ABVec a b n m -> ABVec a b (n+1) m;
ConsB : b -> ABVec a b n m -> ABVec a b n (m+1)
abmap : forall {a a' b b' : Type, n m : Nat} . (a -> a') [n] -> (b -> b') [m] -> ABVec a b n m -> ABVec a' b' n m
abmap [f] [g] NilAB = NilAB;
abmap [f] [g] (ConsA x xs) = ConsA (f x) (abmap [f] [g] xs);
abmap [f] [g] (ConsB x xs) = ConsB (g x) (abmap [f] [g] xs)
-- ## Q5
import Vec
data Patient where
Patient : (city : String [Public]) -> (name : String [Private]) -> Patient
reducePublic : forall {a : Type, n : Nat}
. (String -> a -> a) [n] -> a -> (Vec n Patient) -> a
reducePublic [f] d Nil = d;
reducePublic [f] d (Cons (Patient [city] [_]) xs) = f city (reducePublic [f] d xs)
-- # Section 4
-- ## Q1
boolToInt : LChan (Recv Bool (Send Int End)) -> ()
boolToInt c =
let (b, c) = recv c
in case b of
True -> close (send c 1);
False -> close (send c 0)
-- ## Q2
client : LChan (Send Bool (Recv Int End)) -> Int
client c =
let (i, c) = recv (send c True);
() = close c
in i
-- ## Q3
exampleSession : Int
exampleSession =
let c = forkLinear boolToInt
in client c