TypeFamilies vs. FunctionalDependencies & type-level recursion

Simon Peyton-Jones simonpj at microsoft.com
Wed Jun 15 12:36:46 CEST 2011

|   1. mutual dependencies:
|  	class Add x y z | x y -> z, x z -> y, y z -> x
|  I think this example can be emulated with type functions; the
|  emulation didn't work with GHC 6.10, at least. It may work now.

On this point, it doesn't *quite* work yet.  The emulation you mention is something like this this:

	type family Arg1 a2 r :: *
	type family Arg2 a1 r :: *
	class (Arg2 x (R x y) ~ y, Arg1 y (R x y) ~ x) => Add x y where
	  add :: x -> y -> R x y
	  type R x y :: *

	type instance Arg1 Double Double= Int
	type instance Arg2 Int Double = Doulbe
	instance Add Int Double Double where
	  add x y = toDouble x + y
	  type R Int Double = Double

(The example you give is a bit odd because you specified that the types of any two arguments determine the third, which probably isn't what you want for Add.  But no matter.)

What we need for this is equality superclasses.  I have done all the heavy lifting for this (see our paper "Practical aspects of compiling Haskell with System FC"), but I just need a few hours to get the payoff by switching them on.  Coming soon.


More information about the Haskell-prime mailing list