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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:06:24+00:00 2026-05-27T02:06:24+00:00

I am struggling with existential types in my program. I think I’m trying to

  • 0

I am struggling with existential types in my program. I think I’m trying to do something very reasonable however I cannot get past the typechecker 🙁

I have a datatype that sort of mimics a Monad

data M o = R o | forall o1. B (o1 -> M o) (M o1)

Now I create a Context for it, similar to that described in Haskell Wiki article on Zipper, however I use a function instead of a data structure for simplicity –

type C o1 o2 = M o1 -> M o2

Now when I try to write a function that splits a data value into its context and subvalue, the typechecker complains –

ctx :: M o -> (M o1 -> M o, M o1)
ctx (B f m) = (B f, m) -- Doesn't typecheck

Error is –

Couldn't match type `o2' with `o1'
  `o2' is a rigid type variable bound by
       a pattern with constructor
         B :: forall o o1. (o1 -> M o) -> M o1 -> M o,
       in an equation for `ctx'
       at delme1.hs:6:6
  `o1' is a rigid type variable bound by
       the type signature for ctx :: M o -> (M o1 -> M o, M o1)
       at delme1.hs:6:1
Expected type: M o2
  Actual type: M o1
In the expression: m
In the expression: (B f, m)

However, I can work around it like so –

ctx (B f m) = let (c,m') = ctx m in ((B f) . c, m') -- OK

Why does this second definition typecheck but not the first one?

Also, if I try to convert ctx to a complete function by checking for R, I again get a typecheck error –

ctx (R o) = (id, R o) -- Doesn't typecheck

Error –

Couldn't match type `o' with `o1'
  `o' is a rigid type variable bound by
      the type signature for ctx :: M o -> (M o1 -> M o, M o1)
      at delme1.hs:7:1
  `o1' is a rigid type variable bound by
       the type signature for ctx :: M o -> (M o1 -> M o, M o1)
       at delme1.hs:7:1
In the first argument of `R', namely `o'
In the expression: R o
In the expression: (id, R o)

How can I work around this error?

Any help is appreciated!

  • 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-27T02:06:24+00:00Added an answer on May 27, 2026 at 2:06 am

    Let’s look at the failing cases first. Both of these fail for the same reason, which is clearer once you add in the implicit forall in the type signature:

    ctx :: forall o o1. M o -> (M o1 -> M o, M o1)
    

    i.e. your function must not only work for a some o1, but for any o1.

    1. In your first case,

      ctx (B f m) = (B f, m)
      

      we know that f :: (o2 -> M o) and m :: M o2, for some type o2, but we have to be able to offer any type o1, so we can’t assume that o1 ~ o2.

    2. In the second case,

      ctx (R o) = (id, R o)
      

      Here, we know that o :: o, but again, the function has to be defined for any o1, so we can’t assume that o ~ o1.

    Your workaround only seems to work because it’s calling itself, similar to an inductive proof. But without a base case, it’s just circular reasoning, and you cannot write the base case for this function, because there is no way to construct an M o1 from an M o for any combination of o and o1 without using a bottom value.

    What you’ll probably need to do, is to define another existential type for the context, instead of using just a tuple. Not sure if it’ll work for your needs, but this compiles1, at least:

    data Ctx o = forall o1. Ctx (M o1 -> M o) (M o1)
    
    ctx :: M o -> Ctx o
    ctx (B f m) = case ctx m of Ctx c m' -> Ctx (B f . c) m'
    ctx (R o)   = Ctx id (R o) 
    

    1 Try using a let instead of case for a funny GHC error 🙂

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

Sidebar

Related Questions

I'm struggling trying to read a php file inside a php and do some
really struggling to resolve this issue. using nhibernate Im trying to join two different
I struggling with a problem I have in TSQL, I need to get the
Struggling here... I am trying to have views displayed during specific periods in a
struggling to get my head round this. i want to convert the following sql
Been struggling for the past two days to change the background of my widget,
Im struggling to solve in a very clean way a problematic involving memory overload
Struggling to get my head around these MD Arrays so hoping someone can help.
Struggling to get nested repeaters working although I feel I am close! I am
struggling a bit with something. I have built a proof of concept and googled

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.