|
| 1 | +package effekt |
| 2 | +package core |
| 3 | + |
| 4 | + |
| 5 | +abstract class AbstractMonoTests extends CorePhaseTests(Mono) { |
| 6 | + import TypeArg.* |
| 7 | + |
| 8 | + implicit def stringBaseT(name: String): Base = Base(Id(name)) |
| 9 | + |
| 10 | + val BaseTInt: Base = "Int" |
| 11 | + val BaseTString: Base = "String" |
| 12 | + val BaseTChar: Base = "Char" |
| 13 | + val BaseTBool: Base = "Bool" |
| 14 | + val BaseTDouble: Base = "Double" |
| 15 | + |
| 16 | + val fnId: Map[String, FunctionId] = Map( |
| 17 | + "a" -> Id("a"), |
| 18 | + "b" -> Id("b"), |
| 19 | + "c" -> Id("c"), |
| 20 | + "d" -> Id("d"), |
| 21 | + "e" -> Id("e"), |
| 22 | + "f" -> Id("f"), |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +class MonoTests extends AbstractMonoTests { |
| 27 | + |
| 28 | + import TypeArg.* |
| 29 | + |
| 30 | + test("simple polymorphic function") { |
| 31 | + val constraints = List( |
| 32 | + Constraint(Vector(BaseTInt), fnId("a")), |
| 33 | + Constraint(Vector(BaseTString), fnId("a")) |
| 34 | + ) |
| 35 | + val expectedSolved: Solution = Map( |
| 36 | + fnId("a") -> Set(Vector(BaseTInt), Vector(BaseTString)) |
| 37 | + ) |
| 38 | + |
| 39 | + assertEquals(solveConstraints(constraints), expectedSolved) |
| 40 | + } |
| 41 | + |
| 42 | + test("calling other polymorphic function") { |
| 43 | + val constraints = List( |
| 44 | + Constraint(Vector(Var(fnId("b"), 0)), fnId("a")), |
| 45 | + Constraint(Vector(BaseTInt), fnId("a")), |
| 46 | + Constraint(Vector(BaseTString), fnId("b")), |
| 47 | + ) |
| 48 | + val expectedSolved: Solution = Map( |
| 49 | + fnId("a") -> Set(Vector(BaseTInt), Vector(BaseTString)), |
| 50 | + fnId("b") -> Set(Vector(BaseTString)), |
| 51 | + ) |
| 52 | + |
| 53 | + assertEquals(solveConstraints(constraints), expectedSolved) |
| 54 | + } |
| 55 | + |
| 56 | + test("polymorphic function with multiple type args") { |
| 57 | + val constraints = List( |
| 58 | + Constraint(Vector(BaseTInt, BaseTString), fnId("a")), |
| 59 | + Constraint(Vector(BaseTBool, BaseTChar), fnId("a")), |
| 60 | + Constraint(Vector(BaseTBool, BaseTString), fnId("a")), |
| 61 | + ) |
| 62 | + val expectedSolved: Solution = Map( |
| 63 | + fnId("a") -> Set( |
| 64 | + Vector(BaseTInt, BaseTString), |
| 65 | + Vector(BaseTBool, BaseTChar), |
| 66 | + Vector(BaseTBool, BaseTString), |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + assertEquals(solveConstraints(constraints), expectedSolved) |
| 71 | + } |
| 72 | + |
| 73 | + test("calling other polymorphic function with type args swapped") { |
| 74 | + val constraints = List( |
| 75 | + Constraint(Vector(Var(fnId("b"), 1), Var(fnId("b"), 0)), fnId("a")), |
| 76 | + Constraint(Vector(BaseTString, BaseTBool), fnId("b")), |
| 77 | + ) |
| 78 | + val expectedSolved: Solution = Map( |
| 79 | + fnId("a") -> Set(Vector(BaseTBool, BaseTString)), |
| 80 | + fnId("b") -> Set(Vector(BaseTString, BaseTBool)), |
| 81 | + ) |
| 82 | + |
| 83 | + assertEquals(solveConstraints(constraints), expectedSolved) |
| 84 | + } |
| 85 | + |
| 86 | + test("recursive polymorphic function") { |
| 87 | + val constraints = List( |
| 88 | + Constraint(Vector(Var(fnId("a"), 0)), fnId("a")), |
| 89 | + Constraint(Vector(BaseTInt), fnId("a")), |
| 90 | + ) |
| 91 | + val expectedSolved: Solution = Map( |
| 92 | + fnId("a") -> Set(Vector(BaseTInt)), |
| 93 | + ) |
| 94 | + |
| 95 | + assertEquals(solveConstraints(constraints), expectedSolved) |
| 96 | + } |
| 97 | + |
| 98 | + test("mutually recursive polymorphic functions") { |
| 99 | + val constraints = List( |
| 100 | + Constraint(Vector(Var(fnId("b"), 0)), fnId("a")), |
| 101 | + Constraint(Vector(Var(fnId("a"), 0)), fnId("b")), |
| 102 | + Constraint(Vector(BaseTInt), fnId("a")), |
| 103 | + Constraint(Vector(BaseTString), fnId("b")), |
| 104 | + ) |
| 105 | + val expectedSolved: Solution = Map( |
| 106 | + fnId("a") -> Set(Vector(BaseTInt), Vector(BaseTString)), |
| 107 | + fnId("b") -> Set(Vector(BaseTInt), Vector(BaseTString)), |
| 108 | + ) |
| 109 | + |
| 110 | + assertEquals(solveConstraints(constraints), expectedSolved) |
| 111 | + } |
| 112 | +} |
0 commit comments