Skip to content

Instantly share code, notes, and snippets.

@lisael
Created September 12, 2017 01:29
Show Gist options
  • Save lisael/d7637678c2b53ceffb80e823a5dde8b4 to your computer and use it in GitHub Desktop.
Save lisael/d7637678c2b53ceffb80e823a5dde8b4 to your computer and use it in GitHub Desktop.
strange compilator error...
type alias Item a = { a | id: Int, path: List Int }
type Tree a
= Empty
| Node (Item a) (List (Tree a))
insert_: Item a -> Tree a -> Tree a
insert_ item tree =
case tree of
Empty ->
Node item []
Node i lst ->
let
newNode = Node item []
in
if List.member i.id item.path then
case nodeParentId newNode of
Nothing ->
tree
Just id ->
if id == i.id then
Node i ( lst ++ [newNode] )
else
Node i ( List.map (insert_ item) lst )
else
tree
insert t it =
insert_ it t
nodeId: Tree a -> Maybe Int
nodeId n =
case n of
Empty ->
Nothing
Node item _ ->
Just item.id
nodeParentId: Tree a -> Maybe Int
nodeParentId tree =
case tree of
Empty ->
Nothing
Node item _ ->
List.reverse item.path
|> List.drop 1
|> List.head
insertAll: Tree a -> (List (Item a)) -> Tree a
insertAll tree list =
case List.head list of
Nothing ->
tree
Just item ->
let
tree_ = insert_ item tree
in
case List.tail list of
Nothing ->
tree_
Just list_ ->
insertAll tree_ list_
type alias Foo
= {id: Int, path: (List Int), name: String}
insertFoo: Tree Foo -> Foo -> Tree Foo
insertFoo t f =
insert t f
insertFooRev: Foo -> Tree Foo -> Tree Foo
insertFooRev f t =
insert_ f t
@lisael
Copy link
Author

lisael commented Sep 12, 2017

If insertFooRev is commented out, all compiles correctly.

The first implementation of insert was this of insert_ because I wanted to map a partial function at line 25.

insert_ is a dirty workaround to be able to use the lib.

Did I do anything wrong ? Did I run into a bug in elm ?

-- TYPE MISMATCH -------------------------------------------------- ././Tree.elm

The definition of `insertFooRev` does not match its type annotation.

74| insertFooRev: Foo -> Tree Foo -> Tree Foo
75| insertFooRev f t =
76|>  insert_ f t

The type annotation for `insertFooRev` says it always returns:

    Tree Foo

But the returned value (shown above) is a:

    Tree { name : String }

Hint: Looks like a record is missing these fields: id and path

-- TYPE MISMATCH -------------------------------------------------- ././Tree.elm

The 2nd argument to function `insert_` is causing a mismatch.

76|   insert_ f t
                ^
Function `insert_` is expecting the 2nd argument to be:

    Tree { name : String }

But it is:

    Tree Foo

Hint: Looks like a record is missing these fields: id and path

Hint: I always figure out the type of arguments from left to right. If an
argument is acceptable when I check it, I assume it is "correct" in subsequent
checks. So the problem may actually be in how previous arguments interact with
the 2nd.

Detected errors in 1 module.                                        
make: *** [Makefile:91: /home/lisael/projects/postgrest/tutorial/spa/static/js/main.js] Error 1

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