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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:04:10+00:00 2026-05-22T21:04:10+00:00

I thought I’d try the intriguing Representable-functors package to define a Monad and Comonad

  • 0

I thought I’d try the intriguing Representable-functors package to define a Monad and Comonad instance for the functor given by data Pair a = Pair a a which is representable by Bool; as mentioned in the answer to my earlier question on the vector monad.

The first thing I noticed was that to make my type an instance of Representable, I should not only define tabulate and index, but also ensure my type is an instance of the Indexable, Distributive, Keyed, Apply, Applicative, and Functor type classes. Well, ok, index completes the definition of Indexable, and the <.> function of Apply can use <*> from Applicative; and it shouldn’t be a surprise that a Functor instance is required. Nevertheless, I am doubtful of my instances for Keyed and Distributive.

data Pair a = Pair a a
  deriving (Show,Eq)

instance Functor Pair where
  fmap f (Pair x y) = Pair (f x) (f y)

type instance Key Pair = Bool

instance Keyed Pair where
  mapWithKey f (Pair x y) = Pair (f False x) (f False y)

instance Indexable Pair where
  index (Pair x _) False = x
  index (Pair _ y) True  = y

instance Applicative Pair where
  pure a = Pair a a
  Pair f g <*> Pair x y = Pair (f x) (g y)

instance Apply Pair where
  (<.>) = (<*>)

instance Distributive Pair where
  collect f x = Pair (getL . f <$> x) (getR . f <$> x)
    where getL (Pair x _) = x
          getR (Pair _ y) = y

instance Representable Pair where
  tabulate f = Pair (f False) (f True)

My mapWithKey definition borrows from that of the [] instance for Keyed: though I don’t understand why 0 was used there for every iteration. I have similarly used False for each term of Pair.

As I concluded by defining the Monad and Comonad instances, I discovered that Bool requires a Semigroup definition for Extend, and a Monoid definition for Comonad. I follow the Semigroup instance for the Either, which is isomorphic to (||), and choose False for mempty:

instance Monad Pair where
  return = pureRep
  (>>=)  = bindRep

instance Monoid Bool where
  mempty = False
  mappend = (||)

instance Semigroup Bool where
  (<>) = mappend

instance Extend Pair where
  extend = extendRep -- needs Bool Semigroup

instance Comonad Pair where
  extract = extractRep -- needs Bool Monoid

So then, have I met the requirements of the Representable class correctly, and idiomatically?

  • 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-22T21:04:11+00:00Added an answer on May 22, 2026 at 9:04 pm

    Yes you have. Though your instance for Keyed is off.

    instance Keyed Pair where
        mapWithKey f (Pair x y) = Pair (f False x) (f True y)
    

    or even easier

    instance Keyed Pair where
        mapWithKey = mapWithKeyRep
    

    and similarly

    instance Distributive Pair where
        distribute = distributeRep
    

    Given index and tabulate you can use the various fooRep methods in the Representable module to provide definitions for all of the other superclasses.

    The Extend and Comonad definitions, are not actually part of the requirements to be Representable. They are included though, because being representable means you are isomorphic to a function, which enables you to recycle the definition for the “exponential” Comonad (aka cowriter, or Traced comonad) to become a Comonad as well, given some monoid on your representation. This isn’t required though, mainly because I can’t constrain it to hold given the types involved.

    You may want to drop the Semigroup and Monoid for Bool though and just hand implement extend and extract. It is easy enough.

    instance Extend Pair where
        extend f p@(Pair a b) = Pair (f p) (f (Pair b a))
    
    instance Comonad Pair where
        extract (Pair a b) = a
    

    Also, this type is provided by the representable-tries package, which includes a number of other instances.

    And,

    import Control.Applicative
    bool = [True, False]
    f tt _tf _ft _ff True  True  = tt
    f _tt tf _ft _ff True  False = tf
    f _tt _tf ft _ff False True  = ft
    f _tt _tf _ft ff False False = ff
    associative f = and (assoc <$> bool <*> bool <*> bool) where 
        assoc a b c = f (f a b) c == f a (f b c)
    semigroups = filter associative 
        [ f tt tf ft ff | tt <- bool, tf <- bool, ft <- bool, ff <- bool ]
    unital (u, f) = all unit bool where 
        unit a = f u a == a && f a u == a
    monoids = filter unital 
        [ (u, f) | u <- bool, f <- semigroups ]
    

    shows that as you surmised there are the 4 possible monoids you surmised and if you only want an extend instance, there are 8 semigroups available.

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

Sidebar

Related Questions

Thought I'd try to implement SHA1 in Haskell myself. I came up with an
Thought I will try my luck here. Have tried virtually every solution I could
I thought .Net code gets compiled into MSIL, so I always wondered how do
I thought jQuery Intellisense was supposed to be improved with SP1. I even downloaded
I thought I heard that py2exe was able to do this, but I never
I thought that there was some way in .net 3.0 to give an array
I thought I had seen a bug report about this on the jQuery site,
I thought I'd offer this softball to whomever would like to hit it out
I thought I understood what the default method does to a hash... Give a
I thought people would be working on little code projects together, but I don't

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.