Skip to content

Commit

Permalink
Further changes for BlockType Wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
MattisKra committed Jun 26, 2023
1 parent 52c6ca2 commit 9fec468
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 14 deletions.
6 changes: 3 additions & 3 deletions effekt/shared/src/main/scala/effekt/core/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
case v: source.Var =>
val sym = v.definition
Context.blockTypeOf(sym) match {
case BlockTypeRef(x : BlockTypeWildcard) => Context.panic("Wildcard in unexpected place")
case BlockTypeRef(x) => Context.panic("Wildcard in unexpected place")
case _: BlockType.FunctionType => transformAsControlBlock(tree)
case _: BlockType.InterfaceType => transformAsObject(tree)
}
Expand Down Expand Up @@ -218,7 +218,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
val sym = v.definition
val tpe = Context.blockTypeOf(sym)
tpe match {
case BlockTypeRef(x : BlockTypeWildcard) => Context.abort("Wildcard in unexpected place")
case BlockTypeRef(x) => Context.panic("Wildcard in unexpected place")
case BlockType.FunctionType(tparams, cparams, vparamtps, bparamtps, restpe, effects) =>
// if this block argument expects to be called using PureApp or DirectApp, make sure it is
// by wrapping it in a BlockLit
Expand Down Expand Up @@ -525,7 +525,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
}

def transform(tpe: BlockType)(using Context): core.BlockType = tpe match {
case BlockTypeRef(x : BlockTypeWildcard) => Context.panic("Wildcard in unexpected place")
case BlockTypeRef(x) => Context.panic("Wildcard in unexpected place")
case BlockType.FunctionType(tparams, cparams, vparams, bparams, result, effects) =>

val capabilityTypes = effects.canonical.map(transform)
Expand Down
6 changes: 4 additions & 2 deletions effekt/shared/src/main/scala/effekt/typer/Constraints.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ class Constraints(
}

y match {
case BlockTypeRef(y: BlockUnificationVar) => connectNodes(getNode(x), getNode(y))
case tpe => learnType(getNode(x), tpe)
case BlockTypeRef(y: BlockUnificationVar) =>
connectNodes(getNode(x), getNode(y))
case tpe =>
learnType(getNode(x), tpe)
}
}

Expand Down
10 changes: 9 additions & 1 deletion effekt/shared/src/main/scala/effekt/typer/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ trait TypeUnifier {
val unificationVar : UnificationVar = unificationVarFromWildcard(w)
requireEqual(unificationVar, tpe1, ctx)

case (ValueTypeRef(w: ValueTypeWildcard), ValueTypeApp(t2, args2), _) =>
val unificationVar : UnificationVar = unificationVarFromWildcard(w)
requireEqual(unificationVar, tpe2, ctx)

// For now, we treat all type constructors as invariant.
case (ValueTypeApp(t1, args1), ValueTypeApp(t2, args2), _) =>

if (t1 != t2) { error(tpe1, tpe2, ErrorContext.TypeConstructor(ctx)) }
if (t1 != t2) {
error(tpe1, tpe2, ErrorContext.TypeConstructor(ctx))
}

if (args1.size != args2.size)
abort(pp"Argument count does not match $t1 vs. $t2", ctx) // TODO add to context
Expand All @@ -81,6 +87,8 @@ trait TypeUnifier {
def unifyBlockTypes(tpe1: BlockType, tpe2: BlockType, ctx: ErrorContext): Unit = (tpe1, tpe2) match {
case (t, BlockTypeRef(x : BlockTypeWildcard)) =>
requireEqual(unificationVarFromWildcard(x), t, ctx)
case (BlockTypeRef(x : BlockTypeWildcard), t) =>
requireEqual(unificationVarFromWildcard(x), t, ctx)
case (t: FunctionType, s: FunctionType) => unifyFunctionTypes(t, s, ctx)
case (t: InterfaceType, s: InterfaceType) => unifyInterfaceTypes(t, s, ctx)
case (t, s) => error(t, s, ctx)
Expand Down
19 changes: 13 additions & 6 deletions examples/pts/blockType.effekt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module examples/pts/blockType

def main() = {
var num : _ = 1
num = 2
println(num)
module examples/pts/blockType

def runFunc{func : _} = {
func("abc", 1)
}

def main() = {
def test(x : String, y : Int) : String = {
"a"
}

val res : Int = runFunc { test }
println(res)
}
18 changes: 18 additions & 0 deletions examples/pts/blockTypes.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module examples/pts/blockTypes

def main() = {
def test1(x : String, y : Int) : String = {
"a"
}

def test2(x : String, y : String) : String = {
"a turtle"
}

val res : Int = runFunc { test1 } { test2 }
println(res)
}

def runFunc{func1 : _}{func2 : _} = {
func2(func1("abc", 1), " turtle")
}
12 changes: 12 additions & 0 deletions examples/pts/functionParameterTypes.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module examples/pts/functionParameterTypes

def constant(num : _) = {
val a : Test = num
return 5
}

def main() = {
val input : Int = 2
val res : Int = constant(input)
println(res)
}
16 changes: 16 additions & 0 deletions examples/pts/longType.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module examples/pts/longType


type List[A] {
Nil();
Cons(head: A, tail: List[A])
}

type TypeWithReallyLongName {
X(y : Int)
}

def main() = {
val listA : List[_] = Cons(X(1), Cons(X(2), Cons(X(3), Nil())))
println(listA)
}
19 changes: 19 additions & 0 deletions examples/pts/nestedTypes.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module examples/pts/nestedTypes

type List[A] {
Nil();
Cons(head: A, tail: List[A])
}

type Map[A, B] {
Map(keys : List[A], values : List[B])
}

def main() = {
val listA : List[_] = Cons(1, Cons(2, Cons(3, Nil())))
val listB : List[_] = Cons("a", Cons("b", Cons("c", Nil())))

val map1 : Map[_, _] = Map(listA, listB)
val map2 : Map[String, Int] = map1
println("end")
}
5 changes: 3 additions & 2 deletions examples/pts/valueType.effekt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module examples/pts/valueType

def main() = {
val num : Int = 1;
println(num);
val num1 : _ = 1
val num2 : String = num1
println(num1)
}

0 comments on commit 9fec468

Please sign in to comment.