-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
It is unclear how to use the Map typeclass #312
Comments
TBH, I think we should scrap this approach to 'Map/Functor' and do something more like vanilla Haskell. The current implementation came out of the experience we got building programs with structural types where we were very enthusiastic about the additional power that structural reasoning gave us. This went a little bit too far in the case of 'Map' where we basically had to infer an intended nominal type from a structural one, and that's not possible in general. Rather than distinguishing certain structural patterns like this, we should just overload on type constructors in the usual way (and we already have a way to map type constructors with arguments into structural types if we need that information for something else). That will make these puzzles much less difficult to solve and more straightforward to reason about the error messages that come up. We still need to be a little bit more general than in Haskell, because for example if you map a function over a stored sequence, you don't want the result (necessarily) to itself be a stored sequence. PS: I'm curious to know if you've looked at the structural type constraints, they will let you avoid a lot of boilerplate you might otherwise have to handle with something like Template Haskell (and IMHO e.g. fix the awkwardness of record types in Haskell). |
FWIW, one way to recover something like Haskell's
So that e.g.:
It wouldn't be a lot of work to refactor things this way, but as described earlier this approach has some problems as in e.g. the case of querying out of structured data files (where we don't want the result structure to itself be a stored type, though the source structure must be). But it's enough to define the class you want:
So that:
HTH |
Thanks, this helps in understanding what is going on. The structural typing is very nice, as you say it gets rid of the need of a lot of boilerplate, although I'm still not totally certain that this ease isn't going to bite me later. |
If it helps (in case the point wasn't made clearly elsewhere), the typical win for structural types is in writing explicit instance generators where you'd otherwise rely on implicit magic with "deriving". For example, when you make custom record types in Haskell you might rely on "deriving Eq" to get equality testing. But you can generically deconstruct record types this way to make the obvious definition:
So that two instances of a record are equal if all of their fields are equal. It's the same thing you'd get with "deriving Eq" in Haskell but now the tools are in your hands to make the "deriving" logic yourself (and that tool is just plain old type classes). Where I've found that you can get in trouble with structural types (where I've got in trouble) is in designating a "meaning" to a particular structure when that structure isn't canonical. The bits you found with IMHO this is not a bug with structural types per se, but just a "user error" (one I've made a few times as I've come to terms with having this tool available myself). In hindsight, I think that the right approach is to make structural types the default and introduce "intentional types" to designate these cases where you have some intention where you pick a structural representation arbitrarily. Does that make sense? I think you can still do what you do in Haskell, but don't have to resort to Template Haskell (or avoid it out of exhaustion) and can also do obvious things like have two struct types with overlapping field names in the same module, ditto for variants. |
I've got a couple simple typeclass things working, but the code below fails, and I don't see why
class AddOne a b | a -> b where
addOne :: a -> b
instance AddOne int int where
addOne x = x + 1
instance (Map a b c d e f g h, AddOne c d) => AddOne f h where
addOne x = fmap(addOne, x)
Since we have both
instance () => Map d c a b 'array' [a] 'array' [b]
instance AddOne int int
it would seem that we should be able to resolve AddOne [int] [int]
But addOne([1]) fails.
Sadly nothing in boot does anything of the form instance Map ... => ... so it is tricky to puzzle out.
The text was updated successfully, but these errors were encountered: