Skip to content

Instantly share code, notes, and snippets.

@razielgn
Created October 2, 2015 11:44
Show Gist options
  • Save razielgn/edc2e9e50280057079ed to your computer and use it in GitHub Desktop.
Save razielgn/edc2e9e50280057079ed to your computer and use it in GitHub Desktop.
newtype ListLayer a = ListLayer [a]
class Layerable a where
something :: a -> a
instance Layerable (ListLayer a) where
something = id
@giorgio-v
Copy link

Here is the relevant part.

data LTree a = LLeaf a | LNode a [LTree a]
data Layer a = Layer a

lleaf :: (t -> a) -> t -> LTree a
lleaf f x = LLeaf (f x)


class Layerable a where
    initialL :: ([a] -> b) -> [a] -> Layer (LTree b)

instance Layerable [a] where
    initialL f = map (lleaf f . wrap)
        where wrap x = [x]

And here’s is the error:

Couldn't match type ‘[LTree b]’ with ‘Layer (LTree b)’
Expected type: [[a]] -> Layer (LTree b)
  Actual type: [[a]] -> [LTree b]
Relevant bindings include
  f :: [[a]] -> b
    (bound at /var/folders/yq/ydckqkv92jz4dhhlvd1ry5yr0000gn/T/flycheck57018gry/nexus.hs:49:14)
  initialL :: ([[a]] -> b) -> [[a]] -> Layer (LTree b)
    (bound at /var/folders/yq/ydckqkv92jz4dhhlvd1ry5yr0000gn/T/flycheck57018gry/nexus.hs:49:5)
In the expression: map (lleaf f . wrap)
In an equation for ‘initialL’:
    initialL f
      = map (lleaf f . wrap)
      where
          wrap x = [x]

Things to be noted:

  • The problem, as I exposed it to the ML, is not the type parameter of the instance of Layerable. As we said, we can simply use a list of a.
  • The implementation of initialL (for lists) maps over (a composition of) f, so it’s not really it returns a list of LTree b. So the problem is how to enforce the equivalence between the Layer a and [a], which in the end is the same question on the ML, but without the type class nonsense.
  • For the sake of the exploration that I’m trying to do, I do want different implementations Layerable (yes, the name sucks) so my guess is that I cannot use some kind of newtype because I can only have one constructor, AFAIK.

Maybe the best course of action would be declare Layer a functor and use fmap and be done with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment