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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:46:28+00:00 2026-06-11T22:46:28+00:00

I’m writing a type class for my pipes library to define an abstract interface

  • 0

I’m writing a type class for my pipes library to define an abstract interface to Proxy-like types. The type class looks something like:

class ProxyC p where
    idT   :: (Monad m) => b' -> p a' a b' b m r
    (<-<) :: (Monad m)
          => (c' -> p b' b c' c m r)
          -> (b' -> p a' a b' b m r)
          -> (c' -> p a' a c' c m r)
    ... -- other methods

I’m also writing extensions for the Proxy type that are of the form:

instance (ProxyC p) => ProxyC (SomeExtension p) where ....

… and I’d like these instances to be able to impose an additional constraint that if m is a Monad then p a' a b' b m is a Monad for all a', a, b', and b.

However, I have no clue how to cleanly encode that as a constraint either for the ProxyC class or for the instances. The only solution I currently know of is to do something like encoding it in the method signatures of the class:

    (<-<) :: (Monad m, Monad (p b' b c' c m), Monad (p a' a b' b m))
          => (c' -> p b' b c' c m r)
          -> (b' -> p a' a b' b m r)
          -> (c' -> p a' a c' c m r)

… but I was hoping there would be a simpler and more elegant solution.

Edit: And not even that last solution works, since the compiler does not deduce that (Monad (SomeExtension p a' a b' b m)) implies (Monad (p a' a b' b m)) for a specific choice of variables, even when given the following instance:

instance (Monad (p a b m)) => Monad (SomeExtension p a b m) where ...

Edit #2: The next solution I’m considering is just duplicating the methods for the Monad class within the ProxyC class:

class ProxyC p where
    return' :: (Monad m) => r -> p a' a b' b m r
    (!>=) :: (Monad m) => ...

… and then instantiating them with each ProxyC instance. This seems okay for my purposes since the Monad methods only need to be used internally for extension writing and the original type still has a proper Monad instance for the downstream user. All this does is just expose the Monad methods to the instance writer.

  • 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-11T22:46:29+00:00Added an answer on June 11, 2026 at 10:46 pm

    a rather trivial way to do this is use a GADT to move the proof to the value level

    data IsMonad m where
      IsMonad :: Monad m => IsMonad m 
    
    class ProxyC p where
      getProxyMonad :: Monad m => IsMonad (p a' a b' b m)
    

    you will need to explicitly open the dictionary wherever you need it

    --help avoid type signatures
    monadOf :: IsMonad m -> m a -> IsMonad m
    monadOf = const
    
    --later on
    case getProxyMonad `monadOf` ... of
      IsMonad -> ...
    

    the tactic of using GADTs to pass proofs of propositions is really very general. If you are okay using constraint kinds, and not just GADTs, you can instead use Edward Kmett’s Data.Constraint package

    class ProxyC p where
      getProxyMonad :: Monad m => Dict (Monad (p a' a b' b m))
    

    which lets you defined

    getProxyMonad' :: ProxyC p => (Monad m) :- (Monad (p a' a b' b m))
    getProxyMonad' = Sub getProxyMonad
    

    and then use a fancy infix operator to tell the compiler where to look for the monad instance

     ... \\ getProxyMonad'
    

    in fact, the :- entailment type forms a category (where the objects are constraints), and this category has lots of nice structure, which is to say it is pretty nice to do proofs with.

    p.s. none of these snippets are tested.

    edit:
    you could also combine the value level proofs with a newtype wrapper and not need to open GADTs all over the place

    newtype WrapP p a' a b' b m r = WrapP {unWrapP :: p a' a b' b m r}
    
    instance ProxyC p => Monad (WrapP p) where
      return = case getProxyMonad of
                    Dict -> WrapP . return
      (>>=) = case getProxyMonad of
                   Dict -> \m f -> WrapP $ (unWrapP m) >>= (unWrapP . f)
    
    instance ProxyC p => ProxyC (WrapP p) where
      ...
    

    I suspect, but obviously have not tested, that this implementation will also be relatively efficient.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

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.