Skip to content

Instantly share code, notes, and snippets.

@johnpmayer
Last active December 20, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnpmayer/6170463 to your computer and use it in GitHub Desktop.
Save johnpmayer/6170463 to your computer and use it in GitHub Desktop.
Another vector library in Elm, which fails due to a suspected bug in the record type system?
[1 of 5] Compiling Vec2 ( Vec2.elm )
Type error on line 31:
{x = v.x, y = v.y}
Expected Type: a
Actual Type: {}
Type error on line 34:
origin
Expected Type: {}
Actual Type: a
make: *** [Main.html] Error 1
module Vec2 where
{- 2D Vector -}
type Vec2Ext a = { a | x : Float, y : Float }
type Vec2 = Vec2Ext {}
origin : Vec2
origin = { x = 0, y = 0 }
scaleVec : Float -> Vec2Ext a -> Vec2Ext a
scaleVec a v = { v | x <- v.x * a, y <- v.y * a }
rotVec : Float -> Vec2Ext a -> Vec2Ext a
rotVec theta v =
let newX = v.x * cos theta - v.y * sin theta
newY = v.x * sin theta + v.y * cos theta
in { v | x <- newX, y <- newY }
addVec : Vec2Ext a -> Vec2Ext b -> Vec2Ext b
addVec v1 v2 =
let newX = v1.x + v2.x
newY = v1.y + v2.y
in { v2 | x <- newX, y <- newY }
flip : (a -> b -> c) -> (b -> a -> c)
flip f a b = f b a
extractVec : Vec2Ext a -> Vec2
extractVec v = { x = v.x, y = v.y }
sumVec : [ Vec2Ext a ] -> Vec2
sumVec = foldl (flip addVec) origin
@johnpmayer
Copy link
Author

The problem occurs with sumVec - and I think the issue is unifiying the extended type with the empty record type

@evancz
Copy link

evancz commented Aug 7, 2013

flip should be built-in. Not sure though?

Actually, I think this should be a type error. I think the type of sumVec should be this:

sumVec : [ Vec2Ext a ] -> Vec2Ext a

I think the same would happen with a Haskell program that needs to unify free and bound type variables that are listed by the user.

@evancz
Copy link

evancz commented Aug 7, 2013

I think this is the same as trying to write the following type:

specificListId :: [a] -> [()]
specificListId xs = xs

Which does not work in Haskell either.

@evancz
Copy link

evancz commented Aug 7, 2013

I'll add a note about "cannot unify rigid type variable" or something. Probably something more comprehensible than that though :)

@evancz
Copy link

evancz commented Aug 7, 2013

I pushed a change that provides better error messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment