From 8e95712a161d8a4b0082ba5fe59a9f9026ba9974 Mon Sep 17 00:00:00 2001 From: "P. Oscar Boykin" Date: Sun, 14 Oct 2018 17:09:06 -1000 Subject: [PATCH] Fix a bug with type param orders (#82) * Fix a bug with type param orders * use reverseMap to save one pass --- .../main/scala/org/bykn/bosatsu/Statement.scala | 6 ++++-- .../main/scala/org/bykn/bosatsu/rankn/Infer.scala | 6 ++++++ .../org/bykn/bosatsu/rankn/RankNInferTest.scala | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/org/bykn/bosatsu/Statement.scala b/core/src/main/scala/org/bykn/bosatsu/Statement.scala index 722c0cc76..668608ad6 100644 --- a/core/src/main/scala/org/bykn/bosatsu/Statement.scala +++ b/core/src/main/scala/org/bykn/bosatsu/Statement.scala @@ -230,7 +230,8 @@ sealed abstract class TypeDefinitionStatement extends Statement { val initVars = existingVars(argsType) val initState = ((initVars.toSet, initVars.reverse), 0L) val (((_, typeVars), _), params) = buildParams(argsType).run(initState).value - val typeParams = typeVars.map { tv => + // we reverse to make sure we see in traversal order + val typeParams = typeVars.reverseMap { tv => tv.toVar match { case b@Type.Var.Bound(_) => b case unexpected => sys.error(s"unexpectedly parsed a non bound var: $unexpected") @@ -262,7 +263,8 @@ sealed abstract class TypeDefinitionStatement extends Statement { val initVars = existingVars(conArgs.toList.flatMap(_._2)) val initState = ((initVars.toSet, initVars.reverse), 0L) val (((_, typeVars), _), constructors) = constructorsS.run(initState).value - val typeParams = typeVars.map { tv => + // we reverse to make sure we see in traversal order + val typeParams = typeVars.reverseMap { tv => tv.toVar match { case b@Type.Var.Bound(_) => b case unexpected => sys.error(s"unexpectedly parsed a non bound var: $unexpected") diff --git a/core/src/main/scala/org/bykn/bosatsu/rankn/Infer.scala b/core/src/main/scala/org/bykn/bosatsu/rankn/Infer.scala index ef5ba59f3..af31c3256 100644 --- a/core/src/main/scala/org/bykn/bosatsu/rankn/Infer.scala +++ b/core/src/main/scala/org/bykn/bosatsu/rankn/Infer.scala @@ -37,6 +37,12 @@ object Infer { } + /** + * The first element of the tuple are the the bound type + * vars for this type. + * the next are the types of the args of the constructor + * the final is the defined type this creates + */ type Cons = (List[Type.Var], List[Type], Type.Const.Defined) case class Env( diff --git a/core/src/test/scala/org/bykn/bosatsu/rankn/RankNInferTest.scala b/core/src/test/scala/org/bykn/bosatsu/rankn/RankNInferTest.scala index 9efdcc4c0..c45c47cfd 100644 --- a/core/src/test/scala/org/bykn/bosatsu/rankn/RankNInferTest.scala +++ b/core/src/test/scala/org/bykn/bosatsu/rankn/RankNInferTest.scala @@ -415,6 +415,20 @@ main = Monad(optPure, optBind) """, "Monad[Opt]") } + test("def with type annotation and use the types inside") { + parseProgram("""# + +struct Pair(fst, snd) + +def fst(p: Pair[a, b]) -> a: + match p: + Pair(f, _): + f + +main = fst(Pair(1, "1")) +""", "Int") + } + test("test that we see some ill typed programs") { parseProgramIllTyped("""#