-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTypes4.hs
79 lines (55 loc) · 2.17 KB
/
Types4.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE GeneralizedNewtypeDeriving, TypeOperators, TypeFamilies,
FlexibleContexts #-}
module Types4 where
-- Derived from Connor McBride's WhatrTypes4 at
-- https://github.com/pigworker/WhatRTypes4/blob/master/Types4.hs
import Data.Monoid
import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.Char
newtype P thing = P {parse :: String -> [(thing, String)]} deriving Monoid
instance Functor P where
fmap f (P p) = P $ fmap (first f) . p -- P $ \s -> fmap (first f) $ p s
instance Applicative P where
pure x = P $ \s -> [(x, s)]
(P pf) <*> (P pv) = P $ \s -> [(f v, s'') | (f, s') <- pf s, (v, s'') <- pv s']
-- instance Monad P where
-- return x = P $ \ s -> [(x, s)]
-- P af >>= k = P $ \ s -> do
-- (a, s) <- af s
-- parse (k a) s
instance Alternative P where -- (what if P used Maybe?)
empty = mempty
(<|>) = mappend
eat :: (Char -> Bool) -> P Char
eat p = P $ \s -> case s of
(c : s) | p c -> [(c, s)]
_ -> []
type Cell = Maybe Int
pcell :: P Cell
pcell = many (eat isSpace) *> (Just . digitToInt <$> eat isDigit <|> Nothing <$ eat (== '.'))
newtype I x = I x deriving (Show, Functor)
newtype K a x = K a deriving (Show, Functor)
data (f :*: g) x = f x :*: g x deriving Show
data (f :+: g) x = L (f x) | R (g x) deriving Show
newtype (f :.: g) x = C {unC :: f (g x)} deriving Show
instance (Functor f, Functor g) => Functor (f :*: g) where
fmap k (fx :*: gx) = fmap k fx :*: fmap k gx
instance (Functor f, Functor g) => Functor (f :+: g) where
fmap k (L fx) = L (fmap k fx)
fmap k (R gx) = R (fmap k gx)
instance (Functor f, Functor g) => Functor (f :.: g) where
fmap k (C fgx) = C (fmap (fmap k) fgx)
instance Applicative I where
pure = I
I f <*> I s = I (f s)
instance Monoid a => Applicative (K a) where
pure x = K mempty
K f <*> K s = K (mappend f s)
instance (Applicative f, Applicative g) => Applicative (f :*: g) where
pure x = pure x :*: pure x
(ff :*: gf) <*> (fs :*: gs) = (ff <*> fs) :*: (gf <*> gs)
instance (Applicative f, Applicative g) => Applicative (f :.: g) where
pure = C . pure . pure
C fgf <*> C fgs = C $ liftA2 (<*>) fgf fgs