Skip to content

Commit

Permalink
Fix a bug with type param orders (#82)
Browse files Browse the repository at this point in the history
* Fix a bug with type param orders

* use reverseMap to save one pass
  • Loading branch information
johnynek committed Oct 15, 2018
1 parent 858324e commit 8e95712
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/main/scala/org/bykn/bosatsu/Statement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/org/bykn/bosatsu/rankn/Infer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/scala/org/bykn/bosatsu/rankn/RankNInferTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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("""#
Expand Down

0 comments on commit 8e95712

Please sign in to comment.