Skip to content

Commit 960ea3f

Browse files
committed
Fix hasCycle implementation
1 parent 239336f commit 960ea3f

File tree

1 file changed

+24
-21
lines changed
  • effekt/shared/src/main/scala/effekt/core

1 file changed

+24
-21
lines changed

effekt/shared/src/main/scala/effekt/core/Mono.scala

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ object Mono extends Phase[CoreTransformed, CoreTransformed] {
1717
val polys = findConstraints(definitions)
1818
polys.foreach(p => println(p))
1919
println()
20+
println("Has cycle: " + hasCycle(polys))
21+
println()
2022
}
2123
}
2224
}
@@ -30,8 +32,13 @@ object Mono extends Phase[CoreTransformed, CoreTransformed] {
3032
// so we can generate the required monomorphic functions
3133

3234
enum PolyType {
33-
case Var(val sym: symbols.Symbol)
3435
case Base(val tpe: symbols.Symbol)
36+
case Var(val sym: symbols.Symbol)
37+
38+
def toSymbol: symbols.Symbol = this match {
39+
case Base(tpe) => tpe
40+
case Var(sym) => sym
41+
}
3542
}
3643

3744
type PolyConstraints = Map[symbols.Symbol, Set[PolyType]]
@@ -94,29 +101,25 @@ def findConstraints(definitions: List[Toplevel]): PolyConstraints =
94101
}
95102
typeFlow
96103

97-
object PolyGraphCalc {
98-
var visited: Set[symbols.Symbol] = Set()
99-
var recStack: Set[symbols.Symbol] = Set()
104+
def hasCycle(constraints: PolyConstraints): Boolean =
105+
var visited: Set[symbols.Symbol] = Set()
106+
var recStack: Set[symbols.Symbol] = Set()
100107

101-
def hasCycle(vertex: symbols.Symbol, adjacency: PolyConstraints): Boolean =
102-
if (recStack.contains(vertex)) return true
108+
def hasCycleHelper(vertex: symbols.Symbol): Boolean =
109+
if (recStack.contains(vertex)) return true
110+
if (visited.contains(vertex)) return false
103111

104-
if (visited.contains(vertex)) return false
112+
visited += vertex
113+
recStack += vertex
105114

106-
visited += vertex
107-
recStack += vertex
115+
var cycleFound = false
116+
constraints.getOrElse(vertex, Set()).foreach(v => cycleFound |= hasCycleHelper(v.toSymbol))
108117

109-
adjacency.foreach((v, edges) => if (hasCycle(v, adjacency)) return true)
118+
recStack -= vertex
110119

111-
recStack -= vertex
112-
false
113-
114-
def hasCycle(constraints: PolyConstraints): Boolean =
115-
visited = Set()
116-
recStack = Set()
117-
118-
constraints.keys.foreach(v => if (hasCycle(v, constraints)) return true)
119-
120-
false
121-
}
120+
cycleFound
121+
122+
var cycleFound = false
123+
constraints.keys.foreach(v => cycleFound |= !visited.contains(v) && hasCycleHelper(v))
122124

125+
cycleFound

0 commit comments

Comments
 (0)