MPTC+FunDep: get bare value of dependent type?
This fails with Could not deduce (Foo a0 b):
class Foo a b | a -> b where
foo :: b
But this works fine:
class Bar a b | a -> b where
bar :: Const b a
It seems like bar should carry no more actual data about a than foo, and all Const does is explicitly pull the instance variable into scope. But this fails the same as Foo:
type Const' a b = a
class Baz a b | a -> b where
baz :: Const' b a
Meanwhile, this is fine:
class Qux a b | a -> b where
qux :: a
What's really going on here? Is there any way I can get an unwrapped b?
Posted Aug 2, 2018, 9:08 PM UTC
33
Well, think of it this way:
a -> bmeans the type ofbis determined bya, but if you use lets sayfoo :: Intit does not tell you anything aboutaand thus nothing about the value offoo.An example of why this is important would be this:
If you now use
a = foo :: Intisathen1or2? You cannot know without knowinga. If your associated types are injective you can also add anotherb -> afundep, which would make the second instanceFoo String Intillegal.To resolve your issue, you can use
Constor if you likeTagged(from thetaggedpackage) or you pass in a proxy or value.The
proxyapproach is the cleaner version of the last one btw. I think there is also a way to do this withTypeApplicationsbut I'm always confused as to how you supply the arguments for methods of type classes. You could tryfoo @a @bwithaandbsubstituted for your concrete type.PS: Its not called dependent type that term is reserved for a different thins entirely. Perhaps call is associated value.
Ah... so basically, the answer is that
Constrequires the caller to know the type ofa, while a type synonym does not. Makes sense!I was completely unaware that multiple fundeps were possible! They don't seem to pop up in examples anywhere. I guessed at the syntax and happily got it right on the first try (comma-separated), this is actually what I wanted in the first place!
Isn't it a form of dependent pair, though? And it's a functional dependency. :)
Nice to see you've found what you were looking for.
It is a functional dependency, but the 'dependent types' term is already taken. https://en.wikipedia.org/wiki/Dependent_type