Search code examples
purescript

What does the pipe character (|) mean when part of a purescript type signature?


I've been unable to figure this out exactly from reading the available docs.

On the record section of the Type docs, it seems to have to do with Row Polymorphism, but I don't understand it's general usage. What does it mean when there is a type signature with a | symbol?

For example:

class Monad m <= MonadTell w m | m -> w where
  tell :: w -> m Unit

Solution

  • The pipe in PureScript is not used "generally". There are multiple uses of it depending on the context. One, as you mentioned, is for type row combinations. Another is for function guards.

    The specific syntax you're quoting is called "functional dependency". It is a property of a multi-parameter type class, and it specifies that some variables must be unambiguously determined by others.

    In this particular case, the syntax means "for every m there can be only one w". Or, in plainer language, a given m cannot be MonadTell for several different ws.

    Functional dependencies show up in many other places. For example:

    -- For every type `a` there is only one generic representation `rep`
    class Generic a rep | a -> rep where
    
    -- Every newtype `t` wraps only one unique inner type `a`
    class Newtype t a | t -> a where