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

  • SEARCH
  • Home
  • 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 6825491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:02:02+00:00 2026-05-26T22:02:02+00:00

A vanilla data type in Haskell has zero or more constructors, each of which

  • 0

A vanilla data type in Haskell has zero or more constructors, each of which plays two roles.

In expressions, it supports introduction, its a function from zero or more arguments to the data type.

In patterns, it supports elimination, its kinda like a function from the data type to Maybe (tuple of argument types).

Is it possible for a module signature to hide the former while exposing the latter?

The use case is this: I have a type, T, whose constructors types alone can sometimes be used to construct nonsense. I have construction functions which can be used to build instances of the type that are guaranteed not to be nonsense. It would make sense to hide the constructors in this case, but it would still be useful for callers to be able to pattern match over the guaranteed-non-nonsense that they build with the construction functions.

I suspect this is impossible, but in case anyone has a way to do it, I though I would ask.

Next best thing is to hide the constructors and create a bunch of functions from T -> Maybe (This, That), T -> Maybe (The, Other, Thing), etc.

  • 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-26T22:02:02+00:00Added an answer on May 26, 2026 at 10:02 pm

    You can use a view type and view patterns to do what you want:

    module ThingModule (Thing, ThingView(..), view) where
    
    data Thing = Foo Thing | Bar Int
    
    data ThingView = FooV Thing | BarV Int
    
    view :: Thing -> ThingView
    view (Foo x) = FooV x
    view (Bar y) = BarV y
    

    Note that ThingView is not a recursive data type: all the value constructors refer back to Thing. So now you can export the value constructors of ThingView and keep Thing abstract.

    Use like this:

    {-# LANGUAGE ViewPatterns #-}
    module Main where
    
    import ThingModule
    
    doSomethingWithThing :: Thing -> Int
    doSomethingWithThing(view -> FooV x) = doSomethingWithThing x
    doSomethingWithThing(view -> BarV y) = y
    

    The arrow notation stuff is GHC’s View Patterns. Note that it requires a language pragma.

    Of course you’re not required to use view patterns, you can just do all the desugaring by hand:

    doSomethingWithThing :: Thing -> Int
    doSomethingWithThing = doIt . view
      where doIt (FooV x) = doSomethingWithThing x
            doIt (BarV y) = y
    

    More

    Actually we can do a little bit better: There is no reason to duplicate all the value constructors for both Thing and ThingView

    module ThingModule (ThingView(..), Thing, view) where
    
       newtype Thing = T {view :: ThingView Thing}
       data ThingView a = Foo a | Bar Int
    

    Continue useing it the same way as before, but now the pattern matches can use Foo and Bar.

    {-# LANGUAGE ViewPatterns #-}
    module Main where
    
    import ThingModule
    
    doSomethingWithThing :: Thing -> Int
    doSomethingWithThing(view -> Foo x) = doSomethingWithThing x
    doSomethingWithThing(view -> Bar y) = y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add an ADO.NET data service to a vanilla ASP.NET-MVC project. Does
Currently I have a custom tool which generates vanilla-SQL (only using standard SQL), from
I have a developed two small Java applications - a vanilla Java app and
An svn repository I'm mirroring through git-svn has changed URL. In vanilla svn you'd
ExtJS provides a fancy combo-box that has lots of features - type ahead, allowing
I have a thunderbird email data file from which I need to extract individual
One of my apps has an initial_data.json file in /fixtures, which works great when
Is there any way efficiently to join JSON data? Suppose we have two JSON
I know that unfortunately detecting and changing programmatically data connection (GPRS/UMTS) on vanilla Android
Plain-vanilla NHibernate setup, eg, no fluent NHibernate, no HQL, nothing except domain objects and

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.