#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by cactus):
I realize I was wrong on my last comment: let's not even discuss exporting
data constructors as pattern-only here, otherwise this ticket will lose
focus.
So let's just come up with some syntax to discern unidirectional vs.
bidirectional pattern synonym definitions.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:29>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by igloo):
Ah, I see, I had expected that something defined with "pattern" (e.g.
`pattern Arrow t1 t2 = App "->" [t1, t2]`) could only be used as a
pattern. I think a different keyword, e.g. "synonym" (or perhaps "view",
as Simon suggested) would be better in that case.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:28>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by simonpj):
Replying to [comment:25 cactus]:
> > We have at various times discussed how to allow a module to export a
data constructor that can be used as a pattern but not as a constructor
(in expressions), because the data type is abstract and you should only
use a smart constructor. With pattern synonyms you can (almost) do that
>
> What if you could just export `pattern P` (where `P` is the name of the
data constructor) without having to define `AbsP`? Would that be taking
things too far?
That would treat pattern synonyms and data constructors differently, which
I am keen to avoid. Under what you propose:
* If P is a data constructor then `pattern P` would export P uni-
directionally (pattern only)
* If P is a (bi-directonal) pattern synonym then `pattern P` would export
P bi-directionally.
I don't like that... a data constructor is precisely a (degenerate)
bidirectional pattern.
I suppose you could put the directionality in the export list:
{{{
module M(
pattern P, -- Uni-directional
constructor Q -- Bidirectional
) where
pattern P x = [x]
pattern Q y = [y,y]
}}}
But I don't want to go this way. If you export P you get all of what P
is. Otherwise we'll get into exporting it unidirectionally from one
module, bidirectionally from another, then importing both of those, and
combining the directinoality of those imports. Too complicated.
Maybe `datacon P`, or `view P`, rather than `pattern P` would address
Ian's worry?
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:27>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by simonpj):
Replying to [comment:24 igloo]:
> I haven't followed the design, but I would expect exporting "pattern P"
to either be an error ("P is a data constructor, not a pattern") or to
export P in a way that allows it only to be used as a pattern, not an
expression.
Well in the design here [wiki:PatternSynonyms], `pattern P` exports P for
use both in patterns and expressions. By all means suggest alternative
designs; that's what this thread is all about.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:26>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by cactus):
Replying to [comment:23 simonpj]:
> I suggest we allow the user to control whether a pattern synonym is
bidirectional or not. I'm not sure what syntax to use for that. We want
to be able to say
> * Uni-directional pattern synonym
> * Bi-directional pattern synonym
> * Bi-directional pattern synonym with explicit user code for the
reverse direction
>
Once a syntax for this is decided, this is trivial to add to my current
implementation.
> We have at various times discussed how to allow a module to export a
data constructor that > can be used as a pattern but not as a constructor
(in expressions), because the data type is abstract and you should only
use a smart constructor. With pattern synonyms you can (almost) do that
What if you could just export `pattern P` (where `P` is the name of the
data constructor) without having to define `AbsP`? Would that be taking
things too far?
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:25>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by igloo):
I haven't followed the design, but I would expect exporting "pattern P" to
either be an error ("P is a data constructor, not a pattern") or to export
P in a way that allows it only to be used as a pattern, not an expression.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:24>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by simonpj):
We must treat data constructors uniformly with pattern synonyms. For
example, it should definitely be OK to have
{{{
module M( pattern P, foo, bar ) where
-- The "pattern P" exports data constructor P
data T = P | Q
foo :: T
foo = P
bar :: T -> Int
bar P = 1
bar Q = 2
module A where
import M
-- T is not in scope
w1 = bar P -- Uses P as an expression
w2 = case foo of
P -> 1 -- Uses P as a pattern
}}}
We have at various times discussed how to allow a module to export a data
constructor that can be used as a ''pattern'' but not as a ''constructor''
(in expressions), because the data type is abstract and you should only
use a smart constructor. With pattern synonyms you can (almost) do that:
{{{
module M( T, pattern AbsP, pattern Q ) where
data T = P Int | Q
pattern AbsP = P
}}}
But I really wanted `AbsP` to be uni-directional and according to the
current spec [wiki:PatternSynonyms] it is "implicitly bidirectional". I
suggest we allow the user to control whether a pattern synonym is
bidirectional or not. I'm not sure what syntax to use for that. We want
to be able to say
* Uni-directional pattern synonym
* Bi-directional pattern synonym
* Bi-directional pattern synonym with explicit user code for the reverse
direction
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:23>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
#5144: Pattern synonyms
-------------------------------------+------------------------------------
Reporter: simonpj | Owner: cactus
Type: feature request | Status: new
Priority: normal | Milestone: _|_
Component: Compiler | Version:
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by cactus):
If your question is whether there's a syntactic way to see if a given
pattern synonym import can be used as an expression, then no, not at the
moment. So if you do something like
{{{
import M(pattern P)
x = P
}}}
then the only error you will get if `P` is not bidirectional is from the
type checker:
{{{
/tmp/hs/M2.hs:5:5:
P used in an expression, but it's a non-bidirectional pattern synonym
In the expression: P
In an equation for ‛x’: x = P
}}}
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/5144#comment:22>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler