
Hi folks, Once in a while I stumble upon declarations like this: class EditAction e a | e -> a where apply :: a -> e -> a Why is the vertical bar there? I understand that => defines a context, but can't find what is the "|" for. Thanks Rafael Gustavo da Cunha Pereira Pinto

On Fri, Jul 24, 2009 at 09:41:34AM -0300, Rafael Gustavo da Cunha Pereira Pinto wrote:
Hi folks,
Once in a while I stumble upon declarations like this:
class EditAction e a | e -> a where apply :: a -> e -> a
Why is the vertical bar there? I understand that => defines a context, but can't find what is the "|" for.
It's a functional dependency: the e -> a means that the type e determines the type a, that is, there can only be one instance of EditAction for any particular type e. -Brent

On Fri, Jul 24, 2009 at 08:44:32AM -0400, Brent Yorgey wrote:
On Fri, Jul 24, 2009 at 09:41:34AM -0300, Rafael Gustavo da Cunha Pereira Pinto wrote:
class EditAction e a | e -> a where apply :: a -> e -> a
It's a functional dependency: the e -> a means that the type e determines the type a, that is, there can only be one instance of EditAction for any particular type e.
Alternatively you could write that class using a type family: class EditAction e where type Text e :: * apply :: Text e -> e -> Text e (I don't know if "Text e" is a good name, but Haskell-wise this should do the same trick as the fundep class.) One example of an instance (out of the top of my head): data Editor = Editor {text :: String, ...} data EditorAction = Prepend String | ... instance EditAction EditorAction where type Text EditorAction = Editor apply editor (Prepend s) = editor {text = s ++ text editor} HTH, -- Felipe.

So, it is a language extension as I predicted. A cool one, by the way.
Thank you all!
On Fri, Jul 24, 2009 at 09:44, Brent Yorgey
On Fri, Jul 24, 2009 at 09:41:34AM -0300, Rafael Gustavo da Cunha Pereira Pinto wrote:
Hi folks,
Once in a while I stumble upon declarations like this:
class EditAction e a | e -> a where apply :: a -> e -> a
Why is the vertical bar there? I understand that => defines a context, but can't find what is the "|" for.
It's a functional dependency: the e -> a means that the type e determines the type a, that is, there can only be one instance of EditAction for any particular type e.
-Brent _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc.
participants (3)
-
Brent Yorgey
-
Felipe Lessa
-
Rafael Gustavo da Cunha Pereira Pinto