Add foldAppl

This commit is contained in:
Joe Hendrix 2017-03-05 23:56:37 -08:00
parent 40b92c901d
commit 2d23dba229
No known key found for this signature in database
GPG Key ID: 00F67DE32381DB9F
2 changed files with 23 additions and 0 deletions

View File

@ -62,6 +62,7 @@ module Data.Macaw.CFG
, appWidth , appWidth
, mapApp , mapApp
, foldApp , foldApp
, foldAppl
, traverseApp , traverseApp
-- * RegState -- * RegState
, RegState , RegState
@ -430,8 +431,10 @@ data App (f :: Type -> *) (tp :: Type) where
-> App f BoolType -> App f BoolType
FPCvt :: !(FloatInfoRepr flt) FPCvt :: !(FloatInfoRepr flt)
-- ^ Input float type
-> !(f (FloatType flt)) -> !(f (FloatType flt))
-> !(FloatInfoRepr flt') -> !(FloatInfoRepr flt')
-- ^ Output float type
-> App f (FloatType flt') -> App f (FloatType flt')
FPCvtRoundsUp :: !(FloatInfoRepr flt) FPCvtRoundsUp :: !(FloatInfoRepr flt)
@ -497,6 +500,23 @@ mapApp f m = runIdentity $ traverseApp (return . f) m
foldApp :: Monoid m => (forall u. f u -> m) -> App f tp -> m foldApp :: Monoid m => (forall u. f u -> m) -> App f tp -> m
foldApp f m = getConst (traverseApp (\f_u -> Const $ f f_u) m) foldApp f m = getConst (traverseApp (\f_u -> Const $ f f_u) m)
newtype FoldFn s a = FoldFn { getFoldFn :: s -> s }
instance Functor (FoldFn s) where
fmap _ (FoldFn g) = FoldFn g
instance Applicative (FoldFn s) where
pure _ = FoldFn id
FoldFn g <*> FoldFn h = FoldFn (\s -> h (g s))
-- | Left-fold over all values in the app
foldAppl :: forall f s tp . (forall u . s -> f u -> s) -> s -> App f tp -> s
foldAppl f s0 a = getFoldFn (traverseApp go a) s0
where go :: f u -> FoldFn s (f u)
go v = FoldFn (\s -> f s v)
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- App pretty printing -- App pretty printing

View File

@ -14,6 +14,7 @@
{-# LANGUAGE GADTs #-} {-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE ConstraintKinds #-}
@ -133,6 +134,8 @@ data FloatInfoRepr (flt::FloatInfo) where
QuadFloatRepr :: FloatInfoRepr QuadFloat QuadFloatRepr :: FloatInfoRepr QuadFloat
HalfFloatRepr :: FloatInfoRepr HalfFloat HalfFloatRepr :: FloatInfoRepr HalfFloat
deriving instance Show (FloatInfoRepr tp)
instance TestEquality FloatInfoRepr where instance TestEquality FloatInfoRepr where
testEquality x y = orderingF_refl (compareF x y) testEquality x y = orderingF_refl (compareF x y)