Suggestions for a product type with a unit element

I’ve been reading a paper and implementing some of the Haskell code in PureScript.

So far I’ve been able to substitute (x, y) for Tuple x y but now I’ve reached something which relies on () in Haskell.

At first I thought using unit/ Unit would work but that doesn’t type check. Should I be looking for something other than Tuple?

I’m not sure if it’s essential to implement this part of the paper but I’m curious how to address this either way.

Well, it’s definitely essential to finish implementing the paper :slightly_smiling_face:

FWIW:

-- Haskell
unit :: Foo a (a, ())
unit = Foo f g where
  f x = Just (x, ())
  g (x, ()) = Just x
-- PureScript
-- Could not match type Foo t2 (Tuple t2 Unit) with type Unit
unit :: forall a. Foo a (Tuple a Unit)
unit = Foo f g
  where
  f x = Just (Tuple x unit)

  g (Tuple x y)
    | y == unit = Just x
    | otherwise = Nothing

where

data Foo a b = Foo (a -> Maybe b) (b -> Maybe a)

I think I can get away with doing this:

-- PureScript
import Prelude

import Data.Unit as Unit

type TUnit = Tuple Unit Unit

tunit :: TUnit
tunit = Tuple Unit.unit Unit.unit

The problem here is your unit definition is shadowing the normal unit, so when you say:

f x = Just (Tuple x unit)

The unit there is not the inhabitant of Unit, it’s the function again. If you rename the function you’re defining to unit' or foonit or whatever it works fine :slight_smile:

1 Like

I knew I must have been missing something obvious!

Perfect for your avatar, as usual.

Thanks Gary