-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVideo12.scala
131 lines (97 loc) · 3.65 KB
/
Video12.scala
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package livevideos
object Video12 extends App:
import org.justinhj.typeclasses.monad.{eitherMonad, _}
import org.justinhj.typeclasses.numeric.{given, _}
case class ReaderT[F[_],R,A](run: R => F[A])
object ReaderT:
def lift[F[_],R,A](fa: F[A]): ReaderT[F,R,A] = ReaderT(_ => fa)
given readerTMonad[F[_]: Monad,R]: Monad[[A1] =>> ReaderT[F,R,A1]] with
def pure[A](a:A): ReaderT[F,R,A] = ReaderT(_ => Monad[F].pure(a))
extension [A,B](far: ReaderT[F,R,A])
def flatMap(f: A => ReaderT[F,R,B]): ReaderT[F,R,B] = {
ReaderT((r: R) =>
val fa: F[A] = far.run(r)
val fb: F[B] = fa.flatMap(b => f(b).run(r))
fb
)
}
import Exp._
import Video12._
enum EvalError:
case InvalidSymboName
case SymbolNotFound
case DivisionByZero
type EvalResult[A] = ReaderT[[A1] =>> Either[EvalError, A1], Env[A],A]
// Implement Numeric for EvalResult
given evalResultNumeric[A: Numeric]: Numeric[EvalResult[A]] with {
def isZero(a: EvalResult[A]): Boolean = {
a.run(Map.empty[String,A]) match {
case Right(a) if summon[Numeric[A]].isZero(a) => true
case _ => false
}
}
def add(fa: EvalResult[A], fb: EvalResult[A]): EvalResult[A] = {
fa.map2(fb)((a,b) => a + b)
}
def div(a: EvalResult[A], b: EvalResult[A]): EvalResult[A] = {
if isZero(b) then
ReaderT.lift(Left(EvalError.DivisionByZero))
else
a.map2(b)(_ / _)
}
def sub(a: EvalResult[A], b: EvalResult[A]): EvalResult[A] = {
a.map2(b)((a, b) => a - b)
}
def mul(a: EvalResult[A], b: EvalResult[A]): EvalResult[A] = {
a.map2(b)((a, b) => a * b)
}
}
enum Exp[A]:
case Val(value: A) extends Exp[A]
case Add(left: Exp[A], right: Exp[A]) extends Exp[A]
case Sub(left: Exp[A], right: Exp[A]) extends Exp[A]
case Mul(left: Exp[A], right: Exp[A]) extends Exp[A]
case Div(left: Exp[A], right: Exp[A]) extends Exp[A]
case Var(identifier: String) extends Exp[A]
type Env[A] = Map[String, A]
import Exp._
type WithEnv[A] = ReaderT[[A1] =>> Either[EvalError,A1], Env[A], A]
def summonEnv[A] : Env[A] ?=> Env[A] = summon[Env[A]]
def eval[A : Numeric](exp: Exp[A]): WithEnv[A] =
exp match
case Var(id) => handleVar(id)
case Val(value) => ReaderT.lift(Right(value))
case Add(l,r) => handleAdd(l,r)
case Sub(l,r) => handleSub(l,r)
case Div(l,r) => handleDiv(l,r)
case Mul(l,r) => handleMul(l,r)
def handleAdd[A : Numeric](l: Exp[A] , r: Exp[A] ): WithEnv[A] = eval(l) + eval(r)
def handleSub[A : Numeric](l: Exp[A] , r: Exp[A] ): WithEnv[A] = eval(l) - eval(r)
def handleMul[A : Numeric](l: Exp[A] , r: Exp[A] ): WithEnv[A] = eval(l) * eval(r)
def handleDiv[A : Numeric](l: Exp[A] , r: Exp[A] ): WithEnv[A] = eval(l) / eval(r)
def handleVar[A](s: String): WithEnv[A] =
ReaderT((env: Env[A]) =>
env.get(s) match {
case Some(value) => Right(value)
case None => Left(EvalError.SymbolNotFound)
})
// A sample expression
val exp1 : Exp[Int] = Add(
Var("z"),
Add(
Val(10),
Mul(
Var("x"),
Var("y"))))
// Provide an environment and eval the expression
{
val envMap: Env[Int] = Map("x" -> 7, "y" -> 6, "z" -> 22)
val eval1 = eval(exp1).run(envMap)
println(s"Eval exp gives $eval1")
}
// And again with a missing symbol
{
val envMap: Env[Int] = Map("x" -> 17, "y" -> 10, "a" -> 2)
val eval1 = eval(exp1).run(envMap)
println(s"Eval exp gives $eval1")
}