@@ -98,7 +98,6 @@ import Prologue
9898import Prelude hiding (fail )
9999import qualified Assigning.Assignment.Table as Table
100100import Control.Monad.Except (MonadError (.. ))
101- import Control.Monad.Free.Freer
102101import Data.AST
103102import Data.Error
104103import Data.Range
@@ -200,7 +199,7 @@ choice :: (Enum grammar, Eq1 ast, Ix grammar, HasCallStack) => [Assignment ast g
200199choice [] = empty
201200choice alternatives
202201 | null choices = asum alternatives
203- | otherwise = tracing (Choose (Table. fromListWith (<|>) choices) (wrap . tracing . Alt . toList <$> nonEmpty atEnd) (mergeHandlers handlers)) `Then ` pure
202+ | otherwise = tracing (Choose (Table. fromListWith (<|>) choices) ((` Then ` id ) . tracing . Alt . toList <$> nonEmpty atEnd) (mergeHandlers handlers)) `Then ` pure
204203 where (choices, atEnd, handlers) = foldMap toChoices alternatives
205204 toChoices :: (Enum grammar , Ix grammar ) => Assignment ast grammar a -> ([(grammar , Assignment ast grammar a )], [Assignment ast grammar a ], [Error (Either String grammar ) -> Assignment ast grammar a ])
206205 toChoices rule = case rule of
@@ -356,7 +355,7 @@ instance MonadFail (Assignment ast grammar) where
356355 fail :: HasCallStack => String -> Assignment ast grammar a
357356 fail s = tracing (Fail s) `Then ` pure
358357
359- instance (Enum grammar , Eq1 ast , Ix grammar , Show grammar , Show1 ast ) => Parsing (Assignment ast grammar ) where
358+ instance (Enum grammar , Eq1 ast , Ix grammar , Show grammar ) => Parsing (Assignment ast grammar ) where
360359 try = id
361360
362361 (<?>) :: HasCallStack => Assignment ast grammar a -> String -> Assignment ast grammar a
@@ -369,7 +368,7 @@ instance (Enum grammar, Eq1 ast, Ix grammar, Show grammar, Show1 ast) => Parsing
369368 eof = tracing End `Then ` pure
370369
371370 notFollowedBy :: (HasCallStack , Show a ) => Assignment ast grammar a -> Assignment ast grammar ()
372- notFollowedBy a = a *> unexpected ( show a ) <|> pure ()
371+ notFollowedBy a = (a >>= unexpected . show ) <|> pure ()
373372
374373instance (Enum grammar , Eq1 ast , Ix grammar , Show grammar ) => MonadError (Error (Either String grammar )) (Assignment ast grammar ) where
375374 throwError :: HasCallStack => Error (Either String grammar ) -> Assignment ast grammar a
@@ -381,22 +380,55 @@ instance (Enum grammar, Eq1 ast, Ix grammar, Show grammar) => MonadError (Error
381380 Choose choices atEnd (Just onError) -> Tracing cs (Choose (fmap (>>= continue) choices) (fmap (>>= continue) atEnd) (Just (\ err -> (onError err >>= continue) <|> handler err))) `Then ` pure
382381 _ -> Tracing cs assignment `Then ` ((`catchError` handler) . continue)) (fmap pure rule)
383382
384- instance Show1 f => Show1 (Tracing f ) where
385- liftShowsPrec sp sl d = liftShowsPrec sp sl d . runTracing
386-
387- instance (Enum grammar , Ix grammar , Show grammar , Show1 ast ) => Show1 (AssignmentF ast grammar ) where
388- liftShowsPrec sp sl d a = case a of
389- End -> showString " End" . showChar ' ' . sp d ()
390- Location -> showString " Location" . sp d (L. Location (Range 0 0 ) (Span (Pos 1 1 ) (Pos 1 1 )))
391- CurrentNode -> showString " CurrentNode"
392- Source -> showString " Source" . showChar ' ' . sp d " "
393- Children a -> showsUnaryWith showChild " Children" d a
394- Choose choices atEnd _ -> showsBinaryWith (liftShowsPrec showChild showChildren) (liftShowsPrec showChild showChildren) " Choose" d choices atEnd
395- Many a -> showsUnaryWith (liftShowsPrec (\ d a -> sp d [a]) (sl . pure )) " Many" d a
396- Alt as -> showsUnaryWith (const sl) " Alt" d (toList as)
397- Label child string -> showsBinaryWith (liftShowsPrec sp sl) showsPrec " Label" d child string
398- Fail s -> showsUnaryWith showsPrec " Fail" d s
399- GetLocals -> showString " GetLocals"
400- PutLocals locals -> showsUnaryWith showsPrec " PutLocals" d locals
401- where showChild = liftShowsPrec sp sl
402- showChildren = liftShowList sp sl
383+
384+ -- Freer
385+
386+ data Freer f a where
387+ Return :: a -> Freer f a
388+ Then :: f x -> (x -> Freer f a ) -> Freer f a
389+
390+ infixl 1 `Then `
391+
392+ instance Functor (Freer f ) where
393+ fmap f = go
394+ where go (Return result) = Return (f result)
395+ go (Then step yield) = Then step (go . yield)
396+ {-# INLINE go #-}
397+ {-# INLINE fmap #-}
398+
399+ instance Applicative (Freer f ) where
400+ pure = Return
401+ {-# INLINE pure #-}
402+
403+ Return f <*> param = fmap f param
404+ Then action yield <*> param = Then action ((<*> param) . yield)
405+ {-# INLINE (<*>) #-}
406+
407+ Return _ *> a = a
408+ Then r f *> a = Then r ((*> a) . f)
409+ {-# INLINE (*>) #-}
410+
411+ Return a <* b = b *> Return a
412+ Then r f <* a = Then r ((<* a) . f)
413+ {-# INLINE (<*) #-}
414+
415+ instance Monad (Freer f ) where
416+ return = pure
417+ {-# INLINE return #-}
418+
419+ Return a >>= f = f a
420+ Then r f >>= g = Then r (g <=< f)
421+ {-# INLINE (>>=) #-}
422+
423+ (>>) = (*>)
424+ {-# INLINE (>>) #-}
425+
426+ -- | Tear down a 'Freer' 'Monad' using iteration with an explicit continuation.
427+ --
428+ -- This is analogous to 'iter' with a continuation for the interior values, and is therefore suitable for defining interpreters for GADTs/types lacking a 'Functor' instance.
429+ iterFreer :: (forall x . (x -> a ) -> f x -> a ) -> Freer f a -> a
430+ iterFreer algebra = go
431+ where go (Return result) = result
432+ go (Then action continue) = algebra (go . continue) action
433+ {-# INLINE go #-}
434+ {-# INLINE iterFreer #-}
0 commit comments