Last active
December 20, 2015 17:39
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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.
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.
I'll add a note about "cannot unify rigid type variable" or something. Probably something more comprehensible than that though :)
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
The problem occurs with sumVec - and I think the issue is unifiying the extended type with the empty record type