puffnfresh / roy

Small functional language that compiles to JavaScript.
http://roy.brianmckenna.org/
MIT License
836 stars 74 forks source link

Fixed type inference of pattern variable #111

Closed taku0 closed 12 years ago

taku0 commented 12 years ago

Problem

Failed to infer types of variables in deep pattern matches.

Sample Code

data XX = X
data YY = Y XX
let yy = Y X
let idXX (x:XX) = x
match yy
  case (Y x) = idXX x

Expected

Compiled without error.

Actual

Type error: YY is not XX

Cause

in addVarsToEnv:

newEnv[v.value] = currentValue;

newEnv[v.value] should be the type of the variable but currentValue is the type of the pattern. data[p.tag.value][i] is the expected type from data constructor.

Fix

Assign data[p.tag.value][i] instead of currentValue to newEnv[v.value].