Opened 3 years ago

Closed 11 months ago

Last modified 3 months ago

#8761 closed feature request (fixed)

Make pattern synonyms work with Template Haskell

Reported by: goldfire Owned by: bollmann
Priority: normal Milestone: 8.2.1
Component: Template Haskell Version: 8.0.1
Keywords: PatternSynonyms Cc: merijn@…, heisenbug, cactus, cocreature
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D1940
Wiki Page:

Description

Template Haskell has no support for pattern synonyms. This should be added.

Specifically:

  • Create syntax in Language.Haskell.TH.Syntax and corresponding functions in Language.Haskell.TH.Lib
  • Support in DsMeta for desugaring pattern synonym quotations. See rep_bind.
  • Support in Convert for splicing in pattern synonyms.
  • Support in TcSplice for reifying pattern synonyms. See reifyThing.

Change History (31)

comment:1 Changed 3 years ago by thoughtpolice

Milestone: 7.8.2
Version: 7.8.1-rc1

This isn't going to be done in time for 7.8.1 at least.

comment:2 Changed 3 years ago by thoughtpolice

Or, well, unless you propose to add it (very soon), Richard. :)

comment:3 Changed 3 years ago by goldfire

No. My thought is that the TH interface should (barring some large disaster) remain stable from the RC to the released version. That way, packages that use TH can test against the RC and be confident that their code will work against the release. Even if I were to implement it today (no plans to), I wouldn't recommend merging. I created the ticket just as a reminder (to anyone) that this should be done.

comment:4 Changed 3 years ago by thoughtpolice

Milestone: 7.8.27.8.3

comment:5 Changed 3 years ago by thoughtpolice

Milestone: 7.8.37.10.1

Moving to 7.10.1

comment:6 Changed 3 years ago by merijn

Cc: merijn@… added

comment:7 Changed 2 years ago by cactus

Cc: cactus added
Keywords: pattern synonyms added

comment:8 Changed 2 years ago by cactus

Keywords: PatternSynonyms added; pattern synonyms removed

comment:9 Changed 2 years ago by thoughtpolice

Milestone: 7.10.17.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:10 Changed 19 months ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:11 Changed 17 months ago by sboo

one motivation is for fixed points https://www.reddit.com/r/haskell/comments/3qrkzr/one_weird_trick_for_nicer_functor_fixpoints/cwhsy6i

for example:

