2

Please look at the following piece of code:

draw :: (Ord n, Show n) => StagedListVM n -> Widget n
draw state =
  vLimit
    (StagedListVM.rowsAmount state)
    ( renderList
        ( \_isSelected pairs ->
            hBox $ map (someFunction _isSelected) pairs
        )
        (StagedListVM.isFocused state)
        (StagedListVM.bricksList state)
    )
  where
    someFunction _isSelected pair = StagedListVM.something

I have 2 more similar pieces of code:

draw :: (Ord n, Show n) => UnstagedListVM n -> Widget n
draw state =
  vLimit
    (UnstagedListVM.rowsAmount state)
    ( renderList
        ( \_isSelected pairs ->
            hBox $ map (someFunction _isSelected) pairs
        )
        (UnstagedListVM.isFocused state)
        (UnstagedListVM.bricksList state)
    )
  where
    someFunction _isSelected pair = UnstagedListVM.something

And the last one:

draw :: (Ord n, Show n) => TreeVM n -> Widget n
draw state =
  vLimit
    (TreeVM.rowsAmount state)
    ( renderList
        ( \_isSelected pairs ->
            hBox $ map (someFunction _isSelected) pairs
        )
        (TreeVM.isFocused state)
        (TreeVM.bricksList state)
    )
  where
    someFunction _isSelected pair = TreeVM.something

As you can see, they are the same, except the type of the first parameter to the draw function. I am somewhat new to Haskell. Now, if it was some imperative OOP language, I would write one function instead of 3, let's call it superDraw and pass it some abstract interface as a parameter.

How can I wrap these 3 functions in one function in Haskell?

Thanks.

5
  • 3
    You probably want a typeclass that defines rowsAmount, isFocused, etc, with each of StagedListVM et al. having instances of that type class. Then draw :: (Ord n, Show n, YourTypeClass t) => t n -> Widget n.
    – chepner
    Mar 16 at 18:13
  • @chepner Hmmm... I needed to google type classes in Haskell. It seems to be they are "Haskell/Function Programming" alternative to OOP's abstract interface idiom. This seems like forcing OOP stuff onto Functional Programming - it's usually bad. But not always. Maybe this is the "not always" case? I don't know. Mar 16 at 18:20
  • 3
    @RefaelSheinker They can be used as an alternative to OOP interfaces, but they are not OOP interfaces. IMO, they are much closer to FP than OOP. Very loosely, type classes are a convenient way to omit arguments from functions definitions and functions calls. A definition max :: Ord a => a -> a -> a is silently passed a comparison function as an additional argument. A call max "a" "bc" implicitly passes the string comparison operator.
    – chi
    Mar 16 at 19:02
  • Type classes provide ad hoc polymorphism. For example, length :: [a] -> Int implements parametric polymorphism: the definition does not depend on the specific type a, only on the fact that it's a list. (+) :: Num a => a -> a -> a, on the other hand, provides ad hoc polymorphism: every class that has a Num instance defines (+) in an a-specific way.
    – chepner
    Mar 16 at 20:01
  • Thanks to all the current/future commenters. Mar 17 at 6:21

1 Answer 1

5

It seems to be they are "Haskell/Function Programming" alternative to OOP's abstract interface idiom. This seems like forcing OOP stuff onto Functional Programming

This is a good attitude. Indeed, people too often use typeclasses in Haskell, thinking by analogy with OO language when this doesn't actually make sense. However, that's not to say typeclasses don't have uses at all.

The question I would be asking myself first is: why are StagedListVM, UnstagedListVM and TreeVM even three separate types in the first place? If they are structurally similar and can be used completely interchangeable (like different subclasses in OO), then this buys you nothing but added complexity, compared to a single type with some variant-data in its implementation. If there a small difference that you want the type system to track, you can always add a type parameter just for that purpose, which the function working on your type mostly ignore.

Even if the three versions use completely different implementations, it can be best to simply wrap the methods-if-this-were-OO in a simple record. For much of your code it looks a lot like this might make sense:

data VM = VM { rowsAmount :: Int
             , isFocused :: Bool
             , ...
             }

Only something doesn't seem to fit in this approach, because it does not take the state argument.

And that to me signals that this might indeed be a use case where a typeclass is appropriate:

class VM v where
  rowsAmount :: v n -> Int
  isFocused :: v n -> Bool
  ...
  something :: Something (v n) ...

instance VM StagedListVM where
  rowsAmount = StagedListVM.rowsAmount
  ...
instance VM UnstagedListVM where
...

With that, the code becomes readily polymorphic over all the three types:

draw :: (VM v, Ord n, Show n) => v n -> Widget n
draw state =
  vLimit
    (rowsAmount state)
    ( renderList
        ( \_isSelected pairs ->
            hBox $ map (someFunction _isSelected) pairs
        )
        (isFocused state)
        (bricksList state)
    )
  where
    someFunction _isSelected pair = something
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.