AspectAG-0.3.5: Attribute Grammars in the form of an EDSL

Safe HaskellSafe-Infered

Language.Grammars.AspectAG

Contents

Description

Library for First-Class Attribute Grammars.

The library is documented in the paper: Attribute Grammars Fly First-Class. How to do aspect oriented programming in Haskell

For more documentation see the AspectAG webpage: http://www.cs.uu.nl/wiki/bin/view/Center/AspectAG.

Synopsis

Rules

type Att att val = LVPair att valSource

Field of an attribution.

data Fam l ho c p Source

A Family Fam contains a single attribution p for the parent, l for local attributes, ho for higer-order attributes and a collection of attributions c for the children.

Constructors

Fam l ho c p 

Instances

(HasField lprd r (Rule lf hof sc ip l' ho' ic' sp' l'' ho'' ic'' sp''), HUpdateAtLabel lprd (Rule lf hof sc ip l ho ic sp l'' ho'' ic'' sp'') r r') => ComSingle HTrue (Prd lprd (Rule lf hof sc ip l ho ic sp l' ho' ic' sp')) r r' 
HExtend (LVPair att val) sp sp' => Apply (FnSyn att) (Fam lf hof sc ip -> val) (Rule lf hof sc ip l ho ic sp l ho ic sp') 
Defs att nts vals ic ic' => Apply (FnInh att nts) (Fam lf hof sc ip -> vals) (Rule lf hof sc ip l ho ic sp l ho ic' sp) 

type Chi ch atts = LVPair ch attsSource

Field of the record of attributions for the children.

type Rule lf hof sc ip l ho ic sp l' ho' ic' sp' = Fam lf hof sc ip -> Fam l ho ic sp -> Fam l' ho' ic' sp'Source

The type Rule states that a rule takes as input the local attributes lf, the higher-order attributes hof, the synthesized attributes of the children sc and the inherited attributes of the parent ip and returns a function from the output constructed thus far (local attributes l, higher-order attributes ho, inherited attributes of the children ic and synthesized attributes of the parent sp) to the extended output.

emptyRule :: Rule lf hof sc ip l ho ic sp l ho ic spSource

An emptyRule does not introduce any new attribute.

instdef :: HExtend (Att att val) ho ho' => att -> val -> Fam l ho ic sp -> Fam l ho' ic spSource

The function instdef adds the definition of a higher-order attribute. It takes a label att representing the name of the new attribute, a value val to be assigned to this attribute, and it builds a function which updates the output constructed thus far.

locdef :: HExtend (Att att val) l l' => att -> val -> Fam l ho ic sp -> Fam l' ho ic spSource

The function locdef adds the definition of a local attribute. It takes a label att representing the name of the new attribute, a value val to be assigned to this attribute, and it builds a function which updates the output constructed thus far.

inhdef :: Defs att nts vals ic ic' => att -> nts -> vals -> Fam l ho ic sp -> Fam l ho ic' spSource

The function inhdef introduces a new inherited attribute for a collection of non-terminals. It takes the following parameters: att: the attribute which is being defined, nts: the non-terminals with which this attribute is being associated, and vals: a record labelled with child names and containing values, describing how to compute the attribute being defined at each of the applicable child positions. It builds a function which updates the output constructed thus far.

syndef :: HExtend (Att att val) sp sp' => att -> val -> Fam l ho ic sp -> Fam l ho ic sp'Source

The function syndef adds the definition of a synthesized attribute. It takes a label att representing the name of the new attribute, a value val to be assigned to this attribute, and it builds a function which updates the output constructed thus far.

inhmod :: Mods att nts vals ic ic' => att -> nts -> vals -> Fam l ho ic sp -> Fam l ho ic' spSource

The function inhmod modifies an inherited attribute for a collection of non-terminals. It takes the following parameters: att: the attribute which is being defined, nts: the non-terminals with which this attribute is being associated, and vals: a record labelled with child names and containing values, describing how to compute the attribute being defined at each of the applicable child positions. It builds a function which updates the output constructed thus far.||

synmod :: HUpdateAtLabel att val sp sp' => att -> val -> Fam l ho ic sp -> Fam l ho ic sp'Source

The function synmod modifies the definition of a synthesized attribute. It takes a label att representing the name of the attribute, a value val to be assigned to this attribute, and it builds a function which updates the output constructed thus far.

synupd :: (HasField l1 r v, HUpdateAtLabel l1 v1 r p) => l1 -> (v -> v1) -> Fam l ho c r -> Fam l ho c pSource

