mlhaufe/brevity

Related Traits and composition

Opened this issue · 0 comments

If there are a set of related functions that need to be defined together how is that accomplished?

Example: Ordering in Haskell

data Ordering =  LT | EQ | GT deriving
                              (Eq, Ord, Bounded, Enum, Read, Show)

class Eq a where
      (==), (/=)  ::  a -> a -> Bool

      x /= y  = not (x == y)
      x == y  = not (x /= y)

class (Eq a) => Ord a where
  compare              :: a -> a -> Ordering
  (<), (<=), (>=), (>) :: a -> a -> Bool
  max, min             :: a -> a -> a

  compare x y | x == y    = EQ
              | x <= y    = LT
              | otherwise = GT

  x <= y  = compare x y /= GT
  x <  y  = compare x y == LT
  x >= y  = compare x y /= LT
  x >  y  = compare x y == GT

  -- Note that (min x y, max x y) = (x,y) or (y,x)
  max x y | x <= y    =  y
          | otherwise =  x
  min x y | x <= y    =  x
          | otherwise =  y

Early thoughts are that traits should define the name explicitly and not rely on the variable name itself:

const print1 = trait('print', dataDecl, {...})

const print2 = trait('print', dataDecl, { [extend]: print1, ...})

Then support direct trait composition:

const gt = trait('gt', dataDecl, {...}}

const lt = trait('lt', dataDecl, {...}}

const eq = trait('eq', dataDecl, {...}}

const ord = merge(gt, lt, eq)

Having complect as well as merge doesn't seem reasonable so can these be combined?

What does it mean to try and merge two factories?

What does it mean for a trait to try an extend ord?

If complect is subsumed by merge and some data/trait tries to extend merged, how does that work?