14 Jun 2012 05:23
Re: What extension do I need to write "type Job = Map k a"?
Arlen Cuss <ar <at> len.me>
2012-06-14 03:23:51 GMT
2012-06-14 03:23:51 GMT
(resending to café, turns out I wasn't subbed from this address.)
Hi Magicloud,
This is correct; because you've hidden the type-variables away by universally quantifying them, there's
no more level of specificity you can get back *out* of them than just "some kind of Map" (Job = M.Map k b, where
k ≠ k0, b ≠ b0).
If you have a Job type which can store *any* kind of Map (forall k a. Job (Map k a)), then that means you could
have a Job with a Map Int Bool, and a Job with a Map String (Float -> Float), and they'd both have the same type
"Job". You can't do anything with the values within, because you're being too permissive about what a Job is.
You may want "data Job k a = Job (Map k a)", *or* if you do actually use one kind of Map only, then why not "data Job
= Job (Map Int String)" (substituting your real types for Int and String). In this case, you could also
consider using newtype ("newtype Job = Job { getJob :: Map Int String }") to provide the guarantee that
you're getting a Job (and not any Map Int String) without performance loss.
Let me know if I've been more confusing than helpful;
Arlen
On Thursday, 14 June 2012 at 1:16 PM, Magicloud Magiclouds wrote:
> Hi there,
> Thanks for the reply. To be clear, all I want is to "avoid having to
> type type variables all over the place". What should I do? My original
> code with RankNTypes and ImpredicativeTypes does not work....
>
> The "type Job = forall k a. M.Map k a" works now. But function uses
> it does not. Compiler complains about "Couldn't match expected type
> `Job' with actual type `M.Map k0 b0'".
(Continue reading)
RSS Feed