Create a gist now

Instantly share code, notes, and snippets.

@rblaze /error.txt Secret
Created Nov 26, 2017

What would you like to do?
/home/blazered/tmp/test.hs:23:21: error:
• Couldn't match type ‘Rep a0’ with ‘Rep a’
Expected type: Rep a x0
Actual type: Rep a0 x0
NB: ‘Rep’ is a type function, and may not be injective
The type variable ‘a0’ is ambiguous
• In the first argument of ‘update’, namely ‘(from def)’
In the first argument of ‘to’, namely ‘(update (from def) parts)’
In the expression: to (update (from def) parts)
• Relevant bindings include
parseLog :: BS8.ByteString -> a
(bound at /home/blazered/tmp/test.hs:21:3)
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
module Test where
import Data.Default
import GHC.Generics
import qualified Data.ByteString.Char8 as BS8
class LogField a where
parseField :: BS8.ByteString -> Maybe a
instance LogField BS8.ByteString where
parseField = Just
class LogReadable a where
parseLog :: BS8.ByteString -> a
default parseLog :: (Default a, Generic a) => BS8.ByteString -> a
parseLog line =
let parts = BS8.split '\1' line
in to (update (from def) parts)
class Updater f where
update :: f p -> [BS8.ByteString] -> f p
instance LogField c => Updater (K1 i c) where
update d [] = d
update d (x:_) =
case parseField x of
Just v -> K1 v
Nothing -> d
instance (LogField c, Updater g) => Updater ((K1 i c) :*: g) where
update d [] = d
update (d :*: r) (x:xs) = (update d [x]) :*: (update r xs)
instance Updater f => Updater (M1 i t f) where
update (M1 x) xs = M1 (update x xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment