kamatsu•

This is very cool, and I think it's the best suggestion for improvement in this area i've seen. Someone please implement in GHC.

6
Paczesiowa•

Tom Schrijvers wrote a few papers with SPJ, so it (whatever it is, still downloading video) either will be implemented or there is a good reason against implementation.

3
yatima2975•

There's also a prototype implementation on github. I haven't tried it yet, but it looks to be a preprocessor for .hs files.

1
[deleted]•

I've been using it for a while. It has a lot of bugs and is very difficult to build under GHC 6.12 due to dependency building problems.

1
hsenag•

I don't see how that follows.

1
cdsmith•

Just out of curiosity, and from a position of at least a little ignorance... is there still a justification for data families? Or is it just that the injectivity assumptions made it easier to implement (I know, for example, that data families were implemented and included in GHC long before type families were.)

3
doliorules•

It isn't uncommon for data families to be exactly what one wants. For instance, if we're making generalized tries:

data family Trie k :: * -> *
data instance Trie () a = UNil | UJust a -- newtype instance Trie () a = TUnit (Maybe a)
data instance Trie [k] a = LT (Maybe a) (Trie k (Trie [k] a))
...

It's true that the unit instance is just Maybe, so we could make due with a synonym family. However, the list case genuinely requires a new, recursive datatype. And there are other similar cases. So, if you lack data families, you'll have to have (for certain families) lots of extra data definitions scattered around whose only purpose is to be the target of type families. It's needless indirection.

I haven't thought about class families enough to know how they'd compare. The talk said that class synonym families were always the more elegant choice in their investigations. That's clearly not true for type families versus data families.

2
cdsmith•

This seems to be more of a counterexample than a supporting example. First of all, you'd clearly like for Trie () to be Maybe, but you aren't able to do so, because Trie is declared as a data family instead of a type family. In the second instance, you get to save one line declaring a new data type...

But then things get way worse. Given your type for Trie [k], we're going to need a gazillion other instances for the element types of lists -- Char, Int, and so on -- just to indicate that a "trie" of that key type is just an ordinary map. Here, it would be really nice to be able to just say, for example,

type instance Trie Char = M.Map Char
type instance Trie Int = M.Map Int

But you can't. Instead, you need to build brand new types for all of these. And all of this, apparently, is to save declaring top-level names for recursive data types that are actually interesting.

At best, I see this as a use case for some syntax for "anonymous data declarations" or some such thing, but certainly not a reason for a new kind of type-level function that guarantees injectivity by requiring that everything in its image be a brand new data declaration.

What I suspect, actually, is that there is a reason that the injectivity guarantee that this speaker referred to, but I still haven't seen what it is. Perhaps there are certain things that are forbidden for type families but allowed for data families, because that property is needed to ensure they are... defined? decidable? type inferrable?

2
doliorules•

No, we don't want to use Data.Map.Map for most things. For instance, Integer also has a new datatype, for some sort of bit trie. The Int case might look like IntMap, but for that to be a loss, you have to already assume that it exists. It does in the case of IntMap, but when it was written, the only choice was to write separate, monomorphic structures for each type. Had data families been around at the time (along some kind of collection organization based on them), what we know as IntMap could conceivably have been Map Int instead.

And then there are trees, byte strings, and every other algebraic datatype you could conceivably want to use to key a trie. Are all of these interesting, and deserving of the top-level name FooTrie (and maybe even a separate package full of monomorphic functions working on them) in addition to Trie Foo?

Injectivity is, certainly, useful as well. I think 80% of the questions about type families on haskell-cafe and elsewhere boil down to something like the following not working:

type family Foo a :: *

foo :: Foo a -> Foo a
foo x = foo x

and the reason is that Foo is not injective. You can work around this:

foo :: a -> Foo a -> Foo a
foo w x = foo w x

but it's ugly (and the number of witnesses you need can easily get out of hand).

3
edwardkmett•

Due to injectivity, data families are often exactly what you want because they let type inference run 'backwards' from the family to the arguments, whereas with a type family it can't. This makes type inference possible in a number of situations with data families where it can't be done with type families. More importantly there are some classes that can be defined with a class associated data type for which a class associated type doesn't work

class Foo a where
    data Bar a :: *
    data Baz a :: *
    quux :: Bar a -> Baz a

you can define quux using data families, but unless at least one of those is a data family, the above class can't be defined without lots of newtype noise or other dispatch tricks.

1
Paczesiowa•

why are you all so afraid of overlapping?

2
Peaker•

Because if you import an extra module, instead of getting a compilation error, you get modified semantics...

5
Paczesiowa•

with great power comes great responsibility.

can you really imagine real life situation, that there are two overlapping instances, and an import brings third one, that sits between those two?

0
Peaker•

That's not what I'm referring to.

Say you are using a broad instance for (forall a. a) -- and now you import a more specific instance...

1
Paczesiowa•

I have a really hard time thinking about real-life example of this. in fact, orphan instances should prohibit such cases.

1
yatima2975•

Ooooh, shiny indeed! Finally a nice way to solve the whole "Set is not a Functor/Monad" question.

However, am I right in thinking that this will also require a revamp/refit of a large number of basic libraries?

1
[deleted]•

The Haskell type Set is neither a functor nor a monad. The Ord constraint on Set exposes its implementation via binary trees, and binary trees are neither functors nor monads. If you want Set to be a functor, you need to hide its implementation better, not add new features to the language to further detach the meaning of classes like Functor for the math they (claim to) represent.

3
RayNbowOP•

The Haskell type Set is neither a functor nor a monad.

Not even in a subcategory of Hask?

3
[deleted]•

The diagram

 x --- F ---> xF
 |            |
 g            gF
 |            |
 v            v
 y --- F ---> yF

(which hopefully comes out monospaced) still doesn't commute when F is Set. The paper makes this "work" by adding a hidden condition on y, which (to me) seems to change the meaning of Functor away from the category theoretic one.

1
doliorules•

Set is a functor over the subcategory of Hask with types-with-an-Ord-instance as objects and compare-preserving functions as arrows (compare s t = compare (toList s) (toList t) probably works for set comparison, if it isn't already defined). Data.Set even has a function for the action on arrows, called mapMonotonic.

This is, of course, not the sort of insight the paper is promoting.

4
kamatsu•

Yes, but I believe in a backwards-compatible way. This is very exciting.

1
yatima2975•

It looks like the constraint families are backward compatible for the instances, but what would the type of a restricted mapM be, for example. Well, time to read the paper, I guess!

2
rwbarton•

Right, this seems like a issue with any backwards-compatibility. If we add an associated constraint to the Functor class, say, then clients of the Functor class methods (trivial example:

fmap' :: Functor f => (a -> b) -> (f a -> f b)
fmap' = fmap

) will no longer typecheck.

2
rwbarton•

Perhaps it was mentioned in the video, but does anyone have an example of a problem that might be solved with class families?

1
cdsmith•

If you mean constraint families (the things they are talking about mostly in this video), then here's one of their examples.

class Functor f where
    constraint Inv f a
    fmap :: (Inv f a, Inv f b) => (a -> b) -> f a -> f b

instance Functor [] where
    constraint Inv [] a = ()
    fmap = map

instance Functor Set where
    constraint Inv Set a = (Ord a)
    fmap = Set.map

In other words, this lets us declare a Functor class for "partial" functors like Set, which are restricted to operate only on types in certain classes.

1