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
28
votes
5 answers

Haskell Bytestrings: How to pattern match?

I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString. The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' &&…
LOS
  • 523
  • 4
  • 6
16
votes
1 answer

Pattern synonyms lead to unexhaustive pattern matching

I've managed to construct the following "minimal" example that shows my problem. Provided the PatternSynonyms extension is enabled data Vec = Vec Int Int pattern Ve x y = Vec x y f :: (Vec, Vec) -> Vec f (v@(Ve a b), Ve c d) | a > b = Vec…
Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
12
votes
2 answers

Pattern matching Data.Sequence like lists

I am using Data.Sequence instead lists for better performance. With lists we can do the following foo :: [Int] -> Int foo [] m = m foo (x:xs) m = ... How can this be accomplished with Data.Sequence. I have tried the following: foo:: S.Seq Int ->…
abden003
  • 1,325
  • 7
  • 24
  • 48
12
votes
1 answer

A list whose "Nil" carries a value?

Does some standard Haskell library define a data type like this data ListWithEnd e a = Cons a (ListWithEnd e a) | End e That is a list whose terminating element carries a value of a designated type? So ListWithEnd () is…
Petr
  • 62,528
  • 13
  • 153
  • 317
11
votes
1 answer

Haskell: "Qualified name in binding position" error with Map.empty

I'm trying to create a pattern synonym for a newtype with an empty map. {-# Language PatternSynonyms #-} import qualified Data.Map as Map newtype StoreEnv = StoreEnv (Map.Map Int String) deriving (Eq, Show) pattern EmptyStore ::…
Tien Ho
  • 123
  • 6
11
votes
2 answers

Pattern matching on a private data constructor

I'm writing a simple ADT for grid axis. In my application grid may be either regular (with constant step between coordinates), or irregular (otherwise). Of course, the regular grid is just a special case of irregular one, but it may worth to…
firegurafiku
  • 3,017
  • 1
  • 28
  • 37
10
votes
1 answer

Combining patterns

Consider the following data type and pattern synonyms: {-# LANGUAGE PatternSynonyms, NamedFieldPuns #-} data Foo = Foo { a :: Int , b :: String , c :: Maybe Bool } pattern Bar a b <- Foo { a, b } pattern Baz c <- Foo { c } I'd like…
wrl
  • 131
  • 4
8
votes
1 answer

Why does my pattern block an error on both sides?

To start off this whole thing I'm working with a pattern synonym defined as follows: {-# Language PatternSynonyms #-} pattern x := y <- x @ y This allows me to run multiple pattern matches across a parameter at once. A regular as binding (@) does…
Wheat Wizard
  • 3,982
  • 14
  • 34
8
votes
1 answer

Pattern synonym can't unify types within type-level list

I'm getting an error when trying to define a pattern synonym based on a GADT that has a type-level list. I managed to boil it down to this example: {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE…
rampion
  • 87,131
  • 49
  • 199
  • 315
7
votes
1 answer

Writing a COMPLETE pragma for a polymorphic pattern synonym?

I have the following code and I don't know what should feed at ??. Or cannot polymorphic patterns make complete? {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE ViewPatterns #-} module Data.Tuple.Single.Class ( Single (..) , pattern Single )…
Kazuki Okamoto
  • 661
  • 5
  • 10
6
votes
3 answers

Haskell pattern matching on vectors

Is it possible to use list style pattern matching on vectors? ie import qualified Data.Vector as V f :: V.Vector a -> a f (x:xs) = x gives an error
matt
  • 1,817
  • 14
  • 35
6
votes
1 answer

How to Pattern Match an Empty Vector in Haskell?

Say I want to implement the length function for lists using pattern matching, then I could do something like this: length' :: (Num b) => [a] -> b length' [] = 0 length' (_:xs) = 1 + length' xs Can I do something similar with Vectors?
frmsaul
  • 1,366
  • 2
  • 12
  • 19
6
votes
1 answer

Why are all recursive pattern synonyms rejected?

{-# LANGUAGE PatternSynonyms, ViewPatterns #-} data Quun = Foo | Bar | Oink Quun fooey :: Quun -> Bool fooey Foo = True fooey (Oink Yum) = True fooey _ = False pattern Yum <- (fooey -> True) This doesn't compile (at least in…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
5
votes
0 answers

Explicit type variable instantiation for pattern synonyms?

Suppose, I have the following code data F a where F :: Typeable a => F a asType :: forall b a. Typeable b => F a -> Maybe (a :~: b, F b) asType e@F{} = case eqT @b @a of Just Refl -> Just (Refl, e) Nothing -> Nothing pattern AsType :: forall…
grepcake
  • 3,960
  • 3
  • 16
  • 26
5
votes
1 answer

No-fun/puzzlement with pattern synonyms as functions

With PatternSynonyms (explicitly bidirectional form), the pattern-to-expr equations in effect form a function but spelled upper-case (providing you end up with an fully saturated data constr of the correct type). Then consider (at GHC 8.10.2) {-#…
AntC
  • 2,623
  • 1
  • 13
  • 20
1
2 3 4