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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:26:31+00:00 2026-05-28T00:26:31+00:00

I want to implement the following stripPrefixBy function: — psuedo code signature stripPrefixBy ::

  • 0

I want to implement the following stripPrefixBy function:

-- psuedo code signature
stripPrefixBy :: forall a. [forall b. a -> Maybe b] -> [a] -> Maybe [a]
stripPrefixBy [] xs = Just xs
stripPrefixBy _ [] = Nothing
stripPrefixBy (p:ps) (x:xs) = case p x of
  Just _ -> stripPrefixBy ps xs
  Nothing -> Nothing

res :: Maybe String
res = stripPrefixBy [const (Just 0), Just] "abc"

wantThisToBeTrue :: Bool
wantThisToBeTrue = case res of
  Just "c" -> True
  _ -> False

I’ve tried using ImpredicativeTypes and RankNTypes but without luck. How can I implement stripPrefixBy with the type I want it to have?

  • 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-28T00:26:32+00:00Added an answer on May 28, 2026 at 12:26 am

    The problem with your signature is that the list passed to stripPrefixBy is declared as a list of functions which take a certain a as an argument, and then produce a Maybe b for any b the caller picks. The only values the functions in the list are allowed to return are ⊥, Nothing and Just ⊥.

    That is to say, when using impredicative polymorphism, the forall doesn’t mean the same thing it does with an existentially quantified type: there, the forall is applying to the type of the constructor, i.e.

    data MyType = forall a. Foo a
    Foo :: forall a. a -> MyType
    

    but here, it’s saying that the function must literally be of type forall b. a -> Maybe b.

    Here’s a corrected example using an existential type:

    {-# LANGUAGE ExistentialQuantification #-}
    
    data Pred a = forall b. Pred (a -> Maybe b)
    
    stripPrefixBy :: [Pred a] -> [a] -> Maybe [a]
    stripPrefixBy [] xs = Just xs
    stripPrefixBy _ [] = Nothing
    stripPrefixBy (Pred p:ps) (x:xs) = case p x of
      Just _ -> stripPrefixBy ps xs
      Nothing -> Nothing
    
    res :: Maybe String
    res = stripPrefixBy [Pred $ const (Just 0), Pred Just] "abc"
    
    wantThisToBeTrue :: Bool
    wantThisToBeTrue = case res of
      Just "c" -> True
      _ -> False
    

    I believe that UHC supports expressing the type you want directly, as

    stripPrefixBy :: [exists b. a -> Maybe b] -> [a] -> Maybe [a]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement the following layout in code: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/widget92 android:layout_width=fill_parent android:layout_height=fill_parent
I'm trying to implement the following feature: I want just one css file to
If I want to implement the following code, would enums be appropriate? I've looked
I want to implement the following constraints in mysql: create table TypeMapping( ... constraint
I want to implement an atomic transaction like the following: BEGIN TRAN A SELECT
i want to implement the following behaviour: if the users browser-language is e.g. EN
I want to implement the following scenario: When a user presses the Home key
I want to implement the following combobox in ExtJS. The question is, how to
I want to implement the following page by Asp.net version 4.0 and ajax but
I want to implement a in-app chat view like the following pic. http://img408.imageshack.us/img408/4057/img0139.png How

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.