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 7989705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:49:35+00:00 2026-06-04T12:49:35+00:00

So, I’m working on a fun experiment in contextual meaning and I’m running into

  • 0

So, I’m working on a fun experiment in contextual meaning and I’m running into a wall. I’m trying to define a data type that can either be a primitive or a function that transforms from one constructor to another.

data WeaponPart =
    WInt Int |
    WHash (Map.Map String Int) |
    WNull |
    WTrans (WeaponPart -> WeaponPart)

instance Show WeaponPart where
    show (WInt x) = "WInt " ++ (show x)
    show (WHash x) = "WHash " ++ (show x)
    show (WTrans _) = "WTrans"
    show WNull = "WNull"

cold :: WeaponPart -> WeaponPart
cold (WInt x) = WHash (Map.singleton "frost" x)
cold (WHash x) = WHash $ Map.insertWith (+) "frost" 5 x
cold (WTrans x) = cold $ x (WInt 5)
cold (WNull) = cold $ (WInt 5)

ofTheAbyss :: WeaponPart -> WeaponPart
ofTheAbyss (WTrans x) = x (WTrans x)

The problems is that the signature for ofTheAbyss allows any WeaponPart as an argument, whereas I only want to allow WTrans-constructred arguments. You can see I’ve only written a pattern match for that case.

I’ve tried doing with with GADTs but I fear it was a rabbit hole. Could never really get them to do what I wanted. Does anyone have any ideas how I could enforce only WTrans arguments into ofTheAbyss? Or am I just completely missing something.

Thanks.

Best,
Erik

  • 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-06-04T12:49:36+00:00Added an answer on June 4, 2026 at 12:49 pm

    You can do this sort of thing with GADTs. Far be it from me to judge whether what results is a rabbit hole, but let me at least show the recipe. I’m using the new PolyKinds extension, but you can manage with less.

    First, decide what sorts of stuff you will need, and define a datatype of those sorts.

    data Sort = Base | Compound
    

    Next, define your data indexed by their sorts. It’s like building a little typed language.

    data WeaponPart :: Sort -> * where
      WInt    :: Int ->                                   WeaponPart Base
      WHash   :: Map.Map String Int ->                    WeaponPart Base
      WNull   ::                                          WeaponPart Base
      WTrans  :: (Some WeaponPart -> Some WeaponPart) ->  WeaponPart Compound
    

    You can represent ‘data of any sort’ via existential quantification, as follows:

    data Some p where
      Wit :: p x -> Some p
    

    Note that the x does not escape, but we can still inspect the ‘evidence’ that x ‘satisfies’ p. Note that Some must be a datatype, not a newtype as GHC objects to existential newtypes.

    You are now free to write Sort-generic operations. If you have generic inputs, you can just use polymorphism, effectively currying Some p -> ... as forall x. p x -> ....

    instance Show (WeaponPart x) where
      show (WInt x)    = "WInt " ++ (show x)
      show (WHash x)   = "WHash " ++ (show x)
      show (WTrans _)  = "WTrans"
      show WNull       = "WNull"
    

    The existential is needed for Sort-generic outputs: here I use it for input and output.

    cold :: Some WeaponPart -> Some WeaponPart
    cold (Wit (WInt x))    = Wit (WHash (Map.singleton "frost" x))
    cold (Wit (WHash x))   = Wit (WHash $ Map.insertWith (+) "frost" 5 x)
    cold (Wit (WTrans x))  = cold $ x (Wit (WInt 5))
    cold (Wit WNull)       = cold $ Wit (WInt 5)
    

    I had to add the occasional touch of Wit about the place, but it’s the same program.

    Meanwhile, we can now write

    ofTheAbyss :: WeaponPart Compound -> Some WeaponPart
    ofTheAbyss (WTrans x) = x (Wit (WTrans x))
    

    So it’s not horrendous to work with embedded type systems. Sometimes there is a cost: if you want your embedded language to have subsorting, you may find you do extra computation just to change the index of some data’s type, making no difference to the data themselves. If you don’t need subsorting, the extra discipline can often be a real friend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.