Options

Question 2.6 - Products in Hask

Products in Hask.

Recall that Haskell has a built-in product (pair) type with constructors written:

data (a,b) = (a,b)

Implement isomorphisms of the following type signatures by drawing diagrams and translating them into code. Explain why the functions you have constructed are isomorphisms.

(a) swap :: (a,b) -> (b,a)
(b) unit :: a -> ((),a)
(c) assoc :: (a,(b,c)) -> ((a,b),c)


Next Prev All

Comments

  • 1.

    a)

    swap :: (a,b) -> (b,a)

    swap (x,y) = (y,x)

    swap $ swap (x,y) = swap (y,x) = (x,y)

    b)

    unit a = ((), a)

    unUnit ((), a) = a

    (unUnit . unit) a = unUnit ((), a) = a

    (unit . unUnit) ((),a) = unit a = ((),a)

    c)

    assoc (x, (y,z)) = ((x,y),z)

    unAssoc ((x,y),z) = (x, (y,z))

    (unAssoc . assoc) (x, (y,z)) = unAssoc ((x,y),z) = (x, (y,z))

    Comment Source:a) > swap :: (a,b) -> (b,a) > swap (x,y) = (y,x) > swap $ swap (x,y) = swap (y,x) = (x,y) b) > unit a = ((), a) > unUnit ((), a) = a > (unUnit . unit) a = unUnit ((), a) = a > (unit . unUnit) ((),a) = unit a = ((),a) c) > assoc (x, (y,z)) = ((x,y),z) > unAssoc ((x,y),z) = (x, (y,z)) > (unAssoc . assoc) (x, (y,z)) = unAssoc ((x,y),z) = (x, (y,z))
Sign In or Register to comment.