Skip to content

Instantly share code, notes, and snippets.

@louissalin
Last active August 29, 2015 14:02
Show Gist options
  • Save louissalin/942fa1a6488a080aec23 to your computer and use it in GitHub Desktop.
Save louissalin/942fa1a6488a080aec23 to your computer and use it in GitHub Desktop.

the code that fails is below. Here's some imported code:

newtype Size = Size Int
  deriving (Eq, Ord, Show, Num)

getSize :: Size -> Int
getSize (Size i) = i

class Sized a where
  size :: a -> Size

instance Sized Size where
  size = id

-- This instance means that things like
--   (Foo, Size)
--   (Foo, (Bar, Size))
--   ...
-- are all instances of Sized.
instance Sized b => Sized (a,b) where
  size = size . snd

instance Monoid Size where
  mempty  = Size 0
  mappend = (+)

The code that fails:

1  data JoinList m a = Empty
2                    | Single m a
3                    | Append m (JoinList m a) (JoinList m a)
4    deriving (Eq, Show)
5   
6  tag :: Monoid m => JoinList m a -> m
7  tag Empty = mempty
8  tag (Single m _) = m
9  tag (Append m _ _) = m
10
11
12 indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a
13 indexJ _ Empty = Nothing
14 indexJ 0 (Single _ a) = Just a
15 indexJ i (Append _ jl1 jl2)
16   | i < (getSize(tag jl1)) = indexJ (i-1) jl1
17   | otherwise     = indexJ (i - (getSize (tag jl1))) jl2
18 indexJ _ _ = Nothing

This doesn't compile. I get the following errors (line numbers have been adjusted):

StringBufEditor_louis.hs:16:22:
    Could not deduce (b ~ Size)
    from the context (Sized b, Monoid b)
      bound by the type signature for
                 indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a
      at StringBufEditor_louis.hs:12:11-63
      ‘b’ is a rigid type variable bound by
          the type signature for
            indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a
          at StringBufEditor_louis.hs:12:11
    Expected type: JoinList Size a
      Actual type: JoinList b a
    Relevant bindings include
      jl2 :: JoinList b a (bound at StringBufEditor_louis.hs:15:24)
      jl1 :: JoinList b a (bound at StringBufEditor_louis.hs:15:20)
      indexJ :: Int -> JoinList b a -> Maybe a
        (bound at StringBufEditor_louis.hs:13:1)
    In the first argument of ‘tag’, namely ‘jl1’
    In the first argument of ‘getSize’, namely ‘(tag jl1)’
Failed, modules loaded: StringBuffer, Editor, Buffer, Sized.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment