I'm having problems with PureScript classes. I have to say ahead that I am not a Haskell expert, so I apologize if these are obvious errors.
I tried several different approaches and hit a wall for each. I am basically trying to define the show function for an edge in a graph. One approach is as follows:
module Foo where data Edge n = Edge { from :: n, to :: n } instance showEdge :: (Show n) => Show (Edge n) where show e = "Edge from "++(show e.from)++" to "++(show e.to) e = Edge { from: 1, to: 2 } main = show e
This gives me an error:
$ psc src/Attempt1.purs Error at src/Attempt1.purs line 6, column 27: Error in declaration showEdge Cannot unify Prim.Object with Foo.Edge.
I assume this is due to the fact that it outputs the type e to the show definition. I tried adding type annotation here, but I get a syntax error:
module Foo where data Edge n = Edge { from :: n, to :: n } instance showEdge :: (Show n) => Show (Edge n) where show e :: Edge n show e = "Edge from "++(show e.from)++" to "++(show e.to) e = Edge { from: 1, to: 2 } main = show e
The second thing I tried was the following:
module Foo where type Edge n = { from :: n, to :: n } instance showEdge :: (Show n) => Show (Edge n) where show e = "Edge from "++(show e.from)++" to "++(show e.to) e :: Edge Number e = { from: 1, to: 2 } main = show e
This gives me:
$ psc src/Attempt2.purs Error at src/Attempt2.purs line 5, column 1: Type synonym instances are disallowed
So, I then tried to explicitly specify the base type:
module Foo where type Edge n = { from :: n, to :: n } instance showEdge :: (Show n) => Show { from :: n, to :: n } where show e = "Edge from "++(show e.from)++" to "++(show e.to) e :: Edge Number e = { from: 1, to: 2 } main = show e
which gives me:
$ psc src/Attempt3.purs Error at src/Attempt3.purs line 5, column 1: Error in type (to :: n, from :: n): Type class instance head is invalid.
I have no idea what the โinstance type of the instance classโ is, so I have nowhere to go from there.
All three attempts failed. Probably for completely different reasons. Being new to PureScript, I just don't know what the problem is. I tried to follow the example of various types of Data.* And reading PureScript as an example. I could not figure it out.
Thanks for any help.