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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:18:43+00:00 2026-05-29T12:18:43+00:00

I am writing a simple HashString class, which is just a string and its

  • 0

I am writing a simple HashString class, which is just a string and its hash:

data HashString = HashString Int    -- ^ hash
                             T.Text -- ^ string!

Now I’m trying to generate these at compile time with something like:

$(hString "hello, world") :: HashString

I want the hash, and the text packing to happen at compile time. How do I do this?

Here’s what I’ve tried so far, but I’m not sure if its right, nor am I sure it does everything at compile time:

hString :: String -> Q Exp
hString s = [| HashString (hash $ T.pack s) (T.pack s) |]
  • 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-29T12:18:44+00:00Added an answer on May 29, 2026 at 12:18 pm

    The way you’ve written your code, no evaluation will happen at compile-time. When you quote a Haskell expression with [| ... |], the quoted code/AST is inserted where you apply it without any evaluation, so writing:

    $(hString "hello, world")
    

    is exactly the same as writing:

    let s = "hello, world" in HashString (hash $ T.pack s) (T.pack s)
    

    But think about it like this: you use [| ... |] to quote an expression to be inserted later, and you generate code at compile-time with $(...). So, if you include some code $(foo) in a quoted expression bla = [| bar $(foo) |], doing $(bla) will generate the code bar $(foo), which in turn will evaluate foo at compile time. Also, to take a value that you generate at compile time and generate an expression from it, you use the lift function. So, what you want to do is this:

    import Data.String (fromString)
    import Language.Haskell.TH.Syntax
    
    hString s = [| HashString $(lift . hash . T.pack $ s) (fromString s) |]
    

    This evaluates the hash function at compile time, since the inner splice is resolved after the outer splice was resolved. By the way, using fromString from Data.String is the generic way of constructing some OverloadedString data type from a String.

    Also, you should consider making a quasi-quoter for your HashString interface. Using quasi-quoters is more natural than manually calling splice functions (And you’ve already used them; the nameless [| ... |] quoter quotes Haskell expressions).

    You would create a quasiquoter like this:

    import Language.Haskell.TH.Quote
    
    hstr =
      QuasiQuoter
      { quoteExp = hString -- Convenient: You already have this function
      , quotePat = undefined
      , quoteType = undefined
      , quoteDec = undefined
      }
    

    This would let you write HashStrings with this syntax:

    {-# LANGUAGE QuasiQuotes #-}
    myHashString = [hstr|hello, world|]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple webservice which takes a String as input
I'm writing simple server/client and trying to get client IP address and save it
I'm learning Rails by writing simple TODO tasks aplication. Two models are: class List
I'm writing a simple I/O class in order to use at a multiplayer game.
I am writing simple webapp in Java (for educational purpuses only) which allows admin
I'm writing simple function witch will read data from myfile.txt with fgets(). Content of
Am writing a simple application which can write a to pdf,doc,xls and access files.
I am writing simple program for my Ubuntu 10.04. I want to see which
Hello I'm writing simple j2me calculator. using GameCanvas class as a basis of my
I'm practising with C, writing simple programs. The little program below should just get

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.