Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve 1164 #1166

Merged
merged 5 commits into from Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -327,6 +327,9 @@ object TypedExprNormalization {
}

f1 match {
// TODO: what if f1: Generic(_, AnnotatedLambda(_, _, _))
// we should still be able ton convert this to a let by
// instantiating to the right args
case AnnotatedLambda(lamArgs, expr, _) =>
// (y -> z)(x) = let y = x in z
val lets = lamArgs.zip(args).map { case ((n, ltpe), arg) =>
Expand Down
64 changes: 57 additions & 7 deletions core/src/main/scala/org/bykn/bosatsu/rankn/Infer.scala
Expand Up @@ -428,9 +428,7 @@ object Infer {

private val checkedKinds: Infer[Type => Option[Kind]] = {
val emptyRegion = Region(0, 0)
GetEnv.map { env =>
tpe => env.getKind(tpe, emptyRegion).toOption
}
GetEnv.map(env => tpe => env.getKind(tpe, emptyRegion).toOption)
}

// on t[a] we know t: k -> *, what is the variance
Expand Down Expand Up @@ -561,10 +559,10 @@ object Infer {
* with what they point to
*/
def zonkType(t: Type): Infer[Type] =
Type.zonkMeta(t)(zonk(_))
Type.zonkMeta(t)(zonk)

def zonkTypedExpr[A](e: TypedExpr[A]): Infer[TypedExpr[A]] =
TypedExpr.zonkMeta(e)(zonk(_))
TypedExpr.zonkMeta(e)(zonk)

val zonkTypeExprK
: FunctionK[TypedExpr.Rho, Lambda[x => Infer[TypedExpr[x]]]] =
Expand Down Expand Up @@ -1529,8 +1527,60 @@ object Infer {
expect match {
case Expected.Check((rho, reg)) =>
checkApply(fn, args, tag, rho, reg)
case inf =>
applyRhoExpect(fn, args, tag, inf)
case inf @ Expected.Inf(_) =>
(maybeSimple(fn), args.traverse(maybeSimple(_)))
.mapN { (infFn, infArgs) =>
infFn.flatMap { fnTe =>
fnTe.getType match {
case Type.Fun.SimpleUniversal(us, argsT, resT)
if argsT.length == args.length =>
infArgs.sequence
.flatMap { argsTE =>
val argTypes = argsTE.map(_.getType)
Type.instantiate(
us.toList.toMap,
Type.Tuple(argsT.toList),
Type.Tuple(argTypes.toList)
) match {
case None =>
pureNone
case Some((frees, inst)) =>
if (frees.nonEmpty) {
// TODO maybe we could handle this, but not yet
pureNone
} else {
val subMap = inst.view.mapValues(_._2).toMap[Type.Var, Type]
val fnType0 = Type.Fun(argsT, resT)
val fnType1 = Type.substituteVar(fnType0, subMap)
val resType = Type.substituteVar(resT, subMap)
val resTe = TypedExpr.App(
TypedExpr.Annotation(fnTe, fnType1),
argsTE,
resType,
term.tag
)

instSigma(
resType,
inf,
HasRegion.region(term)
)
.map(co => Some(co(resTe)))
}
}
}
case _ =>
pureNone
}
}
}
.flatSequence
.flatMap {
case Some(te) => pure(te)
case None =>
applyRhoExpect(fn, args, tag, inf)
}

}
case Generic(tpes, in) =>
for {
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/org/bykn/bosatsu/TypedExprTest.scala
Expand Up @@ -605,7 +605,7 @@ res = (
checkLast("""
res = _ -> 1
""") { te2 =>
assert(te1.void == te2.void, s"${te1.repr} != ${te2.repr}")
assert(te1.void == te2.void, s"${te1.repr.render(80)} != ${te2.repr.render(80)}")
}
}

Expand Down
28 changes: 27 additions & 1 deletion core/src/test/scala/org/bykn/bosatsu/rankn/RankNInferTest.scala
Expand Up @@ -201,7 +201,7 @@ class RankNInferTest extends AnyFunSuite {
)

assert(Type.metaTvs(tp :: Nil).isEmpty, s"illegal inferred type: $teStr")
assert(te.getType.sameAs(typeFrom(tpe)), s"found: ${te.repr}")
assert(te.getType.sameAs(typeFrom(tpe)), s"found: ${te.repr.render(80)}")
}

// this could be used to test the string representation of expressions
Expand Down Expand Up @@ -1576,6 +1576,17 @@ x = hide(y)
"""#
struct Tup(a, b)

def hide[b](x: b) -> exists a. a: x
x = hide(1)
y = hide("1")
z: Tup[exists a. a, exists b. b] = Tup(x, y)
""",
"Tup[exists a. a, exists b. b]"
)
parseProgram(
"""#
struct Tup(a, b)

def hide[b](x: b) -> exists a. a: x
def makeTup[a, b](x: a, y: b) -> Tup[a, b]: Tup(x, y)
x = hide(1)
Expand Down Expand Up @@ -1835,4 +1846,19 @@ f3: Foo[forall a. a] = f1
"Foo[forall a. a]"
)
}

test("test Liskov example") {
parseProgram(
"""
struct Sub[a: -*, b: +*](sub: forall f: +* -> *. f[a] -> f[b])
struct Tup(a, b)

refl_sub: forall a. Sub[a, a] = Sub(x -> x)
refl_any: Sub[forall a. a, exists a. a] = refl_sub
#ignore: Tup[forall a. Sub[a, a], Sub[forall a. a, exists a. a]] = Tup(refl_sub, refl_any)
ignore = Tup(refl_sub, refl_any)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more tests here, looks like we haven't solved it yet.

""",
"Tup[forall a. Sub[a, a], Sub[forall a. a, exists a. a]]"
)
}
}
2 changes: 1 addition & 1 deletion test_workspace/TypeConstraint.bosatsu
Expand Up @@ -77,4 +77,4 @@ refl_bottom1: Sub[forall a. a, forall a. a] = refl_sub
refl_Int: Sub[forall a. a, Int] = refl_sub
refl_any: Sub[forall a. a, exists a. a] = refl_sub
refl_any1: Sub[exists a. a, exists a. a] = refl_sub
refl_Int_any: Sub[Int, exists a. a] = refl_sub
refl_Int_any: Sub[Int, exists a. a] = refl_sub