Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit ac8bd44

Browse files
author
Patrick Thomson
authored
Merge pull request #36 from github/if-any-monad-is-not-freer-then-I-too-am-bound
Copy Freer in to Assignment, 🔥 dependency on freer-cofreer
2 parents d8342ed + 1932332 commit ac8bd44

File tree

4 files changed

+55
-28
lines changed

4 files changed

+55
-28
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
[submodule "vendor/haskell-tree-sitter"]
55
path = vendor/haskell-tree-sitter
66
url = https://github.com/tree-sitter/haskell-tree-sitter.git
7-
[submodule "vendor/freer-cofreer"]
8-
path = vendor/freer-cofreer
9-
url = https://github.com/robrix/freer-cofreer.git
107
[submodule "vendor/proto3-suite"]
118
path = vendor/proto3-suite
129
url = https://github.com/joshvera/proto3-suite.git

semantic.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ library
290290
, cryptohash ^>= 0.11.9
291291
, deepseq ^>= 1.4.4.0
292292
, directory-tree ^>= 0.12.1
293-
, freer-cofreer
294293
, generic-monoid ^>= 0.1.0.0
295294
, ghc-prim ^>= 0.5.3
296295
, gitrev ^>= 1.3.1

src/Assigning/Assignment.hs

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ import Prologue
9898
import Prelude hiding (fail)
9999
import qualified Assigning.Assignment.Table as Table
100100
import Control.Monad.Except (MonadError (..))
101-
import Control.Monad.Free.Freer
102101
import Data.AST
103102
import Data.Error
104103
import Data.Range
@@ -200,7 +199,7 @@ choice :: (Enum grammar, Eq1 ast, Ix grammar, HasCallStack) => [Assignment ast g
200199
choice [] = empty
201200
choice 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

374373
instance (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 #-}

vendor/freer-cofreer

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)