Remove Data.Fix, make recursion manually

This commit is contained in:
Kirill Andreev 2020-06-07 23:51:44 +04:00
parent 19f05f5afc
commit 44defb2114
No known key found for this signature in database
GPG Key ID: CF7DA79DE4785A47
3 changed files with 35 additions and 41 deletions

View File

@ -4,7 +4,6 @@ dependencies:
- base - base
- bytestring - bytestring
- data-default - data-default
- data-fix
- mtl - mtl
- pretty - pretty
- template-haskell - template-haskell

View File

@ -326,14 +326,14 @@ instance UpdateOver ScopeM Name (Pascal a)
-- deriving (Show) via PP (Name it) -- deriving (Show) via PP (Name it)
-- deriving stock (Functor, Foldable, Traversable) -- deriving stock (Functor, Foldable, Traversable)
data Scope = Scope { unScope :: Text } data Scope = Scope { unScope :: [Text] }
instance HasComments Scope where instance HasComments Scope where
getComments = pure . ("(* " <>) . (<> " *)") . unScope getComments = unScope
_testUpdate :: Pascal ASTInfo -> ScopeM (Pascal Scope) currentScope :: ASTInfo -> ScopeM Scope
_testUpdate = updateTree \_ -> do currentScope _ = do
Env topmost <- gets head Env topmost <- gets head
let names = _sdName <$> topmost let names = _sdName <$> topmost
let res = ppToText $ fsep $ map pp names let res = map ppToText names
return $ Scope res return $ Scope res

View File

@ -13,7 +13,7 @@ module Tree
( -- * Tree type ( -- * Tree type
Tree Tree
, spineTo , spineTo
, updateTree , traverseTree
, mk , mk
, infoOf , infoOf
@ -23,10 +23,6 @@ module Tree
) )
where where
import Data.Fix
import Data.Functor.Compose
import Data.Foldable
import Union import Union
import Lattice import Lattice
import Comment import Comment
@ -40,26 +36,25 @@ import Error
-- Can contain `Error` instead of all the above. -- Can contain `Error` instead of all the above.
-- --
newtype Tree layers info = Tree newtype Tree layers info = Tree
{ unTree :: Fix (Either (Error info) `Compose` (,) info `Compose` Union layers) { unTree :: Either (Error info) (info, Union layers (Tree layers info))
} }
instance (Functor (Union layers)) => Functor (Tree layers) where instance (Functor (Union layers)) => Functor (Tree layers) where
fmap f (Tree fixpoint) = Tree $ cata (Fix . go) fixpoint fmap f = go
where where
go (Compose (Left err)) = Compose $ Left $ fmap f err go (Tree (Left err)) = Tree $ Left $ fmap f err
go (Compose (Right (Compose (a, rest)))) = go (Tree (Right (a, rest))) = Tree $ Right (f a, fmap go rest)
Compose $ Right $ Compose (f a, rest)
instance (Functor (Union layers), Foldable (Union layers)) => Foldable (Tree layers) where instance (Functor (Union layers), Foldable (Union layers)) => Foldable (Tree layers) where
foldMap f (Tree fixpoint) = cata go fixpoint foldMap f = go
where where
go (Compose (Left err)) = foldMap f err go (Tree (Left err)) = foldMap f err
go (Compose (Right (Compose (a, rest)))) = f a <> fold rest go (Tree (Right (a, rest))) = f a <> foldMap go rest
instance instance
( Functor (Union layers) ( Functor (Union layers)
, HasComments info , HasComments info
, Pretty (Union layers Doc) , Pretty1 (Union layers)
, Pretty info , Pretty info
) )
=> =>
@ -70,16 +65,16 @@ instance
instance {-# OVERLAPS #-} instance {-# OVERLAPS #-}
( HasComments info ( HasComments info
, Functor (Union fs) , Functor (Union fs)
, Pretty (Union fs Doc) , Pretty1 (Union fs)
, Pretty info , Pretty info
) )
=> =>
Pretty (Tree fs info) Pretty (Tree fs info)
where where
pp (Tree it) = cata aux it pp = go
where where
aux (Compose (Left err)) = pp err go (Tree (Left err)) = pp err
aux (Compose (Right (Compose (info, fTree)))) = c info $ pp fTree go (Tree (Right (info, fTree))) = c info $ pp fTree
-- | Return all subtrees that cover the range, ascending in size. -- | Return all subtrees that cover the range, ascending in size.
spineTo spineTo
@ -89,11 +84,11 @@ spineTo
=> info => info
-> Tree fs info -> Tree fs info
-> [Tree fs info] -> [Tree fs info]
spineTo info = reverse . go . unTree spineTo info = reverse . go
where where
go tree@(Fix (Compose (Right (Compose (info', fres))))) = go tree@(Tree (Right (info', fres))) =
if info <? info' if info <? info'
then Tree tree : foldMap go fres then tree : foldMap go fres
else [] else []
go _ = [] go _ = []
@ -102,40 +97,40 @@ spineTo info = reverse . go . unTree
-- --
-- For each tree piece, will call `before` and `after` callbacks. -- For each tree piece, will call `before` and `after` callbacks.
-- --
updateTree traverseTree
:: ( UpdateOver m (Union fs) (Tree fs a) :: ( UpdateOver m (Union fs) (Tree fs a)
, Traversable (Union fs) , Traversable (Union fs)
) )
=> (a -> m b) -> Tree fs a -> m (Tree fs b) => (a -> m b) -> Tree fs a -> m (Tree fs b)
updateTree act = fmap Tree . go . unTree traverseTree act = go
where where
go (Fix (Compose (Right (Compose (a, union))))) = do go (Tree (Right (a, union))) = do
b <- act a b <- act a
before (Tree <$> union) before union
union' <- traverse go union union' <- traverse go union
after (Tree <$> union) after union
return (Fix (Compose (Right (Compose (b, union'))))) return (Tree (Right (b, union')))
go (Fix (Compose (Left err))) = do go (Tree (Left err)) = do
err' <- traverse act err err' <- traverse act err
return (Fix (Compose (Left err'))) return (Tree (Left err'))
-- | Make a tree out of a layer and an info. -- | Make a tree out of a layer and an info.
mk :: (Functor f, Member f fs) => info -> f (Tree fs info) -> Tree fs info mk :: (Functor f, Member f fs) => info -> f (Tree fs info) -> Tree fs info
mk i fx = Tree $ Fix $ Compose $ Right $ Compose (i, inj $ fmap unTree fx) mk i fx = Tree $ Right (i, inj fx)
-- | Get info from the tree. -- | Get info from the tree.
infoOf :: Tree fs info -> info infoOf :: Tree fs info -> info
infoOf = either eInfo (fst . getCompose) . getCompose . unFix . unTree infoOf = either eInfo fst . unTree
instance Stubbed (Tree fs info) info where instance Stubbed (Tree fs info) info where
stub = Tree . Fix . Compose . Left stub = Tree . Left
instance Foldable (Union fs) => HasErrors (Tree fs info) info where instance Foldable (Union fs) => HasErrors (Tree fs info) info where
errors = go . unTree errors = go
where where
go (Fix (Compose (Left err))) = pure err go (Tree (Left err)) = pure err
go (Fix rest) = foldMap go rest go (Tree (Right (_, rest))) = foldMap go rest
-- | Update callbacks for a @f a@ while working inside monad @m@. -- | Update callbacks for a @f a@ while working inside monad @m@.
class Monad m => UpdateOver m f a where class Monad m => UpdateOver m f a where