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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:38:59+00:00 2026-05-30T04:38:59+00:00

We can use pair sequences to create heterogenous lists in Haskell: type a *:

  • 0

We can use pair sequences to create heterogenous lists in Haskell:

type a *: b = (a, b)
a *: b = (a, b)
infixr 5 *:

hlist :: Int *: String *: Maybe Float *: ()
hlist = 1 *: "hello" *: Just 3 *: () -- (1, ("hello", (Just 3, ())))

Is there a way we can do type-level filtering on these lists? That is, define some polymorphic function hfilter such that for distinct types a, b, and c:

hfilter :: a *: b *: c *: a *: b *: a *: () ->  a *: a *: a *: ()
hfilter :: a *: b *: c *: a *: b *: a *: () ->  b *: b *: ()
hfilter :: a *: b *: c *: a *: b *: a *: () ->  c *: ()
hfilter :: a *: b *: c *: a *: b *: a *: () ->  ()
  • 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-30T04:39:01+00:00Added an answer on May 30, 2026 at 4:39 am

    It’s possible with a few type extensions (as an aside, please check that your example code compiles when posting questions. I had to make quite a few corrections).

    {-# LANGUAGE TypeOperators #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE TypeSynonymInstances #-}
    {-# LANGUAGE OverlappingInstances #-}
    
    type a :* b = (a, b)
    a *: b = (a, b)
    infixr 5 *:
    infixr 5 :*
    
    hlist :: Int :* String :* Int :* Maybe Float :* ()
    hlist = 1 *: "hello" *: 2 *: Just 3 *: ()
    
    
    class TypeFilter lst t where
        hfilter :: lst -> [t]
    
    instance TypeFilter () t where
        hfilter _ = []
    
    instance TypeFilter rest t => TypeFilter (t :* rest) t where
        hfilter (a, rest) = a : hfilter rest
    
    instance TypeFilter rest t => TypeFilter (a :* rest) t where
        hfilter (_, rest) = hfilter rest
    

    Now we can filter items by type by explicitly defining the type of the list we want.

    *Main> hfilter hlist :: [Int]
    [1,2]
    *Main> hfilter hlist :: [String]
    ["hello"]
    *Main> hfilter hlist :: [Maybe Float]
    [Just 3.0]
    *Main> hfilter hlist :: [Maybe Int]
    []
    

    It works by defining a multi-parameter type-class TypeFilter, which takes the type of the heterogenous list and the type we want to filter by. We then define instances for the empty list/unit () and for a list where the type matches (TypeFilter (t :* rest) t) and finally for a list where the type of the head is different than the type we want to retrieve (TypeFilter (a :* rest) t).

    Note that in the last instance there is currently no way to signify that a and t must be different types, but when they are the same OverlappingInstances counts the instance TypeFilter (t :* rest) t as more specific and chooses it over the TypeFilter (a :* rest) t one.

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

Sidebar

Related Questions

If I have sequence of sequences (maybe a list of tuples) I can use
You can use SelectFolder() to get a folder or GetOpenFolderitem(filter as string) to get
I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
I think I can use Cantor's to create a unique hash n = ((x
I am looking to create a client/server application that I can use to slit
To match string, which contains some word, I can use pattern /.*word.*/. But how
You can use more than one css class in an HTML tag in current
You can use a standard dot notation or a method call in Objective-C to
I can use properties of an Excel Worksheet to tell if the worksheet is
You can use ftplib for full FTP support in Python. However the preferred way

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.