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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:18:34+00:00 2026-06-08T20:18:34+00:00

I want to create a map at compile time that maps compile time keys

  • 0

I want to create a map at compile time that maps compile time keys to run time values. Functions should be able to check for those keys and throw a compiler error if all not all of the required keys are present.

This is roughly what I’d like to achieve:

class HasFirst a
  first :: String

class HasMiddle a
  middle :: String

class HasLast a
  last :: String

print_full_name :: (HasFirst a, HasLast a) => a -> String

addFirst :: String -> a -> b
addFirst s x = -- ( Add a first name to x )

emptyName :: -- some empty name

x1 = addFirst "John" $ addLast "Smith" $ emptyName
x2 = addMiddle "Bob" $ addLast "Smith" $ emptyName

main = putStr $ print_full_name x1 -- Compiles
main = putStr $ print_full_name x2 -- No first name so compile error

For those with C++ knowledge, I want roughly what boost::fusion::map does.

I don’t need exactly the code above, what is important that I can check parameters at compile time. Whether the mechanism is classes or something else I don’t mind.

Is there a package that does this or is it easy to develop this?

  • 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-08T20:18:36+00:00Added an answer on June 8, 2026 at 8:18 pm

    Let me tell you how you can get your example to work using a functional design technique called phantom types:

    {-# LANGUAGE EmptyDataDecls #-}
    
    data YES
    data NO
    
    data Name first middle last = Name String String String
    
    emptyName :: Name NO NO NO
    emptyName = Name "" "" ""
    
    addFirst :: String -> Name first middle last -> Name YES middle last
    addFirst s (Name a b c) = Name s b c
    
    addMiddle :: String -> Name first middle last -> Name first YES last
    addMiddle s (Name a b c) = Name a s c
    
    addLast :: String -> Name first middle last -> Name first middle YES
    addLast s (Name a b c) = Name a b s
    
    printFullName :: Name YES middle YES -> String
    printFullName (Name a b c) = a++b++c
    

    We use the three type parameters of Name to track which names have been set. printFullName only accepts names with first and last names. We get a type error when trying to print underdefined names:

    *Main> printFullName $ addFirst "John" $ addLast "Smith" $ emptyName
    "JohnSmith"
    *Main> printFullName $ addFirst "John" $ addMiddle "Edward" $ emptyName
    
    <interactive>:1:16:
        Couldn't match expected type `YES' against inferred type `NO'
          Expected type: Name YES YES YES
          Inferred type: Name YES YES NO
        In the second argument of `($)', namely
            `addFirst "John" $ addMiddle "Edward" $ emptyName'
        In the expression:
              printFullName $ addFirst "John" $ addMiddle "Edward" $ emptyName
    

    Note that this is a very crude encoding, but hopefully it demonstrates the power of phantom types.

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

Sidebar

Related Questions

I want to create a Map object that can lose value only if we
I am using oozie to run my map-reduce job. I want to create the
i want to create something like this. open map application(means maps) then when maps
i'm using google maps v3 and i want create a new map associate to
I want to create a class (static?) that simply maps a name to a
I want to create a map that will provide the benefits of generics, whilst
I want to create a map, that uses iterators as key-type and integers as
I want to create application which would display spatial data on Map. I'm thinking
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that

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.