Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5850397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:08:06+00:00 2026-05-22T13:08:06+00:00

I’d like to write a function that takes both a value constructor for a

  • 0

I’d like to write a function that takes both

  • a value constructor for a certain algebraic data type, and
  • an actual value of that same type,

and determines whether the given value is “made from” the given constructor. Pattern matching seems like a natural fit for this, but the pattern to match against would have to be a function parameter instead of a hard-coded constructor name.

The code below is what I’ve tried, but GHC reports a parse error on the line indicated.

Is there a way to accomplish this?

data FooBar = Foo Int | Bar String

-- Imagine that these are useful functions.
processInt :: Int -> String
processInt = show
processString :: String -> String
processString = id

-- This should take one of the above functions and adapt it to operate on
-- FooBar values of compatible "type".  Values that match the given FooBar
-- constructor should be "unwrapped" and passed to the given function.
typeCheck :: (a -> FooBar) -> (a -> String) -> (FooBar -> Maybe String)
typeCheck constructor func fooBar = case fooBar of
  (constructor x) -> Just (func x)  -- GHC says "Parse error in pattern: constructor"
  _ -> Nothing

-- Define processing functions that operate on FooBars.
processFoo :: FooBar -> Maybe String
processFoo = typeCheck Foo processInt
processBar :: FooBar -> Maybe String
processBar = typeCheck Bar processString
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T13:08:07+00:00Added an answer on May 22, 2026 at 1:08 pm

    Interesting idea. I wonder what you’re trying to do, since this is quite an unusual pattern matching problem.

    You can certainly do it if you can:

    • enumerate the constructors of the type
    • have equality on the type’s elements

    Like so (I break out the application of f part, since that is orthogonal):

    wasBuilt :: Eq t => (t -> Either t t)   -- ^ the constructor
                     -> Either t t          -- ^ a value
                     -> Maybe t             -- ^ the transformed result
    
    wasBuilt k v = case v of
        Left  x | v == k x    -> Just x
        Right x | v == k x    -> Just x
        _                     -> Nothing
    

    But there’s a lot of boilerplate. This problem screams “generics” at me. Try a different approach, and reflect the constructor to data, then match generically on that data, perhaps. This will allow you to treat the constructors as values, instead of functions.


    Here’s roughly what I was thinking, but note, this is an advanced technique. Explicit pattern matching on a regular AST is much, much more idiomatic:

    import Generics.SYB
    
    -- only works for unary constructors
    sameConstructor :: (Data a, Data b) => (a -> b) -> b -> Bool
    sameConstructor k v = toConstr v == toConstr (k undefined)
    
    > sameConstructor (Left :: Char -> Either Char Char) (Right 'x')
    False
    
    > sameConstructor (Left :: Char -> Either Char Char) (Left 'x')
    True
    
    > sameConstructor (:[]) "haskell"
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.