-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Selectable.scala
74 lines (64 loc) · 2.26 KB
/
Selectable.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
/*
* Copyright 2024 fs2-data Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fs2.data.pattern
import cats.Show
/** Describes the structure of an expression in term of constructor
* trees that can be selected.
*/
trait Selectable[Expr, Tag] {
def tree(e: Expr): ConstructorTree[Tag]
def select[GExpr](e: Expr, sel: Selector[GExpr, Tag])(implicit evaluator: Evaluator[GExpr, Tag]): Option[Tag] =
tree(e).select(sel).map(_.tag)
}
trait Evaluator[Expr, Tag] {
def eval(guard: Expr, tree: ConstructorTree[Tag]): Option[Tag]
}
object Evaluator {
implicit def noop[Tag]: Evaluator[NoGuard, Tag] =
new Evaluator[NoGuard, Tag] {
override def eval(guard: NoGuard, tree: ConstructorTree[Tag]): Option[Tag] = None
}
}
/** A sealed trait with no implementation so that it has no inhabitant.
* To be used when a pattern language has no guards.
*/
sealed trait NoGuard
object NoGuard {
implicit val show: Show[NoGuard] = Show.show(_ => "")
}
object Selectable {
def apply[Expr, Tag](implicit ev: Selectable[Expr, Tag]): Selectable[Expr, Tag] =
ev
}
case class ConstructorTree[Tag](tag: Tag, args: List[ConstructorTree[Tag]]) {
def select[Expr](sel: Selector[Expr, Tag])(implicit evaluator: Evaluator[Expr, Tag]): Option[ConstructorTree[Tag]] =
sel match {
case Selector.Root() => Some(this)
case Selector.Cons(parent, tag, n) =>
select(parent).flatMap { const =>
if (const.tag == tag)
const.args.lift(n)
else
None
}
case Selector.Guard(parent, guard) =>
select(parent).flatMap(evaluator.eval(guard, _).map(ConstructorTree(_, Nil)))
}
}
object ConstructorTree {
def noArgConstructor[Tag](tag: Tag): ConstructorTree[Tag] =
ConstructorTree(tag, Nil)
}