0

I have defined types for the Peano numbers

class Plus (n :: T) (m :: T) (r :: T) | r n -> m
instance Plus 'Zero m m
instance Plus n m r => Plus ('Succ n) m ('Succ r)

Now I find myself having two constraints Plus a b c and Plus c d e.

How can I define an addition operation on my class such that the compiler is able to derive Plus a (b + d) e?

marcosh
  • 8,780
  • 5
  • 44
  • 74
  • 1
    Maybe consider using type families rather than classes, as this will circumvent the need for constraints. – AJF May 23 '18 at 10:06
  • There is no way to write `b+d` unless you switch to type families. Type families are much more natural for these things than multi parameter type classes with functional dependencies. – chi May 23 '18 at 13:25
  • 1
    I guess you would add a new class like `class PlusAssociates a b d where associate :: Dict (Plus a b c, Plus c d e) -> exists f. Dict (Plus b d f, Plus a f e)` (use your favorite encoding of existentials); a suitable, complete collection of instances would amount to a proof that addition associates. (The compiler definitely won't derive such proofs for you.) – Daniel Wagner May 23 '18 at 13:52

0 Answers0