1 -- The default type signature of type class methods are changed
2 -- to introduce a 'LiftDerived'* constraint and the same type class but on the 'Derived' repr,
3 -- this setup avoids to define the method with boilerplate code when its default
4 -- definition with 'liftDerived'* and 'derive' does what is expected by an instance
5 -- of the type class. This is almost as explained in:
6 -- https://ro-che.info/articles/2016-02-03-finally-tagless-boilerplate
7 {-# LANGUAGE DefaultSignatures #-}
8 {-# LANGUAGE DeriveGeneric #-} -- For NFData instances
9 {-# LANGUAGE DeriveAnyClass #-} -- For NFData instances
10 {-# LANGUAGE DeriveLift #-} -- For TH.Lift (Exception tok)
11 {-# LANGUAGE DerivingStrategies #-} -- For UnscopedRegister
12 {-# LANGUAGE PatternSynonyms #-} -- For Failure
13 {-# LANGUAGE StandaloneDeriving #-} -- For Show (Exception (InputToken inp))
14 {-# LANGUAGE InstanceSigs #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE ViewPatterns #-} -- For unSomeFailure
17 -- | Semantic of the grammar combinators used to express parsers,
18 -- in the convenient tagless-final encoding.
19 module Symantic.Parser.Grammar.Combinators where
21 import Data.Proxy (Proxy(..))
22 import Control.Monad (Monad(..))
23 import Control.DeepSeq (NFData(..))
24 import GHC.Generics (Generic)
25 -- import Data.Set (Set)
26 -- import GHC.TypeLits (KnownSymbol)
27 import Data.Bifunctor (second)
28 import Data.Bool (Bool(..), not, (||))
29 import Data.Char (Char)
30 import Data.Either (Either(..))
31 import Data.Eq (Eq(..))
32 import Data.Function ((.), flip, const, fix)
33 import Data.Ord (Ord(..), Ordering(..))
35 import Data.Kind (Type, Constraint)
36 import Data.Maybe (Maybe(..))
38 import Data.String (String)
39 import Text.Show (Show(..))
40 import Type.Reflection (Typeable, typeRep, eqTypeRep, (:~~:)(..), SomeTypeRep(..))
41 import qualified Data.Functor as Functor
42 import qualified Data.List as List
43 import qualified Data.Set as Set
44 import qualified Language.Haskell.TH as TH
45 import qualified Language.Haskell.TH.Syntax as TH
47 import Symantic.Derive
48 import qualified Symantic.Class as Prod
49 import Symantic.Parser.Grammar.Production
52 type ReprComb = Type -> Type
54 -- * Class 'CombAlternable'
55 class CombAlternable repr where
56 -- | @('alt' es l r)@ parses @(l)@ and return its return value or,
57 -- if it fails with an 'Exception' within @(es)@,
58 -- parses @(r)@ from where @(l)@ has left the input stream,
59 -- and returns its return value,
60 -- otherwise throw the 'Exception' again.
61 alt :: Exception -> repr a -> repr a -> repr a
62 throw :: ExceptionLabel -> repr a
63 -- | @('try' ra)@ records the input stream position,
64 -- then parses like @(ra)@ and either returns its value it it succeeds or fails
65 -- if it fails but with a reset of the input stream to the recorded position.
66 -- Generally used on the first alternative: @('try' rl '<|>' rr)@.
67 try :: repr a -> repr a
69 FromDerived2 CombAlternable repr =>
70 Exception -> repr a -> repr a -> repr a
72 FromDerived CombAlternable repr =>
73 ExceptionLabel -> repr a
75 FromDerived1 CombAlternable repr =>
77 alt = liftDerived2 . alt
78 throw = liftDerived . throw
79 try = liftDerived1 try
81 failure :: SomeFailure -> repr a
83 FromDerived CombAlternable repr =>
85 failure = liftDerived . failure
87 -- | @(empty)@ parses nothing, always failing to return a value.
89 empty = failure (SomeFailure FailureEmpty)
91 data instance Failure CombAlternable
93 deriving (Eq, Ord, Show, TH.Lift, Generic, NFData)
95 -- ** Data family 'Failure'
96 -- | 'Failure's of the 'Grammar'.
97 -- This is an extensible data-type.
99 (comb :: ReprComb -> Constraint)
103 -- | Convenient utility to pattern-match a 'SomeFailure'.
104 pattern Failure :: Typeable comb => Failure comb -> SomeFailure
105 pattern Failure x <- (unSomeFailure -> Just x)
108 -- ** Type 'SomeFailure'
113 , Show (Failure comb)
114 , TH.Lift (Failure comb)
115 , NFData (Failure comb)
117 ) => SomeFailure (Failure comb {-repr a-})
118 instance Eq SomeFailure where
119 SomeFailure (x::Failure x) == SomeFailure (y::Failure y) =
120 case typeRep @x `eqTypeRep` typeRep @y of
123 instance Ord SomeFailure where
124 SomeFailure (x::Failure x) `compare` SomeFailure (y::Failure y) =
125 -- WARNING: this ordering is convenient to make a 'Set' of 'SomeFailure's
126 -- but it is based upon a hash which changes with packages' ABI
127 -- and also if the install is "inplace" or not.
128 -- Therefore this 'Ord' is not stable enough to put 'SomeFailure'
130 let xT = typeRep @x in
131 let yT = typeRep @y in
132 case SomeTypeRep xT `compare` SomeTypeRep yT of
133 EQ | Just HRefl <- xT `eqTypeRep` yT -> compare x y
135 instance Show SomeFailure where
136 showsPrec p (SomeFailure x) = showsPrec p x
137 instance TH.Lift SomeFailure where
138 liftTyped (SomeFailure x) = [|| SomeFailure $$(TH.liftTyped x) ||]
139 instance NFData SomeFailure where
140 rnf (SomeFailure x) = rnf x
143 instance Derivable (SomeFailure repr) where
144 derive (SomeFailure x) = derive x
147 -- | @(unSomeFailure c :: 'Maybe' ('Failure' comb repr a))@
148 -- extract the data-constructor from the given 'SomeFailure'
149 -- iif. it belongs to the @('Failure' comb repr a)@ data-instance.
150 unSomeFailure :: forall comb. Typeable comb => SomeFailure -> Maybe (Failure comb)
151 unSomeFailure (SomeFailure (c::Failure c)) =
152 case typeRep @comb `eqTypeRep` typeRep @c of
156 -- ** Type 'Exception'
158 = ExceptionLabel ExceptionLabel
160 deriving (Eq, Ord, Show, TH.Lift, Generic, NFData)
161 type ExceptionLabel = String
162 -- type Exceptions = Set Exception
164 -- | Like @('<|>')@ but with different returning types for the alternatives,
165 -- and a return value wrapped in an 'Either' accordingly.
166 (<+>) :: CombApplicable repr => CombAlternable repr => repr a -> repr b -> repr (Either a b)
167 p <+> q = Prod.left <$> p <|> Prod.right <$> q
169 (<|>) :: CombAlternable repr => repr a -> repr a -> repr a
170 (<|>) = alt ExceptionFailure
174 optionally :: CombApplicable repr => CombAlternable repr => repr a -> Production b -> repr b
175 optionally p x = p $> x <|> pure x
177 optional :: CombApplicable repr => CombAlternable repr => repr a -> repr ()
178 optional = flip optionally (Prod.constant ())
180 option :: CombApplicable repr => CombAlternable repr => Production a -> repr a -> repr a
181 option x p = p <|> pure x
183 choice :: CombAlternable repr => [repr a] -> repr a
184 choice = List.foldr (<|>) empty
185 -- FIXME: Here hlint suggests to use Data.Foldable.asum,
186 -- but at this point there is no asum for our own (<|>)
188 maybeP :: CombApplicable repr => CombAlternable repr => repr a -> repr (Maybe a)
189 maybeP p = option Prod.nothing (Prod.just <$> p)
191 manyTill :: CombApplicable repr => CombAlternable repr => repr a -> repr b -> repr [a]
192 manyTill p end = let go = end $> Prod.nil <|> p <:> go in go
194 -- * Class 'CombApplicable'
195 -- | This is like the usual 'Functor' and 'Applicative' type classes
196 -- from the @base@ package, but using @('Production' a)@ instead of just @(a)@
197 -- to be able to use and pattern match on some usual terms of type @(a)@ (like 'Prod.id')
198 -- and thus apply some optimizations.
199 -- @(repr)@, for "representation", is the usual tagless-final abstraction
200 -- over the many semantics that this syntax (formed by the methods
201 -- of type class like this one) will be interpreted.
202 class CombApplicable repr where
203 -- | @(a2b '<$>' ra)@ parses like @(ra)@ but maps its returned value with @(a2b)@.
204 (<$>) :: Production (a -> b) -> repr a -> repr b
205 (<$>) f = (pure f <*>)
206 (<$>%) :: (Production a -> Production b) -> repr a -> repr b
207 a2b <$>% ma = Prod.lam a2b <$> ma
209 -- | Like '<$>' but with its arguments 'flip'-ped.
210 (<&>) :: repr a -> Production (a -> b) -> repr b
213 -- | @(a '<$' rb)@ parses like @(rb)@ but discards its returned value by replacing it with @(a)@.
214 (<$) :: Production a -> repr b -> repr a
217 -- | @(ra '$>' b)@ parses like @(ra)@ but discards its returned value by replacing it with @(b)@.
218 ($>) :: repr a -> Production b -> repr b
221 -- | @('pure' a)@ parses the empty string, always succeeding in returning @(a)@.
222 pure :: Production a -> repr a
224 FromDerived CombApplicable repr =>
225 Production a -> repr a
226 pure = liftDerived . pure
228 -- | @(ra2b '<*>' ra)@ parses sequentially @(ra2b)@ and then @(ra)@,
229 -- and returns the application of the function returned by @(ra2b)@
230 -- to the value returned by @(ra)@.
231 (<*>) :: repr (a -> b) -> repr a -> repr b
233 FromDerived2 CombApplicable repr =>
234 repr (a -> b) -> repr a -> repr b
235 (<*>) = liftDerived2 (<*>)
237 -- | @(ra '<*' rb)@ parses sequentially @(ra)@ and then @(rb)@,
238 -- and returns like @(ra)@, discarding the return value of @(rb)@.
239 (<*) :: repr a -> repr b -> repr a
240 (<*) = liftA2 Prod.const
242 -- | @(ra '*>' rb)@ parses sequentially @(ra)@ and then @(rb)@,
243 -- and returns like @(rb)@, discarding the return value of @(ra)@.
244 (*>) :: repr a -> repr b -> repr b
245 x *> y = (Prod.id <$ x) <*> y
247 -- | Like '<*>' but with its arguments 'flip'-ped.
248 (<**>) :: repr a -> repr (a -> b) -> repr b
249 (<**>) = liftA2 (Prod.flip Prod..@ (Prod.$))
251 (<**>) :: repr a -> repr (a -> b) -> repr b
252 (<**>) = liftA2 (\a f -> f a)
254 -- | @('liftA2' a2b2c ra rb)@ parses sequentially @(ra)@ and then @(rb)@,
255 -- and returns the application of @(a2b2c)@ to the values returned by those parsers.
256 liftA2 :: Production (a -> b -> c) -> repr a -> repr b -> repr c
257 liftA2 f x = (<*>) (f <$> x)
259 infixl 4 <*>, <*, *>, <**>
260 data instance Failure CombApplicable
265 (<:>) :: CombApplicable repr => repr a -> repr [a] -> repr [a]
266 (<:>) = liftA2 Prod.cons
268 sequence :: CombApplicable repr => [repr a] -> repr [a]
269 sequence = List.foldr (<:>) (pure Prod.nil)
271 traverse :: CombApplicable repr => (a -> repr b) -> [a] -> repr [b]
272 traverse f = sequence . List.map f
273 -- FIXME: Here hlint suggests to use Control.Monad.mapM,
274 -- but at this point there is no mapM for our own sequence
276 repeat :: CombApplicable repr => Int -> repr a -> repr [a]
277 repeat n p = traverse (const p) [1..n]
279 between :: CombApplicable repr => repr o -> repr c -> repr a -> repr a
280 between open close p = open *> p <* close
282 void :: CombApplicable repr => repr a -> repr ()
285 unit :: CombApplicable repr => repr ()
286 unit = pure (Prod.constant ())
288 -- * Class 'CombFoldable'
289 class CombFoldable repr where
290 chainPre :: repr (a -> a) -> repr a -> repr a
291 chainPost :: repr a -> repr (a -> a) -> repr a
292 chainPre = liftDerived2 chainPre
293 chainPost = liftDerived2 chainPost
295 FromDerived2 CombFoldable repr =>
296 repr (a -> a) -> repr a -> repr a
298 FromDerived2 CombFoldable repr =>
299 repr a -> repr (a -> a) -> repr a
302 CombApplicable repr =>
303 CombAlternable repr =>
304 repr (a -> a) -> repr a -> repr a
306 CombApplicable repr =>
307 CombAlternable repr =>
308 repr a -> repr (a -> a) -> repr a
309 chainPre op p = go <*> p where go = (Prod..) <$> op <*> go <|> pure Prod.id
310 chainPost p op = p <**> go where go = (Prod..) <$> op <*> go <|> pure Prod.id
313 chainPre op p = flip (foldr ($)) <$> many op <*> p
314 chainPost p op = foldl' (flip ($)) <$> p <*> many op
316 data instance Failure CombFoldable
319 conditional :: CombSelectable repr => [(Production (a -> Bool), repr b)] -> repr a -> repr b -> repr b
320 conditional cs p def = match p fs qs def
321 where (fs, qs) = List.unzip cs
326 CombApplicable repr => CombFoldable repr =>
327 Production (a -> b -> b) -> Production b -> repr a -> repr b
328 pfoldr f k p = chainPre (f <$> p) (pure k)
331 CombApplicable repr => CombFoldable repr =>
332 Production (a -> b -> b) -> Production b -> repr a -> repr b
333 pfoldr1 f k p = f <$> p <*> pfoldr f k p
336 CombApplicable repr => CombFoldable repr =>
337 Production (b -> a -> b) -> Production b -> repr a -> repr b
338 pfoldl f k p = chainPost (pure k) ((Prod.flip <$> pure f) <*> p)
341 CombApplicable repr => CombFoldable repr =>
342 Production (b -> a -> b) -> Production b -> repr a -> repr b
343 pfoldl1 f k p = chainPost (f <$> pure k <*> p) ((Prod.flip <$> pure f) <*> p)
347 CombApplicable repr => CombFoldable repr =>
348 Production (a -> b) -> repr a -> repr (b -> a -> b) -> repr b
349 chainl1' f p op = chainPost (f <$> p) (Prod.flip <$> op <*> p)
352 CombApplicable repr => CombFoldable repr =>
353 repr a -> repr (a -> a -> a) -> repr a
354 chainl1 = chainl1' Prod.id
357 chainr1' :: ParserOps rep => rep (a -> b) -> repr a -> repr (a -> b -> b) -> repr b
358 chainr1' f p op = newRegister_ Prod.id $ \acc ->
359 let go = bind p $ \x ->
360 modify acc (Prod.flip (Prod..@) <$> (op <*> x)) *> go
364 chainr1 :: repr a -> repr (a -> a -> a) -> repr a
365 chainr1 = chainr1' Prod.id
367 chainr :: repr a -> repr (a -> a -> a) -> Production a -> repr a
368 chainr p op x = option x (chainr1 p op)
372 CombApplicable repr => CombAlternable repr => CombFoldable repr =>
373 repr a -> repr (a -> a -> a) -> Production a -> repr a
374 chainl p op x = option x (chainl1 p op)
376 -- Derived Combinators
378 CombApplicable repr => CombFoldable repr =>
380 many = pfoldr Prod.cons Prod.nil
383 CombApplicable repr => CombFoldable repr =>
384 Int -> repr a -> repr [a]
385 manyN n p = List.foldr (const (p <:>)) (many p) [1..n]
388 CombApplicable repr => CombFoldable repr =>
393 CombApplicable repr => CombFoldable repr =>
395 --skipMany p = let skipManyp = p *> skipManyp <|> unit in skipManyp
396 skipMany = void . pfoldl Prod.const Prod.unit -- the void here will encourage the optimiser to recognise that the register is unused
399 CombApplicable repr => CombFoldable repr =>
400 Int -> repr a -> repr ()
401 skipManyN n p = List.foldr (const (p *>)) (skipMany p) [1..n]
404 CombApplicable repr => CombFoldable repr =>
406 skipSome = skipManyN 1
409 CombApplicable repr => CombAlternable repr => CombFoldable repr =>
410 repr a -> repr b -> repr [a]
411 sepBy p sep = option Prod.nil (sepBy1 p sep)
414 CombApplicable repr => CombAlternable repr => CombFoldable repr =>
415 repr a -> repr b -> repr [a]
416 sepBy1 p sep = p <:> many (sep *> p)
419 CombApplicable repr => CombAlternable repr => CombFoldable repr =>
420 repr a -> repr b -> repr [a]
421 endBy p sep = many (p <* sep)
424 CombApplicable repr => CombAlternable repr => CombFoldable repr =>
425 repr a -> repr b -> repr [a]
426 endBy1 p sep = some (p <* sep)
429 CombApplicable repr => CombAlternable repr => CombFoldable repr =>
430 repr a -> repr b -> repr [a]
431 sepEndBy p sep = option Prod.nil (sepEndBy1 p sep)
434 CombApplicable repr => CombAlternable repr => CombFoldable repr =>
435 repr a -> repr b -> repr [a]
437 let seb1 = p <**> (sep *> (Prod.flip Prod..@ Prod.cons <$> option Prod.nil seb1)
438 <|> pure (Prod.flip Prod..@ Prod.cons Prod..@ Prod.nil))
442 sepEndBy1 :: repr a -> repr b -> repr [a]
443 sepEndBy1 p sep = newRegister_ Prod.id $ \acc ->
444 let go = modify acc ((Prod.flip (Prod..)) Prod..@ Prod.cons <$> p)
445 *> (sep *> (go <|> get acc) <|> get acc)
446 in go <*> pure Prod.nil
449 -- * Class 'CombMatchable'
450 class CombMatchable repr where
452 repr a -> [(Production (a -> Bool), repr b)] -> repr b -> repr b
453 conditional a bs = liftDerived1
454 (conditional (derive a) (second derive Functor.<$> bs))
455 default conditional ::
456 FromDerived1 CombMatchable repr => Derivable repr =>
457 repr a -> [(Production (a -> Bool), repr b)] -> repr b -> repr b
458 data instance Failure CombMatchable
461 CombMatchable repr =>
463 repr a -> [Production a] -> (Production a -> repr b) -> repr b -> repr b
464 match a as p = conditional a
466 ( Prod.lam (v Prod.==)
472 CombMatchable repr =>
473 Production (a -> Bool) -> repr a -> repr b -> repr b -> repr b
474 predicate p a b = conditional a [(p, b)]
478 CombMatchable repr =>
479 repr Bool -> (repr a, repr a) -> repr a
480 cond <?:> (p, q) = predicate Prod.id cond p q
482 -- * Class 'CombSatisfiable'
483 class CombSatisfiable tok repr where
484 -- | Like 'satisfyOrFail' but with no custom failure.
485 satisfy :: Production (tok -> Bool) -> repr tok
486 satisfy = satisfyOrFail Set.empty
487 -- | Like 'satisfy' but with a custom set of 'SomeFailure's.
490 Production (tok -> Bool) -> repr tok
491 default satisfyOrFail ::
492 FromDerived (CombSatisfiable tok) repr =>
494 Production (tok -> Bool) -> repr tok
495 satisfyOrFail fs = liftDerived . satisfyOrFail fs
497 data instance Failure (CombSatisfiable tok)
499 -- FIXME: this 'Failure' is a bit special since multiple ones
500 -- with different 'Horizon's makes no sense.
501 -- This should likely be treated separately in 'ParsingError'.
502 | FailureHorizon Int -- FIXME: use Natural?
503 | FailureLabel String
505 deriving (Eq, Ord, Show, Typeable, Generic, NFData)
506 -- | Global 'TH.Name' to refer to the @(InputToken inp)@ type
507 -- from TemplateHaskell code.
508 inputTokenProxy :: TH.Name
509 inputTokenProxy = TH.mkName "inputToken"
510 instance TH.Lift tok => TH.Lift (Failure (CombSatisfiable tok)) where
511 liftTyped :: forall m. TH.Quote m => Failure (CombSatisfiable tok) -> TH.Code m (Failure (CombSatisfiable tok))
514 $$(let inputToken :: TH.Code m (Proxy tok) =
515 TH.unsafeCodeCoerce (return (TH.VarE inputTokenProxy))
517 (Proxy :: Proxy tok') ->
519 FailureAny -> [|| FailureAny @tok' ||]
520 FailureHorizon h -> [|| FailureHorizon @tok' h ||]
521 FailureLabel lbl -> [|| FailureLabel @tok' lbl ||]
522 FailureToken tok -> [|| FailureToken $$(TH.liftTyped tok) ||]
527 CombApplicable repr =>
528 CombSatisfiable Char repr =>
530 char c = satisfyOrFail
531 (Set.singleton (SomeFailure (FailureToken c)))
532 (Prod.equal Prod..@ Prod.constant c)
535 item :: forall tok repr.
536 Ord tok => Show tok => Typeable tok => TH.Lift tok => NFData tok =>
537 CombSatisfiable tok repr => repr tok
539 (Set.singleton (SomeFailure (FailureAny @tok)))
540 (Prod.const Prod..@ Prod.constant True)
543 CombAlternable repr =>
544 CombSatisfiable Char repr =>
549 CombApplicable repr => CombAlternable repr =>
550 CombSatisfiable Char repr =>
551 [Char] -> repr [Char]
552 string = try . traverse char
555 Ord tok => Show tok => Typeable tok => TH.Lift tok => NFData tok =>
556 CombSatisfiable tok repr =>
558 oneOf ts = satisfyOrFail
559 (Set.fromList (SomeFailure . FailureToken Functor.<$> ts))
562 [||\t -> $$(ofChars ts [||t||])||])
565 TH.Lift tok => Eq tok =>
566 CombSatisfiable tok repr =>
568 noneOf cs = satisfy (production
569 (not . (`List.elem` cs))
570 [||\c -> not $$(ofChars cs [||c||])||])
573 TH.Lift tok => Eq tok =>
574 {-alternatives-}[tok] ->
575 {-input-}TH.CodeQ tok ->
578 (\tok acc inp -> [|| tok == $$inp || $$(acc inp) ||])
582 CombAlternable repr =>
583 CombApplicable repr =>
584 CombSatisfiable Char repr =>
585 CombLookable repr => repr ()
586 more = look (void (item @Char))
589 TH.Lift tok => Show tok => Eq tok => Typeable tok =>
590 CombAlternable repr =>
591 CombApplicable repr =>
592 CombSatisfiable tok repr =>
594 token tok = satisfy (Prod.equal Prod..@ Prod.constant tok) $> Prod.constant tok
595 -- token tok = satisfy [ExceptionToken tok] (Prod.eq Prod..@ Prod.qual Prod..@ Prod.char tok) $> Prod.char tok
598 TH.Lift tok => Eq tok => Show tok => Typeable tok =>
599 CombApplicable repr => CombAlternable repr =>
600 CombSatisfiable tok repr => [tok] -> repr [tok]
601 tokens = try . traverse token
603 -- * Class 'CombSelectable'
604 class CombSelectable repr where
605 branch :: repr (Either a b) -> repr (a -> c) -> repr (b -> c) -> repr c
607 FromDerived3 CombSelectable repr =>
608 repr (Either a b) -> repr (a -> c) -> repr (b -> c) -> repr c
609 branch = liftDerived3 branch
610 data instance Failure CombSelectable
613 CombMatchable repr =>
614 CombSelectable repr =>
615 Prod.Constantable () repr =>
616 repr Bool -> repr () -> repr ()
617 when p q = p <?:> (q, Prod.constant ())
620 CombMatchable repr =>
621 CombSelectable repr =>
622 Prod.Constantable () repr =>
624 while x = fix (when x)
626 -- * Class 'CombLookable'
627 class CombLookable repr where
628 look :: repr a -> repr a
629 negLook :: repr a -> repr ()
631 FromDerived1 CombLookable repr =>
634 FromDerived1 CombLookable repr =>
636 look = liftDerived1 look
637 negLook = liftDerived1 negLook
640 eof = liftDerived eof
642 FromDerived CombLookable repr =>
644 -- eof = negLook (satisfy @Char (Prod.const Prod..@ Prod.bool True))
646 data instance Failure CombLookable
648 deriving (Eq, Ord, Show, Typeable, TH.Lift, Generic, NFData)
650 -- Composite Combinators
651 -- someTill :: repr a -> repr b -> repr [a]
652 -- someTill p end = negLook end *> (p <:> manyTill p end)
655 constp :: CombApplicable repr => repr a -> repr (b -> a)
656 constp = (Prod.const <$>)
661 (>>) :: CombApplicable repr => repr a -> repr b -> repr b
664 -- Monoidal Operations
667 (<~>) :: CombApplicable repr => repr a -> repr b -> repr (a, b)
668 (<~>) = liftA2 (Prod.runtime (,))
671 (<~) :: CombApplicable repr => repr a -> repr b -> repr a
675 (~>) :: CombApplicable repr => repr a -> repr b -> repr b
680 CombApplicable repr =>
681 Production (a -> b -> c) -> repr a -> repr b -> repr c
682 liftA2 f x = (<*>) (fmap f x)
685 CombApplicable repr =>
686 Production (a -> b -> c -> d) -> repr a -> repr b -> repr c -> repr d
687 liftA3 f a b c = liftA2 f a b <*> c
692 -- Combinators interpreters for 'Sym.Any'.
693 instance CombApplicable repr => CombApplicable (Sym.Any repr)
694 instance CombSatisfiable repr => CombSatisfiable (Sym.Any repr)
695 instance CombAlternable repr => CombAlternable (Sym.Any repr)
696 instance CombSelectable repr => CombSelectable (Sym.Any repr)
697 instance CombMatchable repr => CombMatchable (Sym.Any repr)
698 instance CombLookable repr => CombLookable (Sym.Any repr)
699 instance CombFoldable repr => CombFoldable (Sym.Any repr)
703 newtype Register r a = Register { unRegister :: UnscopedRegister a }
706 -- ** Type 'UnscopedRegister'
707 newtype UnscopedRegister r = UnscopedRegister { unUnscopedRegister :: TH.Name }
709 deriving newtype Show
713 put_ :: ParserOps rep => Register r a -> rep a -> Parser ()
714 put_ r = put r . pure
716 gets_ :: ParserOps rep => Register r a -> rep (a -> b) -> Parser b
717 gets_ r = gets r . pure
719 modify_ :: ParserOps rep => Register r a -> rep (a -> a) -> Parser ()
720 modify_ r = modify r . pure
724 CombApplicable repr =>
725 CombRegisterable repr =>
726 Register r a -> repr (a -> b) -> repr b
727 gets r p = p <*> get r
730 CombApplicable repr =>
731 CombRegisterable repr =>
732 Register r a -> repr (a -> a) -> repr ()
733 modify r p = put r (gets r p)
736 CombRegisterable repr =>
737 Register r1 a -> Register r2 a -> repr ()
738 move dst src = put dst (get src)
741 CombRegisterable repr =>
742 repr a -> (repr a -> repr b) -> repr b
743 bind p f = new p (f . get)
746 CombApplicable repr =>
747 CombRegisterable repr =>
748 Register r a -> repr a -> repr b -> repr b
749 local r p q = bind (get r) (\x -> put r p *> q <* put r x)
752 CombApplicable repr =>
753 CombRegisterable repr =>
754 Register r1 a -> Register r2 a -> repr ()
755 swap r1 r2 = bind (get r1) (\x -> move r1 r2 *> put r2 x)
758 CombAlternable repr =>
759 CombApplicable repr =>
760 CombRegisterable repr =>
761 Register r a -> repr b -> repr b
762 rollback r p = bind (get r) (\x -> p <|> put r x *> empty)
765 CombApplicable repr =>
766 CombMatchable repr =>
767 CombSelectable repr =>
768 CombRegisterable repr =>
769 Prod.Constantable () repr =>
770 repr a -> repr (a -> Bool) -> repr (a -> a) -> repr () -> repr ()
771 for init cond step body =
773 let cond' = gets i cond in
774 when cond' (while (body *> modify i step *> cond'))
778 -- ** Class 'CombRegisterable'
779 class CombRegisterable (repr::ReprComb) where
780 new :: repr a -> (forall r. Register r a -> repr b) -> repr b
781 get :: Register r a -> repr a
782 put :: Register r a -> repr a -> repr ()
784 FromDerived CombRegisterable repr => Derivable repr =>
785 repr a -> (forall r. Register r a -> repr b) -> repr b
787 FromDerived CombRegisterable repr =>
788 Register r a -> repr a
790 FromDerived1 CombRegisterable repr =>
791 Register r a -> repr a -> repr ()
792 new ini f = liftDerived (new (derive ini) (derive . f))
793 get = liftDerived . get
794 put = liftDerived1 . put