on master help sign in
Moved to mono-traversable and changed some things from arrow to semiisos (#32)
commit5a0a9c1b571f9fea2c520798212e4a4190e3c54f authoriko <ilyakooo0@gmail.com> dateSat 15 Feb 2020, 16:18 (7 years ago) parent1ffcdf0
16 files changed+79−135
package.yaml+1 src/Data/Isoparsec.hs+6−7 src/Data/Isoparsec/ByteString.hs+9−15 src/Data/Isoparsec/Char.hs+7−7 src/Data/Isoparsec/Chunks.hs+1−1 src/Data/Isoparsec/Internal.hs+35−55 src/Data/Isoparsec/Megaparsec.hs+2−3 src/Data/Isoparsec/Printer.hs+4−4 src/Data/Isoparsec/ToIsoparsec.hs+2−3 src/Data/Isoparsec/Tokenable.hs−24 stack.yaml−5 test/Spec/Helper.hs+2−1 test/Spec/JSON.hs+4−4 test/Spec/Orphans.hs+3−3 test/Spec/Ssh.hs+1−1 test/Spec/TwoDigits.hs+2−2
package.yaml+1
@@ -27,6 +27,7 @@ dependencies:
2727# - text
2828- mtl
2929- profunctors
30+- mono-traversable
3031
3132library:
3233 source-dirs: src
src/Data/Isoparsec.hs+6−7
@@ -33,18 +33,17 @@ import Data.Coerce
3333import Data.Either
3434import Data.Isoparsec.Internal as X
3535import Data.Isoparsec.ToIsoparsec as X
36−import Data.Isoparsec.Tokenable as X
3736import qualified Data.Map as M
3837import Prelude hiding ((.), fail, id)
3938
4039opt :: (ArrowPlus m, PolyArrow m SemiIso) => m () () -> m () ()
41−opt m = m <+> konst ()
40+opt m = m <+^ konst ()
4241
4342opt' :: (ArrowPlus m, PolyArrow m SemiIso, Eq a) => a -> m () a -> m () ()
44−opt' a m = (m >>> tsnok a) <+> konst ()
43+opt' a m = (m >>^ tsnok a) <+^ konst ()
4544
4645repeating :: (PolyArrow m SemiIso, ArrowPlus m, Eq b) => m () b -> m () [b]
47−repeating m = (m &&& (repeating m <+> konst [])) >>^ siCons
46+repeating m = (m &&& (repeating m <+^ konst [])) >>^ siCons
4847
4948infixr 1 %>>
5049
@@ -109,8 +108,8 @@ infixr 0 <^>
109108 m x' y'
110109b <^> p = b >>> morphed %>% p
111110
112−coercing :: forall b a m. (Coercible a b, PolyArrow m SemiIso) => m a b
113−coercing = arr $ siPure coerce coerce
111+coercing :: forall b a. Coercible a b => SemiIso a b
112+coercing = siPure coerce coerce
114113
115114morphed ::
116115 (TupleMorphable a c, TupleMorphable b c) => SemiIso a b
@@ -137,7 +136,7 @@ auto :: forall x s m. (ToIsoparsec x s, Isoparsec m s) => m () x
137136auto = toIsoparsec
138137
139138specific :: forall x s m. (ToIsoparsec x s, Isoparsec m s, Eq x, Show x) => x -> m () ()
140−specific x = auto @x >>> check (== x) >>> tsnok x
139+specific x = auto @x >>> check (== x) ^>^ tsnok x
141140
142141throughIntegral ::
143142 (Integral a, Integral b, Num a, Num b, PolyArrow m SemiIso) =>
src/Data/Isoparsec/ByteString.hs+9−15
@@ -30,10 +30,10 @@ class BytesToIsoparsec b (e :: Endianness) where
3030
3131instance (FiniteBits b, Integral b) => BytesToIsoparsec b 'BE where
3232 bytesToIsoparsec _ =
33− konst word8s >>> manyTokens
33+ konst word8s ^>> manyTokens
3434 >>^ siPure
3535 (fromInteger . BS.foldl (\i w -> shiftL i 8 .|. toInteger w) 0)
36− ( liftTokens . snd . P.head
36+ ( fromList . snd . P.head
3737 . P.drop (fromIntegral word8s)
3838 . iterate (\(i, ww) -> (shiftR i 8, fromInteger i : ww))
3939 . (,[])
@@ -44,10 +44,10 @@ instance (FiniteBits b, Integral b) => BytesToIsoparsec b 'BE where
4444
4545instance (FiniteBits b, Integral b) => BytesToIsoparsec b 'LE where
4646 bytesToIsoparsec _ =
47− konst word8s >>> manyTokens
47+ konst word8s ^>> manyTokens
4848 >>^ siPure
4949 (fromInteger . BS.foldr (\w i -> shiftL i 8 .|. toInteger w) 0)
50− ( liftTokens . P.reverse . snd . P.head
50+ ( fromList . P.reverse . snd . P.head
5151 . P.drop (fromIntegral word8s)
5252 . iterate (\(i, ww) -> (shiftR i 8, fromInteger i : ww))
5353 . (,[])
@@ -68,13 +68,13 @@ newtype Byte64 (e :: Endianness) = Byte64 {unByte64 :: Word64}
6868 deriving (Show, Eq, Ord, Enum, Num, Real, Integral, Bounded)
6969
7070instance BytesToIsoparsec Word16 e => ToIsoparsec (Byte16 e) ByteString where
71− toIsoparsec = bytesToIsoparsec (Proxy @e) >>> coercing @(Byte16 e) @Word16
71+ toIsoparsec = bytesToIsoparsec (Proxy @e) >>^ coercing @(Byte16 e) @Word16
7272
7373instance BytesToIsoparsec Word32 e => ToIsoparsec (Byte32 e) ByteString where
74− toIsoparsec = bytesToIsoparsec (Proxy @e) >>> coercing @(Byte32 e) @Word32
74+ toIsoparsec = bytesToIsoparsec (Proxy @e) >>^ coercing @(Byte32 e) @Word32
7575
7676instance BytesToIsoparsec Word64 e => ToIsoparsec (Byte64 e) ByteString where
77− toIsoparsec = bytesToIsoparsec (Proxy @e) >>> coercing @(Byte64 e) @Word64
77+ toIsoparsec = bytesToIsoparsec (Proxy @e) >>^ coercing @(Byte64 e) @Word64
7878
7979instance ToIsoparsec Bool ByteString where
8080 toIsoparsec = anyToken >>> mapIso [(0, False), (1, True)]
@@ -85,13 +85,7 @@ newtype SSHString = SSHString {unSSHString :: String}
8585instance ToIsoparsec SSHString ByteString where
8686 toIsoparsec =
8787 auto @(Byte32 'BE) >>> coercing @Word32
88− >>> siPure fromIntegral fromIntegral ^>> manyTokens
88+ ^>> siPure fromIntegral fromIntegral
89+ ^>> manyTokens
8990 >>> utf8
9091 >>^ siPure SSHString unSSHString
91−
92−instance Tokenable ByteString where
93− type Token ByteString = Word8
94−
95− liftTokens = pack
96−
97− lowerTokens = unpack
src/Data/Isoparsec/Char.hs+7−7
@@ -11,16 +11,16 @@ import Data.Isoparsec
1111import Text.Read
1212import Prelude hiding ((.), id)
1313
14−space :: (Isoparsec m s, Token s ~ Char) => m () ()
14+space :: (Isoparsec m s, Element s ~ Char) => m () ()
1515space = token ' '
1616
17−unsafeWhiteSpace :: (Isoparsec m s, Token s ~ Char) => m () ()
18−unsafeWhiteSpace = tokensWhile isSpace >>> badTsnok mempty
17+unsafeWhiteSpace :: (Isoparsec m s, Element s ~ Char) => m () ()
18+unsafeWhiteSpace = tokensWhile isSpace >>^ badTsnok mempty
1919
20−unsafeWhiteSpace1 :: (Isoparsec m s, Token s ~ Char) => m () ()
21−unsafeWhiteSpace1 = tokensWhile1 isSpace >>> badTsnok (liftToken ' ')
20+unsafeWhiteSpace1 :: (Isoparsec m s, Element s ~ Char) => m () ()
21+unsafeWhiteSpace1 = tokensWhile1 isSpace >>^ badTsnok (singleton ' ')
2222
23−number :: (Isoparsec m s, Token s ~ Char) => m () Integer
23+number :: (Isoparsec m s, Element s ~ Char) => m () Integer
2424number =
2525 tokensWhile1 (\c -> isNumber c || c == '+' || c == '-')
26− >>^ siMaybe (readMaybe @Integer . lowerTokens) (Just . liftTokens . show)
26+ >>^ siMaybe (readMaybe @Integer . otoList) (Just . fromList . show)
src/Data/Isoparsec/Chunks.hs+1−1
@@ -12,4 +12,4 @@ newtype Chunk (n :: Nat) s = Chunk {unChunk :: s}
1212
1313instance (CmpNat n 0 ~ 'GT, KnownNat n) => ToIsoparsec (Chunk n s) s where
1414 toIsoparsec =
15− konst (fromIntegral $ natVal @n Proxy) >>> manyTokens >>> coercing
15+ konst (fromIntegral $ natVal @n Proxy) ^>> manyTokens >>^ coercing
src/Data/Isoparsec/Internal.hs+35−55
@@ -8,7 +8,6 @@ module Data.Isoparsec.Internal
88 isoCheck,
99 siCheck,
1010 check,
11− levitate,
1211 badKonst,
1312 badTsnok,
1413 unroll,
@@ -18,61 +17,53 @@ where
1817
1918import Control.Arrow.Extra
2019import Control.Monad
21−import Control.SemiIso as X
22−import Data.Isoparsec.Tokenable
20+import Control.SemiIso as X hiding (filterM, replicateM)
21+import Data.MonoTraversable as X
22+import Data.Sequences as X
2323import Numeric.Natural
2424import Prelude as P hiding ((.), id)
2525
2626class
27− (PolyArrow m SemiIso, ArrowPlus m, ArrowChoice m, Tokenable s) =>
27+ (PolyArrow m SemiIso, ArrowPlus m, ArrowChoice m, IsSequence s) =>
2828 Isoparsec m s
2929 | m -> s where
3030 {-# MINIMAL anyToken, manyTokens, tuck #-}
3131
32− anyToken :: m () (Token s)
32+ anyToken :: m () (Element s)
3333
34− token :: Token s -> m () ()
35− default token :: Eq (Token s) => Token s -> m () ()
36− token x = anyToken >>> tsnok x
34+ token :: Element s -> m () ()
35+ default token :: Eq (Element s) => Element s -> m () ()
36+ token x = anyToken >>^ tsnok x
3737
38− tokens :: [Token s] -> m () ()
39− default tokens :: [Token s] -> m () ()
38+ tokens :: [Element s] -> m () ()
39+ default tokens :: [Element s] -> m () ()
4040 tokens [] = arr $ isoConst () ()
4141 tokens (t : ts) = token t &&& tokens ts >>> arr (isoConst ((), ()) ())
4242
4343 chunk :: s -> m () ()
44− chunk = tokens . lowerTokens
44+ chunk = tokens . otoList
4545
46− notToken :: Token s -> m () (Token s)
47− default notToken :: Eq (Token s) => Token s -> m () (Token s)
46+ notToken :: Element s -> m () (Element s)
47+ default notToken :: Eq (Element s) => Element s -> m () (Element s)
4848 notToken t = tokenWhere (/= t)
4949
50− tokenWhere :: (Token s -> Bool) -> m () (Token s)
51− tokenWhere f =
52− anyToken >>> check f
50+ tokenWhere :: (Element s -> Bool) -> m () (Element s)
51+ tokenWhere f = anyToken >>^ check f
5352
5453 manyTokens :: m Natural s
5554
56− tokensWhile :: (Token s -> Bool) -> m () s
57−
5855 takeUntil :: s -> m () s
59− default takeUntil :: Eq (Token s) => s -> m () s
60− takeUntil s = takeUntil' s >>^ levitate
61− where
62− takeUntil' s' = (chunk s' >>> konst []) <+> ((anyToken &&& takeUntil' s') >>^ siCons)
56+ default takeUntil :: Eq s => s -> m () s
57+ takeUntil s = (chunk s >>^ konst mempty) <+> ((anyToken &&& takeUntil s) >>^ siCons)
6358
64− default tokensWhile :: (Token s -> Bool) -> m () s
59+ tokensWhile :: (Element s -> Bool) -> m () s
60+ default tokensWhile :: (Element s -> Bool) -> m () s
6561 tokensWhile f =
66− tokensWhile' f >>> check (P.all f) >>^ levitate
67− where
68− tokensWhile' g =
69− (tokenWhere g &&& tokensWhile' g >>^ siCons)
70− <+^ isoConst () []
62+ (((tokenWhere f >>^ check f) &&& tokensWhile f) >>^ siCons)
63+ <+^ isoConst () mempty
7164
72− tokensWhile1 :: (Token s -> Bool) -> m () s
73− tokensWhile1 f =
74− tokenWhere f &&& tokensWhile f
75− >>^ SI (\(a, aa) -> pure $ liftToken a <> aa) levitateHead
65+ tokensWhile1 :: (Element s -> Bool) -> m () s
66+ tokensWhile1 f = tokenWhere f &&& tokensWhile f >>^ siCons
7667
7768 -- | "tucks" the context of the parser into its input.
7869 -- > ┌─────┐
@@ -95,26 +86,15 @@ arrowsWhile :: (PolyArrow m SemiIso, ArrowPlus m) => m () a -> m () [a]
9586arrowsWhile f = ((f &&& arrowsWhile f) >>^ siCons) <+^ isoConst () []
9687
9788unroll :: (PolyArrow m SemiIso, ArrowPlus m, Eq a) => a -> m a (b, a) -> m () [b]
98−unroll a f = (konst a >>> unroll' f) <+^ isoConst () []
89+unroll a f = (konst a ^>> unroll' f) <+^ isoConst () []
9990 where
10091 unroll' g = (g >>> second (unroll' g)) >>^ siCons
10192
102−levitateHead :: Alternative f => Tokenable s => s -> f (Token s, s)
103−levitateHead s = case lowerTokens s of
104− (t : tt) -> pure (t, liftTokens tt)
105− [] -> empty
106−
107−siCons :: SemiIso (t, [t]) [t]
93+siCons :: IsSequence s => SemiIso (Element s, s) s
10894siCons =
109− SI
110− (pure . uncurry (:))
111− ( \case
112− (t : ts) -> pure (t, ts)
113− _ -> empty
114− )
115−
116−levitate :: Tokenable s => SemiIso [Token s] s
117−levitate = siPure liftTokens lowerTokens
95+ siMaybe
96+ (pure . uncurry cons)
97+ uncons
11898
11999class IsoparsecFail m e where
120100 failure :: e -> m a b
@@ -135,17 +115,17 @@ isoCheck f a b = siCheck f (pure . a) (pure . b)
135115isoConst :: s -> a -> SemiIso s a
136116isoConst s a = SI (const $ pure a) (const $ pure s)
137117
138−konst :: (PolyArrow a SemiIso, Eq x) => x -> a () x
139−konst x = badKonst x ^>> check (== x)
118+konst :: Eq x => x -> SemiIso () x
119+konst x = badKonst x >>> check (== x)
140120
141121badKonst :: x -> SemiIso () x
142122badKonst x = SI (const $ pure x) (const $ pure ())
143123
144−tsnok :: (PolyArrow a SemiIso, Eq x) => x -> a x ()
124+tsnok :: Eq x => x -> SemiIso x ()
145125tsnok x = check (== x) >>> badTsnok x
146126
147−badTsnok :: (PolyArrow a SemiIso) => x -> a x ()
148−badTsnok x = arr $ SI (const $ pure ()) (const $ pure x)
127+badTsnok :: x -> SemiIso x ()
128+badTsnok x = SI (const $ pure ()) (const $ pure x)
149129
150−check :: PolyArrow a SemiIso => (s -> Bool) -> a s s
151−check f = arr $ isoCheck f id id
130+check :: (s -> Bool) -> SemiIso s s
131+check f = isoCheck f id id
src/Data/Isoparsec/Megaparsec.hs+2−3
@@ -10,7 +10,6 @@ import Control.Arrow.Extra.Orphans ()
1010import Control.Monad
1111import Data.Functor
1212import Data.Isoparsec.Internal as I
13−import Data.Isoparsec.Tokenable
1413import Text.Megaparsec hiding (Token)
1514import qualified Text.Megaparsec as M
1615import Prelude hiding ((.))
@@ -28,7 +27,7 @@ instance (MonadParsec e s m) => PolyArrow (Kleisli m) SemiIso where
2827 Nothing -> M.failure Nothing mempty
2928
3029instance
31− (MonadParsec e s m, M.Token s ~ Token s, s ~ M.Tokens s, Tokenable s) =>
30+ (MonadParsec e s m, M.Token s ~ Element s, s ~ M.Tokens s, IsSequence s) =>
3231 Isoparsec (Kleisli m) s
3332 where
3433 anyToken = Kleisli $ const anySingle
@@ -47,5 +46,5 @@ instance
4746instance MonadParsec e s m => IsoparsecFail (Kleisli m) e where
4847 failure e = Kleisli $ \_ -> customFailure e
4948
50−instance (MonadParsec e s m, M.Token s ~ Token s, s ~ M.Tokens s) => ArrowPlus (Kleisli m) where
49+instance (MonadParsec e s m, M.Token s ~ Element s, s ~ M.Tokens s) => ArrowPlus (Kleisli m) where
5150 (Kleisli lhs) <+> (Kleisli rhs) = Kleisli $ \x -> try (lhs x) <|> rhs x
src/Data/Isoparsec/Printer.hs+4−4
@@ -25,15 +25,15 @@ instance IsoparsecFail (Cokleisli (WriterT s (Either e))) e where
2525 failure = Cokleisli . const . WriterT . Left
2626
2727instance
28− (MonadPlus m, Monoid s, Eq (Token s), Tokenable s, Show s) =>
28+ (MonadPlus m, Monoid s, Eq (Element s), Eq s, IsSequence s, Show s) =>
2929 Isoparsec (Cokleisli (WriterT (Dual s) m)) s
3030 where
31− token t = Cokleisli $ const $ tell . Dual . liftToken $ t
31+ token t = Cokleisli $ const $ tell . Dual . singleton $ t
3232
33− anyToken = Cokleisli $ tell . Dual . liftToken
33+ anyToken = Cokleisli $ tell . Dual . singleton
3434
3535 manyTokens = Cokleisli $ \w -> do
3636 tell $ Dual w
37− return . fromIntegral . length . lowerTokens $ w
37+ return . fromIntegral . olength $ w
3838
3939 tuck (Cokleisli f) = Cokleisli $ lift . fmap getDual . execWriterT . f
src/Data/Isoparsec/ToIsoparsec.hs+2−3
@@ -5,11 +5,10 @@ where
55
66import Control.Arrow.Extra
77import Data.Isoparsec.Internal
8−import Data.Isoparsec.Tokenable
98import GHC.Generics
109import Prelude hiding ((.))
1110
12−instance {-# OVERLAPPABLE #-} t ~ Token s => ToIsoparsec t s where
11+instance {-# OVERLAPPABLE #-} t ~ Element s => ToIsoparsec t s where
1312 toIsoparsec = anyToken
1413
1514class ToIsoparsec a s where
@@ -21,7 +20,7 @@ class GToIsoparsec a s where
2120 gToIsoparsec :: Isoparsec m s => m () (a b)
2221
2322instance GToIsoparsec U1 s where
24− gToIsoparsec = konst U1
23+ gToIsoparsec = arr $ konst U1
2524
2625instance ToIsoparsec c s => GToIsoparsec (K1 i c) s where
2726 gToIsoparsec =
src/Data/Isoparsec/Tokenable.hsdeleted−24
@@ -1,24 +0,0 @@
1−module Data.Isoparsec.Tokenable
2− ( Tokenable (..),
3− )
4−where
5−
6−class Monoid s => Tokenable s where
7− type Token s
8−
9− {-# MINIMAL (liftTokens | liftToken), lowerTokens #-}
10−
11− liftTokens :: [Token s] -> s
12− liftTokens = mconcat . fmap liftToken
13−
14− liftToken :: Token s -> s
15− liftToken = liftTokens . pure
16−
17− lowerTokens :: s -> [Token s]
18−
19−instance Tokenable [t] where
20− type Token [t] = t
21−
22− liftTokens = id
23−
24− lowerTokens = id
stack.yaml−5
@@ -39,11 +39,6 @@ packages:
3939# - git: https://github.com/commercialhaskell/stack.git
4040# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
4141#
42−extra-deps:
43−- optics-core-0.1
44−- optics-0.1
45−- optics-extra-0.1
46−- optics-th-0.1
4742
4843# Override default flag values for local packages and extra-deps
4944# flags: {}
test/Spec/Helper.hs+2−1
@@ -75,8 +75,9 @@ roundtrip ::
7575 ( ToIsoparsec x s,
7676 Stream s,
7777 Show s,
78− I.Token s ~ M.Token s,
78+ I.Element s ~ M.Token s,
7979 Eq x,
80+ Eq s,
8081 Isoparsec (Kleisli (Parsec Void s)) s
8182 ) =>
8283 x ->
test/Spec/JSON.hs+4−4
@@ -62,13 +62,13 @@ json = SI pure pure ^<< (string <+> array <+> integer <+> object)
6262 &&& repeating
6363 ( (token ',' &&& unsafeWhiteSpace &&& json) >>% morphed
6464 )
65− <+> konst []
65+ <+^ konst []
6666 )
6767 >>% morphed
6868 )
6969 >>^ siCons
7070 )
71− <+> konst []
71+ <+^ konst []
7272 )
7373 &&& unsafeWhiteSpace
7474 &&& token ']'
@@ -76,9 +76,9 @@ json = SI pure pure ^<< (string <+> array <+> integer <+> object)
7676 object =
7777 _Object <.> token '{' &&& unsafeWhiteSpace
7878 &&& ( ( let pair = (unsafeWhiteSpace &&& string' &&& unsafeWhiteSpace &&& token ':' &&& unsafeWhiteSpace &&& json &&& unsafeWhiteSpace) >>^ morphed
79− in (pair &&& (repeating ((token ',' &&& unsafeWhiteSpace &&& pair) >>^ morphed) <+> konst [])) >>^ siCons
79+ in (pair &&& (repeating ((token ',' &&& unsafeWhiteSpace &&& pair) >>^ morphed) <+^ konst [])) >>^ siCons
8080 )
81− <+> konst []
81+ <+^ konst []
8282 )
8383 &&& unsafeWhiteSpace
8484 &&& token '}'
test/Spec/Orphans.hs+3−3
@@ -15,13 +15,13 @@ import Test.Tasty.QuickCheck
1515import Prelude hiding ((.))
1616
1717instance
18− (CmpNat n 0 ~ 'GT, KnownNat n, Arbitrary (Token s), Tokenable s) =>
18+ (CmpNat n 0 ~ 'GT, KnownNat n, Arbitrary (Element s), IsSequence s) =>
1919 Arbitrary (Chunk n s)
2020 where
21− arbitrary = Chunk . liftTokens <$> vectorOf (fromIntegral $ natVal @n Proxy) arbitrary
21+ arbitrary = Chunk . fromList <$> vectorOf (fromIntegral $ natVal @n Proxy) arbitrary
2222
2323instance Arbitrary ByteString where
24− arbitrary = liftTokens <$> listOf arbitrary
24+ arbitrary = fromList <$> listOf arbitrary
2525
2626deriving instance Arbitrary (Byte16 e)
2727
test/Spec/Ssh.hs+1−1
@@ -137,7 +137,7 @@ data NoneMAC = NoneMAC
137137 deriving (Eq, Ord, Show, Generic)
138138
139139instance ToIsoparsec NoneMAC b where
140− toIsoparsec = konst NoneMAC
140+ toIsoparsec = arr $ konst NoneMAC
141141
142142instance Arbitrary NoneMAC where
143143 arbitrary = return NoneMAC
test/Spec/TwoDigits.hs+2−2
@@ -26,8 +26,8 @@ instance ToIsoparsec SingleDigit String where
2626 toIsoparsec =
2727 toIsoparsec @(Chunk 1 String)
2828 >>> coercing
29− >>> check (all @[] isDigit)
30− >>> coercing
29+ ^>> check (all @[] isDigit)
30+ ^>^ coercing
3131
3232instance (Arbitrary SingleDigit) where
3333 arbitrary = SingleDigit . pure <$> elements ['0' .. '9']