Skip to content

Require namespacing fixity declarations for type names - #65

Merged
nomeata merged 7 commits into
ghc-proposals:masterfrom
RyanGlScott:type-infix
Nov 3, 2017
Merged

nomeata merged 7 commits into
ghc-proposals:masterfrom
RyanGlScott:type-infix

Conversation

@RyanGlScott

@RyanGlScott RyanGlScott commented Jul 29, 2017 •

Copy link
Copy Markdown
Contributor

The proposal has been accepted; the following discussion is mostly of historic interest.


This proposes a way to clean up GHC's implementation of fixity declarations for names that live at both the value and type level, as the current implementation is rather unsatisfying (and sometimes broken).

Rendered

@vagarenko

Copy link
Copy Markdown

IMO

infixr 0 type $

looks better than

type infixr 0 $

We are declaring fixity, so it makes more sense to place 'infixr' at the beginning, not 'type'.

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

You raise a good point. I can understand wanting to put infix{l,r} at the beginning. Moreover, I think this would be easier to parse, too (not that this should dictate the design, but it's a nice side effect of this design choice).

If no one objects, I'll repaint the proposal with this new bikeshed color.

@goldfirere

Copy link
Copy Markdown
Contributor

I'm in favor of this proposal, without caring much about the concrete syntax.

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

I've updated the proposal to use the new infix{l,r} n type syntax, and included a blurb in the Alternatives section discussion the possible choices of bikeshed color for the syntax.

@treeowl

treeowl commented Jul 30, 2017 via email

Copy link
Copy Markdown
Contributor

@RyanGlScott

RyanGlScott commented Jul 30, 2017 •

Copy link
Copy Markdown
Contributor Author

This sounds like a @goldfirere question, but I'll try to answer nonetheless.

I think what this question is getting at is that there are of some named constructs which can be used in multiple namespaces. Well, I say "some", but at the moment there's really only one such thing: data constructors, via DataKinds, can be used as values or types.

I put a sentence in this proposal about how promoted data constructors can't be assigned a fixity with infix{l,r} n type, since they inherit the fixity assigned to the original, unpromoted, value-level data constructor. But in a brave new dependent world, it's possible that there are more names that will be "promotable" from the value level to the type one. In such a scenario, I'd imagine the same principle would apply. You still wouldn't be able to assign these value names fixities via infix{l,r} n type—you'd have to assign a fixity to the value (via infix{l,r} n first), and the promoted version would inherit that.

(Correct me if I'm totally off-base here, @goldfirere.)

I didn't think to include this in the proposal, since Dependent Haskell is its own, somewhat orthogonal thing. But I can fold in this discussion in the section about promoted data constructors if you'd wish.

@RyanGlScott

RyanGlScott commented Jul 30, 2017 •

Copy link
Copy Markdown
Contributor Author

I just had a thought. In the proposal, I give an outline for phasing about infix{l,r} n's ability to refer to type-level names. But this isn't as straightforward as I originally thought. For example, what about this scenario?

infixr 5 $

($) :: (a -> b) -> a -> b
f $ x = f x

type f $ x = f x

Now it's true that as a consequence of the way GHC currently works, the infixr 5 $ declaration applies to both the value-level and type-level ($). But perhaps the programmer doesn't care about the fixity of the type-level ($) at all! However, under my proposed three-release deprecation plan, the programmer will start seeing warnings when compiling this code in some future major release.

I imagine the programmer would be quite annoyed by this because the fixity of type-level ($) is unimportant to her—she originally intended, and continues to intend, for infixr 5 $ to refer to the value-level ($) only. But in order to squelch these warnings, she'd have to add an explicit infix{l,r} n type $ declaration. But really, there's nothing that ought to be changed at all, since in the following major GHC release, the above code will continue to compile without problems (since infixr 5 $ will then unambiguously refer to the value-level ($)).

To avoid these kind of headaches, we should probably scale back the scope of these warnings to only include scenarios where a fixity declaration refers to something that is only a type-level name, like this:

infixr 5 $

type f $ x = f x

This will eventually fail to compile, so it makes sense to warn here. However, I don't think we'd be able to warn about every such scenario which will break in the future. For instance, what about this?

module A where

infixr 5 $

($) :: (a -> b) -> a -> b
f $ x = f x

type f $ x = f x
module B where

import A

type Foo = Maybe $ Maybe $ Int

The code in module B only compiles because GHC happened to assign infixr 5 to type-level ($), so this will break in the future when infix{l,r} n loses the ability to refer to type-level names. But for the reasons discussed above, it's not appropriate to warn about the code in module A (after all, the real problem is the use site in B, which might be far removed from A). So we're stuck between a rock and a hard place—we have code which will break under this proposed change, but we can't warn about it.

My hope is that such a scenario happens rarely in practice—but maybe I'd need to try building all of Stackage against a version of GHC that takes away infix{l,r} n's ability to refer to multiple namespaces to be sure.

@goldfirere

Copy link
Copy Markdown
Contributor

@treeowl Good question. I find that it's easiest just to think about the fact that Haskell has several different namespaces. Every name belongs to precisely one of these. A fixity is a property of a given name. This fixity exists independent of what context a name is used in.

So, if our old friend : has a fixity, then ': (the "promoted" version of it) has that same fixity -- they're the same name. (I've come to think of the ' in types not as a promotion operator, but rather as a namespace-selection operator. The default namespace in a type is the type-level namespace, and GHC looks in the data-level namespace when the lookup in the type-level namespace fails. Using ' tells GHC to bypass the type-level namespace. And that's it!)

And I disagree with some of @RyanGlScott's response to @treeowl's question. I hate to nitpick like this, but this is all quite subtle: you state "some named constructs which can be used in multiple namespaces". But a namespace is something that's a property of a name (in my parlance, at least). I think you mean multiple contexts, here. As explained in my parenthetical above, GHC does its lookup differently in different contexts. And it's true that there's only one sort of name which makes sense both in a data-level context and a type-level context: data constructors. And so I was pleased when I read in your proposal that the fixity of a data constructor used in a type-level context is the same as its fixity in a data-level context. (For now, it's technically possible to have different fixities in the different contexts. But that would be awfully confusing.)

Bottom line: you assign a name (in whatever namespace it lives) a fixity. And then that name has that fixity in whatever context it is used.

@goldfirere

Copy link
Copy Markdown
Contributor

In response to @RyanGlScott's most recent worry: you could always use data as the opposite of type. So, infixr 5 data $ could unambiguously mean the data-level. This would nicely squelch the warning.

I do believe that your release schedule doesn't fully follow the 3-release policy. Let GHC0 be the release that this is implemented in. Then, GHC-1, GHC0, and GHC1 won't all be able to compile the same code without warnings, if the warning is in -Wall. So I think we need one more release in the plan.

@simonpj

simonpj commented Jul 31, 2017

Copy link
Copy Markdown
Contributor

The namespace question is brought into focus by

data T = T Int Int

Here we have a type constructor and a data constructor called T; but they are in different name-spaces. Even if Haskell became a full-spectrum dependently typed language, it'd doubtless still have these two namespaces, for back-compat reasons.

The question in my mind is: should it be possible to give the two T's different fixities? I think one could reasonably argue either way.

  • GHC currently says "no, they must have the same fixity". Regardless of whether the implementation does a good job, that's a reasonable position. It's confusing to have two different fixities for what looks like the same thing

  • But since the two Ts are different, you could argue that they should have independently-settable fixities.,

I don't feel very strongly. But I think there is a reasonable case for the status quo; and if it is currently buggy I'm sure it could be made to work properly with no more effort than implementing something new.

@goldfirere

Copy link
Copy Markdown
Contributor

@simonpj's T example is good. Here, the two T are tightly related. But what if we have

data T a b = MkT Int Bool a b Double
data X = T Int Int

Now, it seems more compelling to allow the two Ts to have different fixities. Currently, these two Ts can be given different fixities only by declaring them in two different modules. (Or separating them with a top-level TH splice. But I haven't tried that last one.)

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

@goldfirere:

The idea of adding a namespace keywords for value-level things is intriguing. I'm personally not a fan of the keyword data, since saying infixr 5 data $ might mislead a reader into thinking ($) is a datatype. How about infixr 5 value $ instead? There hasn't really been any explicit way to refer to the value namespace up until now, but I suppose it's time to change that.

Moreover, this presents an opportunity for a GHC warning that we could emit even after the three-release windows passes. If you have code like this:

module A where

infixr 5 $

($) :: (a -> b) -> a -> b
f $ x = f x

type f $ x = f x

Then GHC could warn:

* infixr 5 $ refers to both a value-level and type-level name
* Disambiguate by using namespaces (using the ExplicitNamespaces extension):
    infixr 5 value $
    infixr 5 type $

As you noted, this would catch this code in the act before the breaking change drops.

I'll update the proposal soon with the above changes, as well as fixing the three-release-policy window and including a more detailed discussion on things used in multiple contexts in light of @treeowl's and @simonpj's comments.

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

I've updated the proposal to include the value namespace into the discussion, talk more extensively about things that can be used at both the value and type level, and fix the 3-release policy.

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

Four weeks have passed since this proposal was first submitted, and I believe the proposal in its current form resolves folks' concerns. Given the lack of continued discussion, I'd like to submit this to the committee for consideration.

cc @nomeata

@nomeata

nomeata commented Aug 27, 2017

Copy link
Copy Markdown
Contributor

I’ll shephard this one.

@nomeata nomeata self-assigned this Aug 27, 2017
@nomeata nomeata added the Pending committee review The committee needs to evaluate the proposal and make a decision label Aug 27, 2017
@simonpj

simonpj commented Aug 28, 2017

Copy link
Copy Markdown
Contributor

I had not realised this, but today it is already possible to have one fixity for a type-level T and another for a value-level T:

{-# LANGUAGE TypeOperators #-}
module Foo1 where
    infixl 1 `T`
    data T a b = MkT

module Foo2 where
    infixr 9 `T`
    data S = T S S

{-# LANGUAGE TypeOperators #-}
module Foo where
    import Foo1
    import Foo2

    f :: a `T` b `T` c
    f = f

    g a b c = a `T` b `T` c

In module Foo the type level T is left associative, but the term-level T is right-associative.

@simonpj

simonpj commented Aug 28, 2017

Copy link
Copy Markdown
Contributor

The only consistent positions seem to me to be

A. A fixity is a property of an entity, where "entity" is something like "the type constructor T" or "the data constructor T".

B. A fixity is a property of a lexeme like "T", irrespective of namespace. That leads to different change to GHC which would mean looking up fixities by string rather than by Name. Then in the Foo example pointed to by the above link, we'd presumably have an ambiguous fixity for T.

BUT that would run into trouble when you said "a M.T b". In fact Haskell 2010 explicitly says "Fixity is a property of a particular entity (constructor or variable), just like its type; fixity is not a property of that entity’s name”: https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-800004.4

I think B is a non-starter, for the reasons mentioned above.

So, at the AST level I think we must have (some version of) Ryan’s proposal. The only question is whether or not we expose that expressiveness to the programmer. That is, we should fix the TH problem by fixing the AST, so that we can round-trip through TH without losing information.

But at the source-code level we are not forced to make a change. It is tiresome that, to get a different fixity for the type-level and term-level things, you have to declare them in different modules. If they are part of the same data type, or same mutually-recursive group, that might be awkward or impossible. It's a wart. But it's not a very pressing wart, and arguably we have bigger fish to fry.

@goldfirere

Copy link
Copy Markdown
Contributor

How would it work to round-trip via TH? Suppose I have

data T a b = T a b

$( [d| infix 5 T |] )

What would -ddump-splices print?

I guess the question is what fix for the AST do you have in mind? Right now, the fixity declaration is duplicated in the AST, with one declaration for each name. Are you proposing not to do this... say by storing a string instead of a name in a fixity's declaration AST node? (That would actually seem to work, given that you can't, I believe, declare the fixity for an entity declared in a different module.)

@simonpj

simonpj commented Aug 28, 2017

Copy link
Copy Markdown
Contributor

I have not worked out exactly what the AST should say. Presumably it must record enough to reconstruct what was originally written. Currently a FixityDecl has a list of IdP pass; I suppose that must turn into a list of FixItem pass, where a FixItem is something like

data FixItem pass = FixItem 
                                    (Maybe (IdP pass)) -- Value level
                                   (Maybe (IdP pass))  -- Type level

@goldfirere

Copy link
Copy Markdown
Contributor

But you'd only be able to tell the difference after the renamer... so I think that definition isn't quite right, but I do see how such a thing could round-trip through TH.

@simonpj

simonpj commented Aug 28, 2017

Copy link
Copy Markdown
Contributor

Well, if Ryan's proposal can work at all, it must have an AST. Just use that AST.

My bigger point is only that AST can be more expressive than the concrete syntax of the source language.

@RyanGlScott

RyanGlScott commented Aug 29, 2017 •

Copy link
Copy Markdown
Contributor Author

Perhaps I'm missing something obvious, but I don't see how this would roundtrip through TH without changing the concrete syntax. Can you give an example illustrating how this would work?

(Also, apologies for jumping the gun on submitting this for committee approval—it looks like there's still some discussion to be had. Feel free to remove this from committee consideration if you deem it appropriate to do so.)

@mchakravarty

Copy link
Copy Markdown

If it is not user facing, there is no need for a proposal IMHO.

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

That's what I figured, thanks. Nevertheless, I don't want to close the issue quite yet—the exact design is still somewhat murky, and it's possible that in attempting to implement this, I might discover a hidden roadblock.

@mchakravarty

Copy link
Copy Markdown

Sure, please have a look what's possible and what's not and let us know when you have made a decision. Thanks!

@goldfirere

Copy link
Copy Markdown
Contributor

While I imagine a fix for the current TH bug is possible without user-facing consequences (beyond clients of TH, who are used to breaking changes), I don't think this proposal needs to be intimately tied to that bug. In other words, even with the bug fixed, I think this proposal has merit.

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

While attempting a prototype implementation of the plan in #65 (comment), I realized something that I hadn't considered before. I had heretofore assumed that fixity declarations were unique in their ability to refer to both value- and type-level entities simultaneously, but it turns out there is another sort of declaration with this power: DEPRECATED/WARNING pragmas. That is, in the code below:

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Foo where

{-# DEPRECATED (!@#$) "Don't use this." #-}

(!@#$) :: Int -> Int -> Int
x !@#$ y = x + y

type family x !@#$ y
{-# LANGUAGE TypeOperators #-}
module Bar where

import Foo

bar :: Int !@#$ Int -> Int
bar _ = 1 !@#$ 2

When compiling Bar, two deprecation warnings will be emitted, for both the value- and type-level occurrences of (!@#$).

Unlike fixity declarations, DEPRECATION/WARNING pragmas aren't available for use in Template Haskell, so there's not a GHC bug that directly motivates changing the way DEPRECATION/WARNING pragmas are renamed. But it does feel more consistent to do it regardless. (Plus, if DEPRECATION/WARNING pragmas are ever put into Template Haskell, this would save us some headaches down the line.)

@nomeata

nomeata commented Oct 8, 2017

Copy link
Copy Markdown
Contributor

Good observation! This indeed strengthens the proposal. (And in my opinion supports the point that ExplicitNamespaces should be extended, rather than a new pragma introduces.)

@goldfirere

Copy link
Copy Markdown
Contributor

Yes -- very interesting. No one will be surprised when I say that we should have {-# DEPRECATED type F ... #-} and similar for WARNING. I would actually suggest reformulating this proposal to cover such pragmas alongside fixity declarations, relegating the TH bug to an "effect and interaction" and instead focusing on tidying up a wart in the language.

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

Well, you say "such pragmas", but I literally cannot think of another example of a pragma besides DEPRECATED that has this property of being able to refer to both value- and type-level things. For instance:

  • INLINE only applies to value-level things.
  • SPECIALIZE... value-level things.
  • MINIMAL: you guessed it, value-level things.
  • SCC: as it turns out, value-level things only.
  • COMPLETE: value-level, and value-level alone.

So as it stands, there's just two things that happen to have this weird interaction with namespaces. I'll admit that I don't feel motivated to overhaul this much of the language just to fix one pesky bug, so as things stand, once I have an implementation of @simonpj's ideas from #65 (comment) that adequately fix the Trac ticket, I'll close this proposal.

@nomeata

nomeata commented Oct 9, 2017

Copy link
Copy Markdown
Contributor

ANN is for values only, but it is quite reasonable that someone might want it for types some day.

there's just two things that happen to have this weird interaction with namespaces.

Depending how you count:

  1. Import lists
  2. Export lists
  3. fixity declaratoins
  4. various pragmas

@simonpj

simonpj commented Oct 9, 2017

Copy link
Copy Markdown
Contributor

there's just two things that happen to have this weird interaction with namespaces

Yes I was thinking about import and export lists too, where this comes up. E.g. can you export a pattern synonym P thus?

module Foo( P ) where ...

I think not. We've often speculated about being able to write

module Foo( value P ) where ...

or some other prefix, to disambiguate.

So although I hate to destabilise a nearly-done proposal, how hard would it be to generalise it somewhat?

@goldfirere

goldfirere commented Oct 9, 2017 •

Copy link
Copy Markdown
Contributor

I'm all for value Foo. :) In the past, I've suggested data Foo so we don't steal a new keyword, but that was understandably shot down.

And you can annotate types, even with much the same syntax we're considering generalizing: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/extending_ghc.html#annotating-types

@nomeata nomeata mentioned this pull request Oct 14, 2017
@nomeata

nomeata commented Oct 27, 2017

Copy link
Copy Markdown
Contributor

@RyanGlScott We have consensus in the committee to accept this, by extending ExplicitNamespaces to fixity lists and DEPRECATION/WARNING pragmas.

Would you care to amend the proposal accordingly before I merge it?

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

To be clear, what exactly should be amended? The discussion currently revolves around just infix declarations, so should the entire prose be updated to integrate DEPRECATION/WARNING into the mix? Or would it suffice to add a paragraph or so mentioning that similar changes would be needed for these pragmas?

I think there's one other thing I'd like to change about this proposal. Back when I originally submitted this, I was under the impression that this was the only way we could fix Trac #14032, so I reluctantly volunteered myself to implement. But given that there's a middle ground in #65 (comment) that doesn't require any changes to the source language to implement, I'm going to implement just that, and resign as the implementor of this future (more-far-reaching) change. Is that acceptable?

@goldfirere

Copy link
Copy Markdown
Contributor

I don't think we accept/reject implementation responsibilities. So, I suppose that means it's acceptable, yes. This actually strikes me as a great beginner project to implement.

@nomeata

nomeata commented Oct 27, 2017

Copy link
Copy Markdown
Contributor

To be clear, what exactly should be amended? The discussion currently revolves around just infix declarations, so should the entire prose be updated to integrate DEPRECATION/WARNING into the mix? Or would it suffice to add a paragraph or so mentioning that similar changes would be needed for these pragmas?

Can they be treated just the same? Also when it comes to warnings about ambiguity etc.? I am hoping that you’ll do the thinking for me :-)

I'm going to implement just that, and resign as the implementor of this future (more-far-reaching) change. Is that acceptable?

Yes, it totally is (although I hope you are still interested in leaving the proposal in a good state, including the WARNINGs stuff, for whoever picks it up.)

@RyanGlScott

Copy link
Copy Markdown
Contributor Author

@nomeata, I've updated the proposal to give lip service to WARNING/DEPRECATED pragmas. I'll admit that it's somewhat bolted-on, so if you'd like me to elaborate in greater detail about a particular aspect of WARNING pragmas in this proposal, please let me know.

@nomeata

nomeata commented Nov 3, 2017

Copy link
Copy Markdown
Contributor

LGTM

@nomeata
nomeata merged commit 3d497f4 into ghc-proposals:master Nov 3, 2017
@nomeata nomeata added Accepted The committee has decided to accept the proposal and removed Pending committee review The committee needs to evaluate the proposal and make a decision labels Nov 3, 2017
RyanGlScott added a commit to goldfirere/singletons that referenced this pull request May 6, 2018
This implements workaround (2) in
#326 (comment),
which has the advantages of:

(a) Not needing to generate a bunch of superfluous code.
(b) In the event that ghc-proposals/ghc-proposals#65
    is implemented, it will be much easier to notice that the hack can be
    removed (since the workaround will then produce an error).
@adamgundry adamgundry added the Implemented The proposal has been implemented and has hit GHC master label Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Accepted The committee has decided to accept the proposal Implemented The proposal has been implemented and has hit GHC master

Development

Successfully merging this pull request may close these issues.

9 participants