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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:44:44+00:00 2026-06-17T03:44:44+00:00

I’m trying to build a list at the type level, but I’m having some

  • 0

I’m trying to build a list at the type level, but I’m having some trouble figuring out how to enforce constraints.

My base code is:

data Foo z q = Foo1 (z q)
             | Foo2 (z q)

class Qux q -- where ...
class Baz z -- where ...

class Bar a where             -- a has kind *->*
  type BCtx a q :: Constraint -- using ConstraintKinds to allow constraints on the concrete type
  f :: (BCtx a q) => a q -> a q -> a q
  g :: (BCtx a q, BCtx a q') => a q -> a q'

instance (Baz z) => Bar (Foo z) where
  type BCtx (Foo z) q = (Num (z q), Qux q) -- for example
  f (Foo1 x) (Foo1 y) = Foo1 $ x+y -- these functions need access to the type q to do arithmetic mod q
  f (Foo1 x) (Foo2 y) = Foo2 $ x-y
  -- ...

You can think of the qs above representing prime powers. I would also like to represent composite numbers using a type list of qis. I’m imagining something like:

data QList qi qs = QCons qi qs
                 | QNil

with the data

data FList c q = FNil 
               | FCons (c (Head q)) (FList c (Tail q))

where (Head q) should correspond to qi and (Tail q) should correspond to qs. Note that the q parameter for FList is NOT (necessarily) a (Qux q), it is a list of (Qux qi). (I don’t want to flesh out anything more about this list, since it’s one of the design problems I’m posing). I would like to work “modulus-wise” on the FList:

instance (Bar c) => Bar (FList c) where
   type BCtx (FList c) q = () -- Anything I put here is not enough
   f (FCons x xs) (FCons y ys) = FCons (f x y) (f xs ys)
   -- the left call to `f` calls a concrete instance, the right call to `f` is a recursive call on the rest of the list
   -- ...

Compiling these codes snippets together in GHC result in (modulo transcription, abstraction, and typing errors):

Could not deduce (BCtx c (Head q), BCtx c (Tail q))

and then

Could not deduce (BCtx c (Head (Tail q)), BCtx c (Tail (Tail q)))

etc.

I see why I’m getting this error, but not how to fix it.

Concretely, I’m expecting an FList c q type where c~Foo z and q~QCons q1 (QCons q2 QNil), and of course my list will satisfy all of the BCtx constraints at every level.

I’m not sure that fixing those particular errors will result in compiling code, but it is a start. The entire Bar class is basically fixed (the Constraint kind is required, and the instances of Bar must have kind * -> *). I don’t believe I can use existential types to create a list of generic objects because I need access to the qi parameter. I am willing to change the type of FList and QList to allow me to work modulus-wise on a collection of Bars.

Thanks for your time!

  • 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-17T03:44:45+00:00Added an answer on June 17, 2026 at 3:44 am

    To handle type lists, it’s necessary to discriminate empty from nonempty lists and handle them separately. The ‘Could not deduce’ errors in your code occur because your instance assumes a nonempty list, when in fact the list may or may not be empty. Here is a solution using the extensions TypeFamilies, TypeOperators, DataKinds, and GADTs.

    With DataKinds, type lists are predefined. They have kind [*], but they’ll be used in a context where kind * is expected, so an operator is needed to cast them:

    data InjList (qs :: [*])
    

    Using type lists, FList is defined as

    data FList c q where
      FNil :: FList c (InjList '[])
      FCons :: c h -> FList c (InjList t) -> FList c (InjList (h ': t))
    

    It’s defined as a GADT to express how it’s only possible to construct FLists over the type InjList q' for some type-list q'. For instance, the term FCons [True] FNil has type FList [] (InjList (Bool ': '[])). On the other hand, since Bool isn’t of the form InjList q', there are no terms (except ⊥) of type FList [] Bool. By pattern matching on an FList, a function can verify that it has been given a non-⊥ argument, and further determine whether it’s been passed an empty type list.

    An instance of Bar for FLists has to handle nil lists and cons lists separately. A nil list has an empty context.
    A cons list has components for the head and tail of the list. This is expressed by pattern matching on the type-list in the associated type instance of BCtx. The function f examines its argument to verify that it’s not ⊥ and to decide whether it’s an empty list.

    instance (Bar c) => Bar (FList c) where
      -- Empty context for '[]
      type BCtx (FList c) (InjList '[]) = ()
      -- Context includes components for head and tail of list
      type BCtx (FList c) (InjList (h ': t)) = (BCtx c h, BCtx (FList c) (InjList t))
    
      f FNil FNil = FNil
      f (FCons x xs) (FCons y ys) = FCons (f x y) (f xs ys)
    

    We can load the code into GHCi to verify that it works:

    instance Bar [] where
      type BCtx [] q = Num q
      f xs ys = zipWith (+) xs ys
    
    instance Show (FList c (InjList '[])) where
      show FNil = "FNil"
    
    instance (Show (c h), Show (FList c (InjList t))) => Show (FList c (InjList (h ': t))) where
      show (FCons h t) = "FCons (" ++ show h ++ ") (" ++ show t ++ ")"
    
    $ ghci
    
    > :load Test
    > f (FCons [1,2] FNil) (FCons [3,4] FNil)
    FCons ([4,6]) (FNil)
    
    • 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
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
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

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.