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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:14:58+00:00 2026-05-26T01:14:58+00:00

I’m trying to create the ring Z/n (like normal arithmetic, but modulo some integer).

  • 0

I’m trying to create the ring Z/n (like normal arithmetic, but modulo some integer). An example instance is Z4:

instance Additive.C Z4 where
  zero = Z4 0
  (Z4 x) + (Z4 y) = Z4 $ (x + y) `mod` 4

And so on for the ring. I’d like to be able to quickly generate these things, and I think the way to do it is with template haskell. Ideally I’d like to just go $(makeZ 4) and have it spit out the code for Z4 like I defined above.

I’m having a lot of trouble with this though. When I do genData n = [d| data $n = $n Integer] I get “parse error in data/newtype declaration”. It does work if I don’t use variables though: [d| data Z5 = Z5 Integer |], which must mean that I’m doing something weird with the variables. I’m not sure what though; I tried constructing them via newName and that didn’t seem to work either.

Can anyone help me with what’s going on here?

  • 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-26T01:14:58+00:00Added an answer on May 26, 2026 at 1:14 am

    The Template Haskell documentation lists the things you are allowed to splice.

    A splice can occur in place of

    • an expression; the spliced expression must have type Q Exp
    • an type; the spliced expression must have type Q Typ
    • a list of top-level declarations; the spliced expression must have type Q [Dec]

    In both occurrences of $n, however, you’re trying to splice a name.

    This means you can’t do this using quotations and splices. You’ll have to build declaration using the various combinators available in the Language.Haskell.TH module.

    I think this should be equivalent to what you’re trying to do.

    genData :: Name -> Q [Dec]
    genData n = fmap (:[]) $ dataD (cxt []) n []
                               [normalC n [strictType notStrict [t| Integer |]]] []
    

    Yep, it’s a bit ugly, but there you go. To use this, call it with a fresh name, e.g.

    $(genData (mkName "Z5"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.