Repository navigation
Top-level signatures - #148
goldfirere wants to merge 8 commits into
Conversation
It is not immediately clear what this means. |
|
I like that the explicit The examples using For any top-level signature with variables ( I'm struggling to make up an example, but say
To check my understanding: that The |
|
For methods, I'm keen on the idea of giving a full
EDIT: a possibly-fatal problem
end of EDIT. For example class If a method signature has explicit
Then
EDIT2: since this PR is aiming to subsume #129 and #54 for top-level So my rule above re "a constraint that repeats the class name" is to be interpreted as: the constraint can appear anywhere in the signature; the constraint must appear immediately within scope of an explicit Or was it the idea for addressing #129 that the current For class I think no danger of that (Suggestion for |
|
For I think it's acceptable that to do something heinous with BTW for record fields: if we have Perhaps @adamgundry could comment. There's a couple of leftover fishhooks:
|
|
I've updated the proposal to answer several of your questions in the text itself. Briefly, this proposal has no meaningful interaction with the keyword Yes, the For class methods, we can't make a choice based on whether the user has written a
As for I've clarified that Haskell98-syntax newtype constructors are available for use with the proposed feature. (Newtypes can use How are these signatures not lexically or semantically at top level? (I'm considering the body of the |
|
On Overall, I like this idea, though it sounds like the benefits might be relatively modest in comparison to the implementation cost. |
|
Thanks @goldfirere then I continue to be dismayed that this proposal requires giving two signatures. There's always going to be a chance they're out of kilter and/or the top-level one is non-obvious to the reader. To be clear: I like the objective for this proposal. I think there's too much new syntax; and it's too heavyweight. Is the two-signatures what's giving rise to the "type-equivalence check" and "swizzle the type" in section 4? Can we really not try harder to find a way to give only one signature? (My comment about not lexically top-level was a throwaway, hence the winky. Yes all these are semantically top-level decls.)
Hmm? The But OK: for methods, can we look at still having a single sig within the class
I hardly never write H98-syntax data decls. (I do for And for Then if |
|
Could you spell out the class motivation in more detail, for clarity? In particular, why can't, today, we write: Rather than using the "forall" as the signifier for the expanded meaning, we would use quantification over a class variable. Currently, trying to declare I can write something like this today And that I think gets a different meaning under this idea. But writing something that overloads the |
Heh, heh @gbaz : in a post-Hindley-Milner type
Yes. Why it's dumb is revealed in your error message: So the compiler has prefixed to the signature you gave in-class: Now tyvars-of-the-class don't appear in the type body, only in the constraint. That means that at any usage site for So the rule is: in the method signature, at least one of the class parameters must appear free. Note it doesn't need all of the class parameters to appear, so your second example is OK. One of the class parameters does appear, one doesn't, and that's exactly why To give a speculative answer in a post-Hindley-Milner world: Perhaps it doesn't matter if none of the class parameters appear in the method's signature. We could always supply them via type This is kinda using the class's type paramater as a phantom. It's a trick currently worked by using proxy type parameters. And indeed the distant-future proposal for duplicate record names/field selectors is using such a phantom: see the
If I understand @goldfirere correctly, he doesn't want to force use of
I'll defer to Richard if I've mis-represented any of this. |
|
On the naming of cats
All names and their signatures introduced in
Are we bound to produce (and consume) all of this proposal in one lump? Could we parcel it out?
|
|
Sorry for the confusion. When I wrote "Could you spell out the class motivation in more detail, for clarity?" the "you" in question that the remainder was directed to was @goldfirere , which is whom I would like an answer from. |
|
One other potential use case for this feature which I find intriguing is the ability to specify the visibility of class-bound type variables. Currently, when you declare a class such as: class C a b where
meth :: a -> b -> c -> d -> ...Then you have full control of how the type variables bound in the type signature of
You currently don't have much flexibility at all when it comes to class-bound variables In particular, one compelling use case for this is to work around a bit of an awkward design pattern that currently exists in Haskell. You often see type classes of the form: class Size a where
size :: proxy a -> IntWhere the type {-# LANGUAGE AllowAmbiguousTypes #-}
class Size a where
size :: IntBut this requires all end-users of I think the best design for this would be to leverage this proposal and write: class Size a where
size :: Int
size :: forall a -> Size a => IntThen folks could just write Am I correct in thinking that this proposal would enable such an example? Moreover, would writing a top-level signature for |
|
I'm in favor of this proposal, it resolves so many issues at once:
and it is a natural extension to the language.
If no one else volunteers, I can make an attempt at implementation. |
|
I'm a bit doubtful about the proposal. The signatures we already have for class methods and data constructors are so close to top-level type signatures (yes, I know, not quite because of all the tyvar ordering stuff) that it may be more confusing than helpful to have as second type signature. What is the problem that this proposal solves. What goes wrong if we just don't do it? |
Did you read the proposal? I'm not asking to be snarky—the proposal literally answers this exact question in the motivation section. See also #148 (comment), which provides further motivation. |
|
I did. I just was not convinced. The tyvar shuffling business is pretty specialised; and you can always do it anyway by defining a wrapper function of your own. The It's a cost/benefit tradeoff, and (so far) I'm not convinced that the benefits justify the costs. |
|
To be clear, is your particular objection to this proposal about the concept of top-level signatures in general, or just top-level signatures for class methods? I ask since top-level signatures have other compelling use cases for type-level declarations (as proposed in #54), so it seems quite natural to me to extend this idea to other entities in Haskell for which you cannot directly write a top-level type signature.
And yet, type variable shuffling (or more generally, proper quantification, as shown in #148 (comment)) is proving to become more and more important now that our quantifier syntax is becoming richer (see #102). I'd argue that this is a compelling enough use case on its own. And saying "you can always define a wrapper function" is punting the problem down the road, as now all users who define instances of your class with methods that lack the proper quantification will have to suffer. I'd rather treat the problem at its source by using a top-level type signature, which allows those writing instances to benefit from the proper quantification as well.
Yes, but this proposal would be a very important step into making the treatment for numeric literals less ad hoc which seems like a mighty good thing to me. The alternative, as discussed in #129, seems to take things into even further ad hoc territory. |
I feel this too. It seems strange to have a nearly correct type signature for a class method listed inside the class and then a correct one outside the class. I do wish we could tweak the quantification of type variables inside class methods. This proposal suggests (omitting the rejected As others have suggested, I would rather write: To me, this syntax is much more clear, and I haven't been convinced by this thread that this isn't possible. Richard offers:
Users can write forall in class methods today, but they cannot use bring into scope a type variable that shaddows the one in the instance head. For example, with GHC 8.4.3, the following fails to compile: Consequently, there should not be any code that compiles today that wouldn't compile with the fancier class method quantification mechanism. The original goal of this proposal is to resolve the awfulness of cusks. This proposal describes a features that does this and provides four examples (
Would it not be easier to solve the cusk problem with a simple keyword? Something like this: The keyword |
|
@andrewthad, I generally agree, but I do have a caveat. You write "Consequently, there should not be any code that compiles today that wouldn't compile with the fancier class method quantification mechanism." However, I gave a counterexample to this above: I would guess that no useful code today would be changed by this, but the above, perverse, example, does show code that would be changed by this. The trick is that the Overall, I think we should treat the three components of this proposal individually.
|
|
I've updated the proposal with some of the alternatives suggested. I'll try to respond to several points above: I like @gbaz's suggestion of considering features separately. I count 4, not 3:
A few other points to respond to:
|
|
I just added support for pattern synonyms, too. This allows a pattern synonym to list its existential variables before its universals, useful in concert with #126. The repetition of the type signature is particularly galling here, though -- suggests there can be improvement in the syntax for pattern synonym signatures. |
Yes there the
So what makes it not really higher-ranked? Oh, you want me to put parens like this What's the rule (in the absence of parens) that tells me you mean (B) not (A)? Whatever. I agree with @int-index the scoping rules are weird. That's why I suggested separating concerns: the
My understanding is that using
I'm not going to argue about semantics. (There are huge debates on the interwebs already.) All I'm saying is that "subtype" is a skunked term in the industry. And already has a usage in FP which is different. Avoid it if you can. And you can: the operative term you use is "equivalent". Give a definition for that in terms of substitutability. Actually, I think you just want types "unifiable"? Later correction: unifiable without alpha-renaming the tyvars from the class head. (Or is there some reason that won't work? Then please explain.) Or if you do want to argue about semantics, this:
Is incomplete in OOP languages, because OOP also has (destructive) assignment where it works the other way round. See 'Liskov Substitution Principle'. And in OOP, what's "allowed" (on RHS) is that |
Right, I'm thinking that the same rules should apply to all signatures in the module, though at this point it doesn't feel like "top-level" describes the behavior properly.
This sounds pretty much like what I had in mind. Though, I think we should still require that the class variables be used in the signature, i.e. no funny business like (I'm not sure if you intended to relax that requirement.) |
Note the User Guide (neither for Here's an example of the scope weirdness I think we all are worried about. Today I write [Falls off chair] So I can use (Note I do need So I can get the |
|
I have not been following the details of this discussion, but I have another possible idea to put forward:
Example: It's a bit like worker/wrapper: the top-level signature gives the type of a wrapper, whose definition is just a call (perhapas with swizzled arguments) to the underlying secretly-named class method. Advantages
Apologies if this has already been discussed. |
|
@simonpj This was the old version of the proposal, and I think it was great, but then we somehow converged on the idea that the top-level signature should be inside the class declaration, not actually top-level, and @goldfirere came up with cunning scoping rules to make it work. Given this design constraint, I came up with a use of |
Ah, sillly me. I hadn't realised that in the "old version" the top-level signature was strictly in addition to otherwise-unmodified data/class decls, and affects only occurrences. Old or not, I'd like to see it in our conversation. I don't know how we "somehow converged on the idea that the top-level signature should be inside the class declaration", and I'm frar from sure I like it. |
It is in the conversation on @goldfirere's revised proposal. There's a difficulty. But the difficulty isn't exposed with the I've alpha-renamed the toplevel decl, just so you can't see what's going on. OK: in the toplevel decl, which of those constraints |
|
I don't undersatnd teh difficulty. This defines a private function, the "real selector for Now you give a top-level signature for I say this is accepted if and only if this declartion would typecheck Job done. At least that's what I intended. |
|
One more reason I don't think it's a good idea to put top-level signatures in classes is that today the signature that we have inside the class is the actual signature of the field in the dictionary. So I agree with Simon's worker/wrapper view of this: specify both the internal signature (for the dictionary field, and this would be the type used to define instances), and a wrapper signature (appearing on the top-level, used to call the class method) |
|
The new proposal was in response to, e.g., #148 (comment). But, actually, on a second flip through, I don't see much else in support of this new style. @gbaz had made a few comments in that direction, but I don't see unequivocal support. Here's a problem with worker/wrapper: what happens in instance declarations? This question becomes more poignant with consideration for VDQ: What do we think here? Also, with VDQ, the |
That class isn't accepted, because |
|
The then it wouldn't be ambiguous. |
Oh, yes that extension considered harmful. One of the things I dislike about The idea of worker/wrapper for top level signatures seems easy to explain, and not too big of a stretch from writing different-named worker-wrappers today. Also we avoid an extra and clunky reservedword. Then if there is legitimate code (such as for VDQ or functions deliberately to be called with The Could I for the time being tackle this VDQ example with type-trickery such as |
|
To my own surprise, I've lost enthusiasm for this proposal. It's just too easy to do this via hand-written wrapper functions, and the syntactic challenges here are genuine. This proposal also falls into the category of proposals that introduce a syntactic nicety without an increase in expressiveness. These niceties are nice -- and I would use the feature proposed here occasionally -- but we can't have everything. Unless someone changes my mind, I'll close this in a few days. (Of course, someone is welcome to adopt and push this through.) I feel compelled to make one further point, though:
I know not everyone agrees, but I don't consider |
|
Thank you @goldfirere, I think it's been a valuable debate. One difficulty with VDQ I think remains for me
I find it entirely unacceptable that putting a dependently-quantified type in a method requires non-mention of the class parameter in the method signature. Failing to mention (all) class parameters in a method signature is almost certainly an error. If you're doing it consciously, you must signal it very clearly (with I'll reply separately on your points about |
|
I happen to agree with @AntC2 that If a definition is unusable without
After our last conversation about scoping issues of GADTs, I think I won't be writing a competing proposal either. I still want to have uniform scoping rules for classes and GADTs, but now I'm not sure what they should be. |
Then what you should say is that I agree with @int-index we should be aiming for some term- or decl-level level pragma (similar idea to Is it ever legitimate to switch on Perhaps we need an aptitude test before anybody's allowed to use
Those StackOverflow questions are by no means all from newbies. The harm is done because GHC's error message (which talks impenetrably about rigid type variables) recklessly suggests switching on a) If we had a newbie-mode for GHC, it should not be suggesting that at all. Or the message should say: you'll probably need We might lament that the pressure is on learners to get to an answer rather than to get to an understanding. But GHC throws them a lifeline "
So you'll see in the StackOverflow answers, there's typically some comments to-and-fro trying to tease out whether the questioner understands Perhaps saying " |
That's not what I said at all, though. I said we needed VDQ in terms, so that one could write and the users of this interface would write |
|
Heh heh. The global Jeepers. And this is the guy who rammed And a StackOverflow q in which ambiguous types appeared because of irresolvable overlapping instances (combined with FunDeps); but GHC suggested |
Ah, apologies for misunderstanding and misrepresenting. Getting to full-blown VDQ is much bigger a job than a pragma at a point where GHC currently would complain about ambiguous types(?) I didn't think you were proposing that much of a drama. Are you suggesting that signature for If |
Indeed, so I don't have a concrete proposal yet. |
|
Thanks, @AntC2, for #148 (comment). It's insightful. What I've wanted for some time (but haven't ever gotten around to making a reality) is a way for GHC error messages to refer users to web pages. The suggestions that Separately, I strongly support an As to the relationship between And @int-index's point is also interesting: once we have VDQ in terms, there will be little reason for ambiguity..... except, perversely, in class methods where (without this proposal) we can't write the signature we want. Even so, I now close this ticket. |
|
Heh heh. In parallel with the discussion here about
To which the response from @RyanGlScott
I think this underlines my concern that it's not just newbies who struggle with it.
I'll volunteer to write up that proposal -- or at least put up a strawperson to be improved by discussion. It goes without saying that there's only two people fully accredited to implement it; and I'm neither of them. Having probed about a bit, I notice there's lots of error conditions GHC complains about tyvars being ambiguous. But not always does it suggest I think also useful would be a flag |
|
There are two potential problems dealing with ambiguity: at definition sites and at usage sites. Implementation of an |
I rather agree. I'm not dead-set against it but I think we have bigger fish to fry. |
Rendered