Questions tagged [pattern-synonyms]

Abstraction of patterns in the pattern matching sense

In Haskell, use the PatternSynonyms language extension to turn on this feature (as of GHC 7.8+).

48 questions
3
votes
3 answers

Define Haskell type constructor equality?

I'm not meaning as a member of Eq. My code: data Race = Terran | Zerg | Protoss deriving (Eq, Show, Read); data MU = MU Race Race deriving (Eq, Show); In this case I define for instance (MU Terran Zerg). I want to create a data constructor TvZ…
Zorf
  • 6,334
  • 2
  • 29
  • 24
2
votes
1 answer

How to explicitly import 'Fn' pattern from Test.QuickCheck?

Haskell's Test.QuickCheck module exports pattern Fn, which I have been using. When I import it with: import Test.QuickCheck it works fine. However, when I import it with: import Test.QuickCheck (Fn) I get an error: Module ‘Test.QuickCheck’ does…
mherzl
  • 5,624
  • 6
  • 34
  • 75
2
votes
0 answers

Generating shingles with synonyms in Elasticsearch

I have a file of alternate spellings for the terms in my index. I want to produce bigrams containing those alternate spellings for particular terms. For example, I have biriyani, biryani, briyani in my alternate spellings csv file and my field…
2
votes
1 answer

Writing a pattern synonym to hide a constructor

Consider the following: module MyModule ( A(FortyTwo), -- Note we don't expose PrivateA B(P) -- Nor PrivateB ) where pattern FortyTwo = A 42 newtype A = PrivateA Int data B = PrivateB Int Int pattern P :: Int -> A -> B How could I write…
Clinton
  • 22,361
  • 15
  • 67
  • 163
2
votes
1 answer

Use literal numbers in Fin patterns

On one hand, I can use #_ to construct Fins from literals: open import Data.Fin data I'mFinnish : Set where Mk : Fin 5 → I'mFinnish foo : I'mFinnish foo = Mk (# 3) On the other hand, I can use literals to pattern match on naturals: open import…
Cactus
  • 27,075
  • 9
  • 69
  • 149
2
votes
1 answer

Using ViewPatterns and PatternSynonyms to simply pattern matches

Lets say I have a GADT for a language like so (my actual language is much more complex, about 50 constructors, but this is a simplified example): data Expr t where Add :: Expr t -> Expr t -> Expr t Sub :: Expr t -> Expr t -> Expr t Mult ::…
Clinton
  • 22,361
  • 15
  • 67
  • 163
1
vote
1 answer

How to merge nodes that represent synonyms?

Suppose I have a graph like this: Cytoscape graph The nodes 'lipid nanoparticle' and 'LNP' are synonyms. How can I identify nodes that represent synonyms and merge them in Cytoscape? I searched Cytoscape App Store, but didn't manage to find a…
1
vote
2 answers

Design options for constructor constraints: GADT compare PatternSynonym Required

(This is a follow-up to this answer, trying to get the q more precise.) Use Case Constructors to build/access a Set datatype. Being a set, the invariant is 'no duplicates'. To implement that I need an Eq constraint on the element type. (More…
AntC
  • 2,623
  • 1
  • 13
  • 20
1
vote
0 answers

Pattern synonyms deriving read?

Pattern synonyms provide a shorthand way to express a value; also they can provide an abstract name to avoid a client module breaking into the data decl. Here's a not very useful one, as an example for discussion: data MyNum = MkNum Int pattern…
AntC
  • 2,623
  • 1
  • 13
  • 20
1
vote
2 answers

How would we take the last value of a list using colon notation?

Say we have a list in the parameter and pattern match on its head and tail separately so we use x:xs. This means that the inputs are split into a value x and a list xs. So whatever is before the colon is treated as a single value and whatever is…
Qwertford
  • 1,039
  • 2
  • 14
  • 25
1
vote
1 answer

How to pattern match the end of a list?

Say I wanted to remove all zeros at the end of a list: removeEndingZeros :: (Num a, Eq a) => [a] -> [a] removeEndingZeros (xs ++ [0]) = removeEndingZeros xs removeEndingZeros xs = xs This does not work because of the (++) operator in the…
Mark Anastos
  • 365
  • 3
  • 14
0
votes
1 answer

How implement updating index by synonyms without closing index?

I need implement search by synonyms, now it possible todo by closing index, update index by synonym filter, open index. The probem is that users can update synonym dictanory in any time, and it should not affect system, but when index closed,…
0
votes
1 answer

How to refactor a Haskell data type with fields into a tagged union of records?

In a large code base, I want to refactor a data type that has constructors with fields into a simple tagged union of records, mainly to better support -Wincomplete-record-updates. It is important that I do not need to change any pattern matches…
Andreas Abel
  • 190
  • 1
  • 11
0
votes
1 answer

Identifying synonymous rows of a text column in a dataframe using R

Suppose ABC is a dataframe as given below: ABC <- data.frame(Column1 = c(1.222, 3.445, 5.621, 8.501, 9.302), Column2 = c(654231, 12347, -2365, 90000, 12897), Column3 = c('A1', 'B2', 'E3', 'C1', 'F5'), …
Ray
  • 321
  • 2
  • 12
0
votes
0 answers

Patterns and View patterns: am I doing this right

I want to achieve {-# LANGUAGE PatternSynonyms, ViewPatterns #-} pattern Just2 :: (Num a, Eq a) => a -> a -> Maybe (a, a) -- Just2 0 0 ≡ Nothing -- Just2 x y ≡ Just (x, y) I think I've got there via a View pattern, but the code seems verbose.…
AntC
  • 2,623
  • 1
  • 13
  • 20