Monadic

class At l m v | l -> v whereSource

Methods

at :: l -> m vSource

Instances

(HasField (Proxy (lch, nt)) chi v, MonadReader (Fam l ho chi par) m) => At (Proxy (lch, nt)) m v 
MonadReader (Fam l ho chi par) m => At (Proxy Loc) m l 
MonadReader (Fam l ho chi par) m => At (Proxy Lhs) m par 
MonadReader (Fam l ho chi par) m => At (chi -> v) m v 

def :: Reader (Fam l ho chi par) a -> Fam l ho chi par -> aSource

instdefM :: HExtend (Att att a) ho ho' => att -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic sp l ho' ic spSource

locdefM :: HExtend (Att att a) l l' => att -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic sp l' ho ic spSource

inhdefM :: Defs att nts a ic ic' => att -> nts -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic sp l ho ic' spSource

syndefM :: HExtend (Att att a) sp sp' => att -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic sp l ho ic sp'Source

inh :: Defs att nts a ic ic' => att -> nts -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic sp l ho ic' spSource

syn :: HExtend (Att att a) sp sp' => att -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic sp l ho ic sp'Source

inhmodM :: Mods att nts a ic ic' => att -> nts -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic sp l ho ic' spSource

synmodM :: (HUpdateAtHNat n (Att att a) sp sp', HFind att ls n, RecordLabels sp ls) => att -> Reader (Fam lf hof sc ip) a -> Rule lf hof sc ip l ho ic (Record sp) l ho ic (Record sp')Source

synupdM :: (HasField l1 r v, HUpdateAtLabel l1 v1 r p) => l1 -> Reader (Fam l2 ho1 chi par) (v -> v1) -> Fam l2 ho1 chi par -> Fam l ho c r -> Fam l ho c pSource

Rules Composition

ext :: Rule lf hof sc ip l' ho' ic' sp' l'' ho'' ic'' sp'' -> Rule lf hof sc ip l ho ic sp l' ho' ic' sp' -> Rule lf hof sc ip l ho ic sp l'' ho'' ic'' sp''Source

Composition of two rules.

adapt :: Rule lf hof sc ip li hoi ici spi lo hoo ico spo -> (sc' -> sc) -> (ici' -> ici) -> (ico -> ico') -> Rule lf hof sc' ip li hoi ici' spi lo hoo ico' spoSource

Adaption of the childen of a rule.

rename :: (RenRL s sc' sc, RenRL s ici' ici, RenLR s ico ico') => Rule lf hof sc ip li hoi ici spi lo hoo ico spo -> s -> Rule lf hof sc' ip li hoi ici' spi lo hoo ico' spoSource

Children renaming.

mapChildren :: (MapRL s sc' sc, MapRL s ici' ici, MapLR s ico ico') => Rule lf hof sc ip li hoi ici spi lo hoo ico spo -> s -> Rule lf hof sc' ip li hoi ici' spi lo hoo ico' spoSource

Children mapping.

fixCst :: (RecordLabels r ls, HRLabelSet (HCons (LVPair l (Record HNil)) r), HExtend (LVPair l v) t2 l', HRearrange ls r1 r', HLabelSet ls, H2ProjectByLabels (HCons l HNil) t10 t11 r1) => (Fam t t1 l' t3 -> Fam t4 t5 (Record (HCons (LVPair l (Record HNil)) r)) t6 -> Fam t7 t8 (Record t10) t9) -> l -> v -> Fam t t1 t2 t3 -> Fam t4 t5 (Record r) t6 -> Fam t7 t8 (Record r') t9Source

Fixing a constant as a child.

graft :: (HasField' b e (HCons (LVPair e v2) a3) v, HasField' b e (HCons (LVPair e (Record HNil)) a2) v1, HasField e t2 ip1, RecordLabels t ls2, HEq e e b, HRLabelSet a1, HRLabelSet (HCons (LVPair e (Record HNil)) a2), HRLabelSet a3, HRLabelSet (HCons (LVPair e e) r1), HRLabelSet (HCons (LVPair e v2) a3), HRLabelSet (HCons (LVPair e v) r'), HRLabelSet (HCons (LVPair e v1) r'1), HRLabelSet a2, HRLabelSet a, MapLR r ico1 r3, MapLR (HCons (LVPair e e) r1) ico (Record t2), MapRL r1 (Record (HCons (LVPair e (Record HNil)) a2)) (Record r'1), MapRL r (Record a1) sc, MapRL r (Record a) ici, MapRL r1 (Record (HCons (LVPair e v2) a3)) (Record r'), H2ProjectByLabels ls t1 a1 b2, H2ProjectByLabels ls1 t1 a3 b4, H2ProjectByLabels (HCons e HNil) t2 t3 t4, H2ProjectByLabels ls1 t a2 b3, H2ProjectByLabels ls t a b1, RecordValues r1 ls1, RecordValues r ls, HLeftUnion r3 (Record t4) (Record r2), HRearrange ls2 r2 r'2, HLabelSet ls2) => Rule lf hof (Record (HCons (LVPair e v) r')) ip li hoi (Record (HCons (LVPair e v1) r'1)) spi li1 hoi1 ico p -> Record r1 -> e -> Rule lf hof sc ip1 li1 hoi1 ici (Record HNil) l ho ico1 v2 -> Record r -> Fam lf hof (Record t1) ip -> Fam li hoi (Record t) spi -> Fam l ho (Record r'2) pSource

Grafting one tree as a child of the other.

agMacro :: (RecordLabels r ls, HRearrange ls r1 r', HLabelSet ls) => (Fam l1 ho1 c p1 -> Fam l ho c1 p -> Fam t t1 t2 p2, (l1, ho1, c2, Record r) -> (t, t1, t2, Record HNil, Record HNil) -> (l2, ho2, Record r1, c1, c)) -> Fam l1 ho1 c2 p1 -> Fam l ho (Record r) p -> Fam l2 ho2 (Record r') p2Source

A generalized version of graft that grafts into or maps every children.

(~~>) :: (HExtend (LVPair e (Record HNil)) l1 t9, HExtend (LVPair e v) l t10, H2ProjectByLabels (HCons e HNil) t4 t5 t6) => e -> v -> (t, t1, t2, t3) -> (t7, t8, Record t4, l1, l) -> (t7, t8, Record t6, t9, t10)Source

Child mapping definition, it reads replaced by the constant.

(==>) :: (HExtend (LVPair l3 v) l2 t14, HExtend (LVPair l3 (Record HNil)) l t13, HasField l3 t7 p, H2ProjectByLabels (HCons l3 HNil) t7 t8 t9, HLeftUnion r (Record t9) t12) => l3 -> (Fam t t1 c p -> Fam l1 ho c1 (Record HNil) -> Fam t4 t5 t6 v, (t, t1, t2, t3) -> (t4, t5, t6, Record HNil, Record HNil) -> (t10, t11, r, c1, c)) -> (t, t1, t2, t3) -> (l1, ho, Record t7, l, l2) -> (t10, t11, t12, t13, t14)Source

Child mapping definition, it reads replaced by.

(-->) :: (HasField l2 t2 v2, HasField l3 r1 v1, HasField l3 r v, HRLabelSet (HCons (LVPair l3 v2) t6), H2ProjectByLabels (HCons l2 HNil) t2 t3 t4, H2ProjectByLabels (HCons l3 HNil) t4 t5 t6, HExtend (LVPair l2 v1) l1 t9, HExtend (LVPair l2 v) l t10) => l2 -> l3 -> (t, t1, r, r1) -> (t7, t8, Record t2, l1, l) -> (t7, t8, Record (HCons (LVPair l3 v2) t6), t9, t10)Source

Child mapping definition, it reads binds to.

(<.>) :: ((lf, hof, sc, ici) -> (l2, ho2, ico1', ici1', sc1') -> (l3, ho3, ico1'', ici1'', sc1'')) -> ((lf, hof, sc, ici) -> (l1, ho1, ico1, ici1, sc1) -> (l2, ho2, ico1', ici1', sc1')) -> (lf, hof, sc, ici) -> (l1, ho1, ico1, ici1, sc1) -> (l3, ho3, ico1'', ici1'', sc1'')Source

Composition of children mapping definitions for the function macro.

ignore :: HExtend (LVPair lch (Record HNil)) l t6 => lch -> (t, t1, t2, t3) -> (t4, t5, l, t7, t8) -> (t4, t5, t6, t7, t8)Source

noChild :: (t, t1, t2, t3) -> (t4, t5, t6, t7, t8) -> (t4, t5, t6, t7, t8)Source

withChild :: HasField lch sc v => lch -> (v -> Rule lf hof sc ip l ho ic sp l' ho' ic' sp') -> Rule lf hof sc ip l ho ic sp l' ho' ic' sp'Source

withChildAtt :: (HasField lch sc r, HasField att r v) => lch -> att -> (v -> Rule lf hof sc ip l ho ic sp l' ho' ic' sp') -> Rule lf hof sc ip l ho ic sp l' ho' ic' sp'Source

Aspects

type Prd prd rule = LVPair prd ruleSource

Field of an aspect. It associates a production prd with a rule rule.

(.+.) :: Com r r' r'' => r -> r' -> r''Source

Semantic Functions

sem_Lit :: a -> Record HNil -> aSource

Semantic function of a terminal

knit :: (HLeftUnion fc ho fc', Kn fc' ic sc, Empties fc' ec) => Rule l ho sc ip (Record HNil) (Record HNil) ec (Record HNil) l ho ic sp -> fc -> ip -> spSource

The function knit takes the combined rules for a node and the semantic functions of the children, and builds a function from the inherited attributes of the parent to its synthesized attributes.

class SemType t nt | t -> ntSource

Common Patterns

copy :: (Copy att nts vp ic ic', HasField att ip vp) => att -> nts -> Rule lf hof sc ip l ho ic sp l ho ic' spSource

A copy rule copies an inherited attribute from the parent to all its children. The function copy takes the name of an attribute att and an heterogeneous list of non-terminals nts for which the attribute has to be defined, and generates a copy rule for this.

use :: (Use att nts a sc, HExtend (Att att a) sp sp') => att -> nts -> (a -> a -> a) -> a -> Rule lf hof sc ip l ho ic sp l ho ic sp'Source

A use rule declares a synthesized attribute that collects information from some of the children. The function use takes the following arguments: the attribute to be defined, the list of non-terminals for which the attribute is defined, a monoidal operator which combines the attribute values, and a unit value to be used in those cases where none of the children has such an attribute.

chain :: (Chain att nts val sc l ho ic sp ic' sp', HasField att ip val) => att -> nts -> Rule lf hof sc ip l ho ic sp l ho ic' sp'Source

In the chain rule a value is threaded in a depth-first way through the tree, being updated every now and then. For this we have chained attributes (both inherited and synthesized). If a definition for a synthesized attribute of the parent with this name is missing we look for the right-most child with a synthesized attribute of this name. If we are missing a definition for one of the children, we look for the right-most of its left siblings which can provide such a value, and if we cannot find it there, we look at the inherited attributes of the father.

Defining Aspects

inhAspect :: (AttAspect (FnInh att nts) defs defasp, DefAspect (FnCpy att nts) cpys cpyasp, Com cpyasp defasp inhasp) => att -> nts -> cpys -> defs -> inhaspSource

The function inhAspect defines an inherited attribute aspect. It takes as arguments: the name of the attribute att, the list nts of non-terminals where the attribute is defined, the list cpys of productions where the copy rule has to be applied, and a record defs containing the explicit definitions for some productions.

synAspect :: (AttAspect (FnSyn att) defs defasp, DefAspect (FnUse att nts op unit) uses useasp, Com useasp defasp synasp) => att -> nts -> op -> unit -> uses -> defs -> synaspSource

The function synAspect defines a synthesized attribute aspect.

chnAspect :: (DefAspect (FnChn att nts) chns chnasp, AttAspect (FnInh att nts) inhdefs inhasp, Com chnasp inhasp asp, AttAspect (FnSyn att) syndefs synasp, Com asp synasp asp') => att -> nts -> chns -> inhdefs -> syndefs -> asp'Source

A chained attribute definition introduces both an inherited and a synthesized attribute. In this case the pattern to be applied is the chain rule.

attAspect :: AttAspect rdef defs rules => rdef -> defs -> rulesSource

defAspect :: DefAspect deff prds rules => deff -> prds -> rulesSource

class Defs att nts vals ic ic' | vals ic -> ic' whereSource

The class Defs is defined by induction over the record vals containing the new definitions. The function defs inserts each definition into the attribution of the corresponding child.

Methods

defs :: att -> nts -> vals -> ic -> ic'Source

Instances

(Defs att nts (Record vs) ic ic', HasLabel (Proxy (lch, t)) ic' mch, HMember (Proxy t) nts mnts, SingleDef mch mnts att (Chi (Proxy (lch, t)) vch) ic' ic'') => Defs att nts (Record (HCons (Chi (Proxy (lch, t)) vch) vs)) ic ic'' 
Defs att nts (Record HNil) ic ic 

Others

class ListNT nt tHd tTl whereSource

Methods

ch_hd :: Proxy (tHd, nt)Source

ch_tl :: Proxy (tTl, [nt])Source

module Data.HList