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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:14:15+00:00 2026-06-14T15:14:15+00:00

I have simple tuples (e.g. read from a DB) from that I do not

  • 0

I have simple tuples (e.g. read from a DB) from that I do not know the number of elements nor the content. E.g.
(String, Int, Int) or (String, Float, String, Int).

I want to write a generic function that would take all sort of tuples and replace all data with the string “NIL”. If the string “NIL” is already present it should stay untouched.

Coming back to the example:
("something", 3, 4.788) should result in ("something", "NIL", "NIL")

("something else", "Hello", "NIL", (4,6)) should result in ("something else", "NIL", "NIL", "NIL")

I have obviously no idea where to start since it won’t be a problem to do this with tuples that are known. Is it possible here to come to my desired result without Template Haskell?

  • 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-14T15:14:16+00:00Added an answer on June 14, 2026 at 3:14 pm

    It’s possible using GHC.Generics, I thought I’d document it here for completeness though I wouldn’t recommend it over the other recommendations here.

    The idea is to convert your tuples into something that can be pattern matched on. The typical way (which I believe HList uses) is to convert from a n-tuple to nested tuples: (,,,) -> (,(,(,))).

    GHC.Generics does something similar by converting the tuples to nested applications of the product :*: constructor. to and from are functions that convert a value to and from their generic representation. The tuple fields are generically represented by K1 newtypes, so what we want to do is recurse down through the tree of metadata (M1) and product (:*:) nodes until we find the K1 leaf nodes (the constants) and replace their contents with a “NIL” string.

    The Rewrite type function describes how we’re modifying the types. Rewrite (K1 i c) = K1 i String states that we’re going to replace each value (the c type parameter) with a String.

    Given a little test app:

    y0 :: (String, Int, Double)
    y0 = ("something", 3, 4.788)
    
    y1 :: (String, String, String, (Int, Int))
    y1 = ("something else", "Hello", "NIL", (4,6))
    
    main :: IO ()
    main = do
      print (rewrite_ y0 :: (String, String, String))
      print (rewrite_ y1 :: (String, String, String, String))
    

    We can use a generic rewriter to produce:

    *Main> :main
    ("something","NIL","NIL")
    ("something else","NIL","NIL","NIL")
    

    Using the built-in Generics functionality and a typeclass to do the actual transformation:

    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE TypeOperators #-}
    
    import Data.Typeable
    import GHC.Generics
    
    rewrite_
      :: (Generic a, Generic b, Rewriter (Rep a), Rewrite (Rep a) ~ Rep b)
      => a -> b
    rewrite_ = to . rewrite False . from
    
    class Rewriter f where
      type Rewrite f :: * -> *
      rewrite :: Bool -> f a -> (Rewrite f) a
    
    instance Rewriter f => Rewriter (M1 i c f) where
      type Rewrite (M1 i c f) = M1 i c (Rewrite f)
      rewrite x = M1 . rewrite x . unM1
    
    instance Typeable c => Rewriter (K1 i c) where
      type Rewrite (K1 i c) = K1 i String
      rewrite False (K1 x) | Just val <- cast x = K1 val
      rewrite _ _ = K1 "NIL"
    
    instance (Rewriter a, Rewriter b) => Rewriter (a :*: b) where
      type Rewrite (a :*: b) = Rewrite a :*: Rewrite b
      rewrite x (a :*: b) = rewrite x a :*: rewrite True b
    

    And a few instances unused by this example, they’d be required for other data types:

    instance Rewriter U1 where
      type Rewrite U1 = U1
      rewrite _ U1 = U1
    
    instance (Rewriter a, Rewriter b) => Rewriter (a :+: b) where
      type Rewrite (a :+: b) = Rewrite a :+: Rewrite b
      rewrite x (L1 a) = L1 (rewrite x a)
      rewrite x (R1 b) = R1 (rewrite x b)
    

    With a bit more effort the Typeable constraint could be removed from the K1 instance, whether it’s better or not is arguable due to Overlapping/UndecidableInstances. GHC also can’t infer the result type, though it’s seems like it should be able to. In any case, the result type needs to be correct or you’ll get a hard to read error message.

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

Sidebar

Related Questions

I am developing a simple class that maps any Tuples from database, by convention,
I have simple class with width and height member fields which define number of
I have simple php validation form that is halfway working. If you leave the
I have simple win service, that executes few tasks periodically. How should I pass
I have read about the Tuples provided with the coming-out of the new .NET
I wrote a simple Tkinter based Python application that reads text from a serial
I have simple question regarding SQLAlchemy, is it possible to get the rows from
Not sure if I have a simple typo somewhere, but I'm running into issues
I have string of tuples LL = [ (text1,2,3,N/A), (text2,N/A,5,6),(text3,N/A,5,N/A) ] I need LL
I have a simple function call takes two tuples. Getting compiler error on type:

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.