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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:08:17+00:00 2026-06-01T10:08:17+00:00

I’m looking into using monad comprehensions to represent SQL queries, and generate the appropriate

  • 0

I’m looking into using monad comprehensions to represent SQL queries, and generate the appropriate SQL. At first glance, this is not a problem, it seems a good fit. But I have to restrict the types, which can form the monad to products only, no sums, and I can’t think of a way to place such constraints.

I want to use the type checker to ensure that only types representable in SQL can be used.

I could, I suppose, use template haskell to derive the correct instances, and refuse to derive if the type does not fit, but I would prefer it done in the type level. Less chance for me introducing bugs due to my ignorance.

How could I do this? If yes, could you recommend some reading and/or code examples please.

Edit: Thanks, I’ve got some good paths to follow, which require more reading 🙂 And here comes a long weekend.

  • 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-01T10:08:19+00:00Added an answer on June 1, 2026 at 10:08 am

    Unfortunately, this isn’t really possible: a Monad must be fully polymorphic. It’s the same reason you can’t make Set a monad (the Ord constraint).

    If you can deal with only having the result type meet the constraint, then you could have have runSQL :: (Product a) => SQL a -> IO a, or similar. In that case, just deriving the appropriate instances with Template Haskell is the way to go, or alternately, using the new GHC Generics; plain Haskell has no way to determine whether a type is composed of only products.

    But I suspect you need to access the entire structure of the monadic computation, to translate it to SQL. Unfortunately, monads aren’t very well-equipped to this, since they are plugged together with arbitrary Haskell functions, which you can’t “look inside”. Arrows are closer, and let you do more static analysis, but still have that pesky arr that, again, lets you sneak in arbitrary Haskell functions.

    The most viable option for doing something like this is probably to write a Template Haskell splicer to parse the syntax you want; you can say $(sql [| [ (a,b) | a <- table1, b <- table2 |]) and have sql translate the AST to the corresponding SQL at compile-time If that syntax is too ugly, you write use a quasiquoter with haskell-src-meta, which would look like [sql| (a, b) | a <- table1, b <- table2 |].

    You might also be interested in the generalised arrows extension, although it’s probably overkill (and too experimental) for your purposes.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of 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.