Copyright | (c) 2014 Patrick Bahr Emil Axelsson |
---|---|
License | BSD3 |
Maintainer | Patrick Bahr <paba@di.ku.dk> |
Stability | experimental |
Portability | non-portable (GHC Extensions) |
Safe Haskell | None |
Language | Haskell98 |
This module implements recursion schemes derived from attribute grammars.
Synopsis
- runAG :: forall f u d. Traversable f => Syn' f (u, d) u -> Inh' f (u, d) d -> (u -> d) -> Term f -> u
- runRewrite :: forall f g u d. (Traversable f, Functor g) => Syn' f (u, d) u -> Inh' f (u, d) d -> Rewrite f (u, d) g -> (u -> d) -> Term f -> (u, Term g)
- class (Functor t, Foldable t) => Traversable (t :: * -> *)
- pr :: p :< q => q -> p
- type (:<) f g = Proj (ComprEmb (Elem f g)) f g
- lookupNumMap' :: Int -> NumMap t a -> Maybe a
- lookupNumMap :: a -> Int -> NumMap t a -> a
- prodMap :: Mapping m k => v1 -> v2 -> m v1 -> m v2 -> m (v1, v2)
- number :: Traversable f => f a -> f (Numbered a)
- unNumbered :: Numbered a -> a
- data Numbered a = Numbered Int a
- class Functor m => Mapping (m :: * -> *) k | m -> k where
- data NumMap k v
- type Inh f p q = q :< p => Inh' f p q
- type Inh' f p q = forall m i. (Mapping m i, ?below :: i -> p, ?above :: p) => f i -> m q
- type Syn f p q = q :< p => Syn' f p q
- type Syn' f p q = forall a. (?below :: a -> p, ?above :: p) => f a -> q
- type Rewrite f q g = forall a. (?below :: a -> q, ?above :: q) => f a -> Context g a
- below :: (?below :: child -> q, p :< q) => child -> p
- above :: (?above :: q, p :< q) => p
- prodSyn :: (p :< c, q :< c) => Syn f c p -> Syn f c q -> Syn f c (p, q)
- (|*|) :: (p :< c, q :< c) => Syn f c p -> Syn f c q -> Syn f c (p, q)
- prodInh :: (p :< c, q :< c) => Inh f c p -> Inh f c q -> Inh f c (p, q)
- (>*<) :: (p :< c, q :< c, Functor f) => Inh f c p -> Inh f c q -> Inh f c (p, q)
Documentation
:: Traversable f | |
=> Syn' f (u, d) u | semantic function of synthesised attributes |
-> Inh' f (u, d) d | semantic function of inherited attributes |
-> (u -> d) | initialisation of inherited attributes |
-> Term f | input term |
-> u |
This function runs an attribute grammar on a term. The result is the (combined) synthesised attribute at the root of the term.
:: (Traversable f, Functor g) | |
=> Syn' f (u, d) u | |
-> Inh' f (u, d) d | semantic function of synthesised attributes |
-> Rewrite f (u, d) g | semantic function of inherited attributes |
-> (u -> d) | initialisation of inherited attributes |
-> Term f | input term |
-> (u, Term g) |
This function runs an attribute grammar with rewrite function on a term. The result is the (combined) synthesised attribute at the root of the term and the rewritten term.
class (Functor t, Foldable t) => Traversable (t :: * -> *) #
Functors representing data structures that can be traversed from left to right.
A definition of traverse
must satisfy the following laws:
- naturality
t .
for every applicative transformationtraverse
f =traverse
(t . f)t
- identity
traverse
Identity = Identity- composition
traverse
(Compose .fmap
g . f) = Compose .fmap
(traverse
g) .traverse
f
A definition of sequenceA
must satisfy the following laws:
- naturality
t .
for every applicative transformationsequenceA
=sequenceA
.fmap
tt
- identity
sequenceA
.fmap
Identity = Identity- composition
sequenceA
.fmap
Compose = Compose .fmap
sequenceA
.sequenceA
where an applicative transformation is a function
t :: (Applicative f, Applicative g) => f a -> g a
preserving the Applicative
operations, i.e.
and the identity functor Identity
and composition of functors Compose
are defined as
newtype Identity a = Identity a instance Functor Identity where fmap f (Identity x) = Identity (f x) instance Applicative Identity where pure x = Identity x Identity f <*> Identity x = Identity (f x) newtype Compose f g a = Compose (f (g a)) instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose x) = Compose (fmap (fmap f) x) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure x = Compose (pure (pure x)) Compose f <*> Compose x = Compose ((<*>) <$> f <*> x)
(The naturality law is implied by parametricity.)
Instances are similar to Functor
, e.g. given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
a suitable instance would be
instance Traversable Tree where traverse f Empty = pure Empty traverse f (Leaf x) = Leaf <$> f x traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r
This is suitable even for abstract types, as the laws for <*>
imply a form of associativity.
The superclass instances should satisfy the following:
- In the
Functor
instance,fmap
should be equivalent to traversal with the identity applicative functor (fmapDefault
). - In the
Foldable
instance,foldMap
should be equivalent to traversal with a constant applicative functor (foldMapDefault
).
Instances
This function projects the component of type e
out or the
compound value of type p
.
type (:<) f g = Proj (ComprEmb (Elem f g)) f g infixl 5 #
The constraint e :< p
expresses that e
is a component of the
type p
. That is, p
is formed by binary products using the type
e
. The occurrence of e
must be unique. For example we have Int
:< (Bool,(Int,Bool))
but not Bool :< (Bool,(Int,Bool))
.
lookupNumMap' :: Int -> NumMap t a -> Maybe a #
lookupNumMap :: a -> Int -> NumMap t a -> a #
prodMap :: Mapping m k => v1 -> v2 -> m v1 -> m v2 -> m (v1, v2) #
This function constructs the pointwise product of two maps each with a default value.
number :: Traversable f => f a -> f (Numbered a) #
This function numbers the components of the given functorial value with consecutive integers starting at 0.
unNumbered :: Numbered a -> a #
This type is used for numbering components of a functorial value.
class Functor m => Mapping (m :: * -> *) k | m -> k where #
(&) :: m v -> m v -> m v infixr 0 #
left-biased union of two mappings.
(|->) :: k -> v -> m v infix 1 #
This operator constructs a singleton mapping.
This is the empty mapping.
prodMapWith :: (v1 -> v2 -> v) -> v1 -> v2 -> m v1 -> m v2 -> m v #
This function constructs the pointwise product of two maps each with a default value.
findWithDefault :: a -> k -> m a -> a #
Returns the value at the given key or returns the given default when the key is not an element of the map.
Instances
Functor (NumMap k) | |
Foldable (NumMap k) | |
Defined in Data.Comp.Mapping fold :: Monoid m => NumMap k m -> m # foldMap :: Monoid m => (a -> m) -> NumMap k a -> m # foldr :: (a -> b -> b) -> b -> NumMap k a -> b # foldr' :: (a -> b -> b) -> b -> NumMap k a -> b # foldl :: (b -> a -> b) -> b -> NumMap k a -> b # foldl' :: (b -> a -> b) -> b -> NumMap k a -> b # foldr1 :: (a -> a -> a) -> NumMap k a -> a # foldl1 :: (a -> a -> a) -> NumMap k a -> a # elem :: Eq a => a -> NumMap k a -> Bool # maximum :: Ord a => NumMap k a -> a # minimum :: Ord a => NumMap k a -> a # | |
Traversable (NumMap k) | |
Mapping (NumMap k) (Numbered k) | |
type Inh f p q = q :< p => Inh' f p q Source #
The type of semantic functions for inherited attributes.
type Inh' f p q = forall m i. (Mapping m i, ?below :: i -> p, ?above :: p) => f i -> m q Source #
The type of semantic functions for inherited attributes. For
defining semantic functions use the type Inh
, which includes the
inherited attribute that is defined by the semantic function into
the available attributes.
type Syn f p q = q :< p => Syn' f p q Source #
The type of semantic functions for synthesised attributes.
type Syn' f p q = forall a. (?below :: a -> p, ?above :: p) => f a -> q Source #
The type of semantic functions for synthesised attributes. For
defining semantic functions use the type Syn
, which includes the
synthesised attribute that is defined by the semantic function into
the available attributes.
type Rewrite f q g = forall a. (?below :: a -> q, ?above :: q) => f a -> Context g a Source #
A simple rewrite function that may depend on (inherited and/or synthesised) attributes.
below :: (?below :: child -> q, p :< q) => child -> p Source #
This function provides access to attributes of the immediate children of the current node.
above :: (?above :: q, p :< q) => p Source #
This function provides access to attributes of the current node
prodSyn :: (p :< c, q :< c) => Syn f c p -> Syn f c q -> Syn f c (p, q) Source #
Combines the semantic functions for two synthesised attributes to form a semantic function for the compound attribute consisting of the two original attributes.
(|*|) :: (p :< c, q :< c) => Syn f c p -> Syn f c q -> Syn f c (p, q) Source #
Combines the semantic functions for two synthesised attributes to form a semantic function for the compound attribute consisting of the two original attributes.