In a library I'm writing I've found it to be seemingly elegant to write a class that is similar to (but slightly more general than) the following, which combines both the usual uncurry
over products and the fanin
function (from here, or here if you prefer):
{-# LANGUAGE TypeOperators, TypeFamilies,MultiParamTypeClasses, FlexibleInstances #-}
import Prelude hiding(uncurry)
import qualified Prelude
class Uncurry t r where
type t :->-> r
uncurry :: (t :->-> r) -> t -> r
instance Uncurry () r where
type () :->-> r = r
uncurry = const
instance Uncurry (a,b) r where
type (a,b) :->-> r = a -> b -> r
uncurry = Prelude.uncurry
instance (Uncurry b c, Uncurry a c)=> Uncurry (Either a b) c where
type Either a b :->-> c = (a :->-> c, b :->-> c)
uncurry (f,g) = either (uncurry f) (uncurry g)
I usually browse Edward Kmett's categories
package (linked above) to get my bearings for this sort of thing, but in that package we have fanin and uncurry separated into the CoCartesian and CCC classes respectively.
I've read a bit about BiCCCs but don't really understand them yet.
My questions are
Is the abstraction above justified by some way of squinting at category theory?
If so what would be the proper CT-grounded language to talk about the class and its instances?
EDIT: In case it helps and the simplification above is distorting things: in my actual application I'm working with nested products and coproducts, e.g. (1,(2,(3,())))
. Here is the real code (although for boring reasons the last instance is simplified, and doesn't work alone as written)
instance Uncurry () r where
type () :->-> r = r
uncurry = const
instance (Uncurry bs r)=> Uncurry (a,bs) r where
type (a,bs) :->-> r = a -> bs :->-> r
uncurry f = Prelude.uncurry (uncurry . f)
-- Not quite correct
instance (Uncurry bs c, Uncurry a c)=> Uncurry (Either a bs) c where
type Either a bs :->-> c = (a :->-> c, bs :->-> c)
uncurry (f,fs) = either (uncurry f) (uncurry fs) -- or as Sassa NF points out:
-- uncurry (|||)
So the const
instance for ()
instance came naturally as the recursive base case for the n-ary tuple uncurry instance, but seeing all three together looked like... something non-arbitrary.
Update
I found that thinking in terms of algebraic operations, a.la Chris Taylor's blogs about the "algebra of ADTs". Doing so clarified that my class and methods were really just the exponent laws (and the reason why my last instance was not right).
You can see the result in my shapely-data
package, in the Exponent
and Base
classes; see also the source for notes and non-wonky doc markup.