Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,869
    • Issues 4,869
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 455
    • Merge requests 455
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #19531
Closed
Open
Created Mar 12, 2021 by Edward Kmett@ekmett

Allow separate pattern synonym construction types

Motivation

Usually today when I go to write a pattern synonym that has non-trivial constraints on construction and pattern matching I wind up having to clutter user facing code with two patterns names and an extra combinator.

Example:

I build the bidirectional pattern synonym that contains all the constraints:

pattern JSON :: (FromJSON a, ToJSON a) => () => a -> String     
pattern JSON a <- (preview _JSON -> Just a) where     
  JSON a = _JSON # a

but because that is so restrictive, I wind up having to supply a unidirectional pattern and combinator for the other two halves of the transformation, lest I require the user to pass me (possibly quite inefficient) dictionaries for unused directions.

pattern JSON' :: ToJSON a => a -> String
pattern JSON a <- ...

jSON :: FromJSON a => a -> String
jSON = ...

Proposal

I'd really much prefer to be able to split these two sets of constraints up in the explicit bidirectional pattern syntax:

pattern JSON :: ToJSON a => a -> String
pattern JSON a <- ... where
  JSON :: FromJSON a => a -> String
  JSON a = ...

The outer signature should keep its Required => Provided => ... context as usual, but the signature of the constructor could be specialized. Now I only have to take one user facing name and the API is much cleaner with no spurious unnecessary constraints being passed. In the absence of such a signature JSON used as a constructor would default to Required => ... as usual.

Outside of JSON I have examples involving BDD construction, serialization, Read/Show and pretty much anything where the To and From directions are split into separate classes.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking