maxloo
maxloo

Reputation: 491

Haskell - Pattern Synonyms, GADT, pattern matching

Based on the answer for the second problem I face at:

Haskell - Pattern Synonyms, View Patterns, GADTs

{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE GADTs #-}
data T where
  MkT :: (Show b) => b -> T
pattern ExNumPat :: () => Show b => b -> T
pattern ExNumPat x = MkT x
test11 = ExNumPat "True"

The answerer says "pattern-match the b back out of it and just show that" as one of the ways to resolve my error. Could it be further explained? How do I do that? I've tried adding the following, besides other variations:

{-# LANGUAGE ScopedTypeVariables #-}
testExNumPat :: (forall b. Show b => (b -> T) -> T)
testExNumPat (ExNumPat (1::Int)) = MkT 1
test12 = testExNumPat (ExNumPat 1)

But I always ended up with many more errors. For this particular case, the errors are:

• Couldn't match expected type ‘b -> T’ with actual type ‘T’
• In the pattern: ExNumPat (1 :: Int)

• Couldn't match expected type ‘b3’ with actual type ‘Int’
  ‘b3’ is a rigid type variable bound by
    a pattern with pattern synonym:
      ExNumPat :: () => forall b. Show b => b -> T

• Couldn't match expected type ‘b1 -> T’ with actual type ‘T’
• Possible cause: ‘ExNumPat’ is applied to too many arguments

Upvotes: 1

Views: 234

Answers (1)

Noughtmare
Noughtmare

Reputation: 10645

There are two main issues here: as the error message says, your type for testExNumPat is wrong, I think it should be testExNumPat :: T -> T; and secondly you match on an Int in the ExNumPat, but the contained value is existentially quantified, so you can't know that this value will always be an Int. To show the contained value you can simply call show on it:

testExNumPat :: T -> String
testExNumPat (ExNumPat x) = show x

test12 = testExNumPat (ExNumPat 1)

Upvotes: 1

Related Questions