[Haskell-cafe] Identity operator for types

Jack Kelly jack at jackkelly.name
Fri May 17 11:01:57 UTC 2019


Juan Miguel Vilar <jvilar at uji.es> writes:

> El 17/5/19 a las 11:56, ALeX Kazik escribió:
>>> Now, the problem is what can I do to have a type that contains a
>>> single Int? Is there a sort of id for types so that I can write
>>
>> Identity is the solution
>>
> Thank you Alex, but this forces the use of runIdentity. [...]
> Sometimes they will use lists, sometimes simple times, and would like
> that to be as transparent as possible.

I do not like the sibling thread's suggestion of using closed type
families, because I think the regulatity of always having the `f t`
present is a nicer aesthetic. Using a type family to unwrap the
`Identity` makes it noisier to write functions that consume your
structure and are polymorphic in `f`.

You can alleviate some of the syntactic noise of `runIdentity` with
careful choices of type aliases or other helper functions. The type
aliases (and `runFoo` functions) for monad transformers are good
examples, and the `(==>)` function in dependent-sum[1] is a good
additional example of the latter. To save you the click, DSum in
dependent-sum is defined like this:

    data DSum tag f where
      !(tag a) :=> f a

The (==>) helper is defined like this:

    (==>) :: Applicative f => tag a -> a -> DSum tag f
    tag ==> a = tag :=> f a

So when f ~ Identity (or any other Applicative), the machinery becomes
that much easier to use.

-- Jack

[1]: https://hackage.haskell.org/package/dependent-sum-0.5/docs/Data-Dependent-Sum.html


More information about the Haskell-Cafe mailing list