Skip to content

Instantly share code, notes, and snippets.

@lgastako
Created July 25, 2020 21:28
Show Gist options
  • Save lgastako/11467ebd897121ce129efb733fdee7c4 to your computer and use it in GitHub Desktop.
Save lgastako/11467ebd897121ce129efb733fdee7c4 to your computer and use it in GitHub Desktop.
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeSynonymInstances #-}
module ParsingFromScratch where
import Data.String ( String )
import GHC.Num ( (*)
, (+)
)
(.) :: (b -> c) -> (a -> b) -> a -> c
(g . f) x = g (f x)
($) :: (a -> b) -> a -> b
f $ x = f x
undefined :: a
undefined = undefined
identity :: a -> a
identity x = x
-- A parser for a thing is a function from a string to a list of pairs of
-- strings and things.
newtype Parser a = Parser (String -> [(String, a)])
class Functor f where
map :: (a -> b) -> f a -> f b
instance Functor [] where
map f [] = []
map f (x:xs) = f x:map f xs
instance Functor ((->) a) where
map f g = f . g
instance Functor ((,) a) where
map f (x, y) = (x, f y)
instance Functor Parser where
map :: forall a b. (a -> b) -> Parser a -> Parser b
map f (Parser p) = Parser $ map (map f) . p -- $ g
where
g = map (map f) . p
-- src/ParsingFromScratch.hs:46:22-41: error: …
-- • Couldn't match expected type ‘[(String, a)] -> c0’
-- with actual type ‘Parser b’
-- • Possible cause: ‘($)’ is applied to too many arguments
-- In the first argument of ‘(.)’, namely ‘Parser $ map (map f)’
-- In the expression: Parser $ map (map f) . p
-- In an equation for ‘map’:
-- map f (Parser p)
-- = Parser $ map (map f) . p
-- where
-- g = map (map f) . p
-- • Relevant bindings include
-- g :: String -> [(String, b)]
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:48:7)
-- p :: String -> [(String, a)]
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:17)
-- f :: a -> b
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:7)
-- map :: (a -> b) -> Parser a -> Parser b
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:3)
-- |
-- src/ParsingFromScratch.hs:46:22-45: error: …
-- • Couldn't match expected type ‘Parser b’
-- with actual type ‘String -> c0’
-- • Probable cause: ‘(.)’ is applied to too few arguments
-- In the expression: Parser $ map (map f) . p
-- In an equation for ‘map’:
-- map f (Parser p)
-- = Parser $ map (map f) . p
-- where
-- g = map (map f) . p
-- In the instance declaration for ‘Functor Parser’
-- • Relevant bindings include
-- g :: String -> [(String, b)]
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:48:7)
-- f :: a -> b
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:7)
-- map :: (a -> b) -> Parser a -> Parser b
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:3)
-- |
-- src/ParsingFromScratch.hs:46:31-41: error: …
-- • Couldn't match type ‘(String, a)’
-- with ‘ghc-prim-0.5.3:GHC.Types.Char’
-- Expected type: String -> [(String, b)]
-- Actual type: [(String, a)] -> [(String, b)]
-- • In the second argument of ‘($)’, namely ‘map (map f)’
-- In the first argument of ‘(.)’, namely ‘Parser $ map (map f)’
-- In the expression: Parser $ map (map f) . p
-- • Relevant bindings include
-- p :: String -> [(String, a)]
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:17)
-- f :: a -> b
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:7)
-- map :: (a -> b) -> Parser a -> Parser b
-- (bound at /Users/john/src/katas/haskell/src/ParsingFromScratch.hs:46:3)
-- |
-- Compilation failed.
@lgastako
Copy link
Author

...turns out it was because I forgot the infixr 0 $.

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