[Haskell-cafe] "SameConstraints?" type constraints
emax at chalmers.se
Mon Jun 22 07:45:13 UTC 2015
Den 2015-06-22 09:26, Emil Axelsson skrev:
> Den 2015-06-19 22:13, Leza Morais Lutonda skrev:
>> On 06/19/2015 08:44 AM, Sean Leather wrote:
>>> Note this won't work for a Num instance mentioned earlier because the
>>> existentially quantified d types in two SC values are not provably the
>>> same type. In other words, you can't write
>>> instance Num e => Show (S Num e) where
>>> SC x1 y1 + SC x2 y2 = SC (x1 + x2) (y1 + y2)
>>> because x1 and x2 can have different types.
>> Another issue is: what if I want to constraint the type `e` to more
>> classes and make `d` to have the same constrains? I have to re-declare
>> the `S` data type like?:
>> data S c1 c2 ... cN e where
>> SC :: (c1 d, c2 d, ..., cN d) -> d -> e -> S c1 c2 ... cN e
>> Does anyone ever needed such a feature?
> Yes, have a look at the paper "Deconstraing DSLs":
> See also the constraint product (&&&) from the `constraints` package.
Or you might find this interface easier to work with:
It's based on type predicates (`* -> Constraint`), so you can write e.g.
`(c1 /\ c2 /\ ... /\ cN) d => ...`. It also implements "sub-classing"
using a technique similar to Data Types à la Carte.
More information about the Haskell-Cafe