{-# LANGUAGE TemplateHaskell, PatternSynonyms, DeriveFunctor #-}

data TreeF a r
 = LitF a 
 | VarF String 
 | BinF r Operation r
 deriving Functor

data Operation = Add | ...

makeFix ''TreeF

would generate:

type Tree a = Fix (TreeF a)
    
pattern Lit :: a -> Tree a
pattern Lit a = Fix (LitF a)

pattern Var :: String -> Tree a
pattern Var s = Fix (VarF s)

pattern Bin :: Tree a -> Operation -> Tree a -> Tree a
pattern Bin l op r = Fix (BinF l op r) 

which would make TreeF as easy to use as Tree when using pattern matching:

optimize :: Tree a -> Tree a 
optimize (Bin (Lit x) Add (Lit y)) = Lit (x + y) 
optimize ... = ...

and still as easy when using recursion schemes:

evalTreeF :: Map String Integer -> TreeF Integer (Maybe Integer) -> Maybe Integer 
evalTreeF environment (BinF (Just l) Add (Just r)) = Just (l + r) 
evalTreeF ...

evalTree :: Map String Integer -> Tree Integer -> Maybe Integer 
evalTree environment = cata (evalTreeF environment) 

(sorry if here's the wrong place, this is my first post)

comment:12 Changed 17 months ago by goldfire

This is the right place. Thanks for posting!

comment:13 Changed 17 months ago by cocreature

Cc: cocreature added

comment:14 Changed 15 months ago by cocreature

Owner: set to cocreature

comment:15 Changed 14 months ago by thomie

Milestone: 8.0.1

comment:16 in reply to:  14 ; Changed 13 months ago by bollmann

Replying to cocreature:

Have you been working on this cocreature? Otherwise, I'd love to take ownership of this ticket. :-)

comment:17 in reply to:  16 Changed 13 months ago by cocreature

Owner: cocreature deleted

Replying to bollmann:

Replying to cocreature:

Have you been working on this cocreature? Otherwise, I'd love to take ownership of this ticket. :-)

I started working on it, but I ran out of time to finish it. I just pushed the current status. Feel free to pick it up or start from scratch.

comment:18 Changed 13 months ago by bollmann

Owner: set to bollmann

thanks cocreature, I'll have a look at this feature now.

Last edited 13 months ago by bollmann (previous) (diff)

comment:19 Changed 13 months ago by bollmann

Differential Rev(s): Phab:D1940

I added an initial version of "pattern synonym support in TH" with a couple of questions for feedback. See https://phabricator.haskell.org/D1940.

comment:20 Changed 11 months ago by bollmann

Status: newpatch

comment:21 Changed 11 months ago by Ben Gamari <ben@…>

In c079de3/ghc:

Add TH support for pattern synonyms (fixes #8761)

This commit adds Template Haskell support for pattern synonyms as
requested by trac ticket #8761.

Test Plan: ./validate

Reviewers: thomie, jstolarek, osa1, RyanGlScott, mpickering, austin,
goldfire, bgamari

Reviewed By: goldfire, bgamari

Subscribers: rdragon

Differential Revision: https://phabricator.haskell.org/D1940

GHC Trac Issues: #8761

comment:22 Changed 11 months ago by bgamari

Milestone: 8.2.1
Resolution: fixed
Status: patchclosed
Version: 8.0.1

comment:23 Changed 10 months ago by heisenbug

Cc: heisenbug added

Any progress on this? I'd *love* to create explicitly bidirectional pattern synonyms for FPGAs from regmap files.

OH ISEE ITS FIXED! Wow, any chance of a back port to GHC 8.0?

Last edited 10 months ago by heisenbug (previous) (diff)

comment:24 Changed 10 months ago by mpickering

No, I don't think there is any chance of a back port.

comment:25 Changed 9 months ago by Ryan Scott <ryan.gl.scott@…>

In a33b498d/ghc:

Add template-haskell changelog note for #8761

comment:26 Changed 7 months ago by Ben Gamari <ben@…>

In b451fef/ghc:

users_guide: #8761 is now fixed

Remove a leftover note in the users guide claiming that TH doesn't
support pattern synonyms.

comment:27 Changed 6 months ago by heisenbug

Not sure how it is supposed to work, but can I somehow pass a name to be spliced into a pattern synonym quote?

:t \n -> [d| pattern $(n) = 42 |]

errors out (unfortunately).

comment:28 in reply to:  27 ; Changed 6 months ago by goldfire

Replying to heisenbug:

Not sure how it is supposed to work, but can I somehow pass a name to be spliced into a pattern synonym quote?

:t \n -> [d| pattern $(n) = 42 |]

errors out (unfortunately).

No, I don't believe that's possible, because TH doesn't allow "name splices". You'd have to build up the syntax manually, using the Dec constructors or the functions from Language.Haskell.TH.Lib.

comment:29 in reply to:  28 Changed 5 months ago by heisenbug

Replying to goldfire:

Replying to heisenbug:

snip

No, I don't believe that's possible, because TH doesn't allow "name splices". You'd have to build up the syntax manually, using the Dec constructors or the functions from Language.Haskell.TH.Lib.

Oh, sure. I'd build as much as possible using a quote and then alter the name by a transformation on the ADT.

Prelude Language.Haskell.TH> runQ [d| pattern Foo <- 42 where Foo = 43 |]
[PatSynD Foo_0 (PrefixPatSyn []) (ExplBidir [Clause [] (NormalB (LitE (IntegerL 43))) []]) (LitP (IntegerL 42))]

Swapping out the Foo_0 looks easy.

comment:30 Changed 3 months ago by magesh.b

Is possible to extend this feature to support generating COMPLETE pragma #8779, if #8779 is going to land in 8.2

comment:31 Changed 3 months ago by mpickering

Please open a new ticket and assign me to it.

Note: See TracTickets for help on using tickets.