Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
This module contains data types and functions for working with subterms of expressions and type expressions.
There are also functions for finding the names and types of variables that are bound at a given position in an expression.
Synopsis
- childTerms :: Subterm a => a -> [a]
- replaceChildTerms :: Subterm a => a -> [a] -> Maybe a
- replaceChildTerms' :: Subterm a => a -> [a] -> a
- newtype Pos = Pos [Int]
- rootPos :: Pos
- consPos :: Int -> Pos -> Pos
- parentPos :: Pos -> Maybe Pos
- parentPos' :: Pos -> Maybe (Int, Pos)
- ancestorPos :: Pos -> [Pos]
- allPos :: Subterm a => a -> [Pos]
- above :: Pos -> Pos -> Bool
- below :: Pos -> Pos -> Bool
- leftOf :: Pos -> Pos -> Bool
- rightOf :: Pos -> Pos -> Bool
- selectSubterm :: Subterm a => a -> Pos -> Maybe a
- selectSubterm' :: Subterm a => a -> Pos -> a
- replaceSubterm :: Subterm a => a -> Pos -> a -> Maybe a
- replaceSubterm' :: Subterm a => a -> Pos -> a -> a
- replaceSubterms :: Subterm a => a -> [(Pos, a)] -> Maybe a
- replaceSubterms' :: Subterm a => a -> [(Pos, a)] -> a
- findSubtermPos :: Subterm a => (a -> Bool) -> a -> [Pos]
- findSubtermWithPos :: Subterm a => (a -> Pos -> Bool) -> a -> [(a, Pos)]
- findSubterms :: Subterm a => (a -> Bool) -> a -> [a]
- findFirstSubterm :: Subterm a => (a -> Bool) -> a -> Maybe a
- mapSubterms :: Subterm a => (a -> a) -> a -> a
- mapSubtermsM :: (Subterm a, Monad m) => (a -> m a) -> a -> m a
- boundVarsOf :: Expr -> Int -> Set QName
- boundVarsWithTypeOf :: Expr -> Int -> Map QName (Maybe Type)
- boundVarsAt :: Expr -> Pos -> Set QName
- boundVarsWithTypeAt :: Expr -> Pos -> Map QName (Maybe Type)
Direct Children
childTerms :: Subterm a => a -> [a] Source #
Gets the child nodes of the given AST node.
replaceChildTerms :: Subterm a => a -> [a] -> Maybe a Source #
Replaces the child nodes of the given AST node.
replaceChildTerms' :: Subterm a => a -> [a] -> a Source #
Like replaceChildTerms
but throws an error if the wrong number of child
terms is provided.
Positions
Describes a position of a subterm within a Haskell expression.
consPos :: Int -> Pos -> Pos Source #
Extends a position inside of a child node to a position inside of a parent node.
If pos
is the position of a subterm s
of an expression or
type expression tᵢ
, then consPos i pos
is the position of
the subterm s
of a term C t₁ ... tᵢ ... tₙ
.
parentPos :: Pos -> Maybe Pos Source #
Gets the parent position or Nothing
if the given position is the
root position.
parentPos' :: Pos -> Maybe (Int, Pos) Source #
Gets the parent position or Nothing
if the given position is the
root position.
Returns the index of the child term and the position of the parent term.
ancestorPos :: Pos -> [Pos] Source #
Gets the positions of all ancestors of the the given position including the position itself.
allPos :: Subterm a => a -> [Pos] Source #
Gets all valid positions of subterms within the given Haskell expression.
Subterms
selectSubterm :: Subterm a => a -> Pos -> Maybe a Source #
Selects a subterm of the given expression at the specified position.
Returns Nothing
if there is no such subterm.
selectSubterm' :: Subterm a => a -> Pos -> a Source #
Like selectSubterm
but throws an error if there is no such subterm.
:: Subterm a | |
=> a | The (type) expression whose subterm to replace. |
-> Pos | The position of the subterm. |
-> a | The (type) expression to replace the subterm with. |
-> Maybe a |
Replaces a subterm of the given expression or type expression at the specified position with another expression.
Returns Nothing
if there is no such subterm.
replaceSubterm' :: Subterm a => a -> Pos -> a -> a Source #
Like replaceSubterm
but throws an error if there is no such subterm.
replaceSubterms :: Subterm a => a -> [(Pos, a)] -> Maybe a Source #
Replaces all subterms at the given positions with other (type) expressions.
Returns Nothing
if any of the subterms could not be replaced
replaceSubterms' :: Subterm a => a -> [(Pos, a)] -> a Source #
Like replaceSubterms
but throws an error if any of the subterms could
not be replaced.
Searching for Subterms
findSubtermPos :: Subterm a => (a -> Bool) -> a -> [Pos] Source #
Gets a list of positions for subterms of the given expression that satisfy the provided predicate.
findSubtermWithPos :: Subterm a => (a -> Pos -> Bool) -> a -> [(a, Pos)] Source #
Like findSubtermPos
but the predicate gets the position as an additional
argument and also returns the subterm.
findSubterms :: Subterm a => (a -> Bool) -> a -> [a] Source #
Gets a list of subterms of the given expression that satisfy the provided predicate.
findFirstSubterm :: Subterm a => (a -> Bool) -> a -> Maybe a Source #
Gets the first subterm of the given expression that satisfies the provided predicate.
Return Nothing
if there is no such subterm.
Replacing Subterms
mapSubterms :: Subterm a => (a -> a) -> a -> a Source #
Applies the given function to all subterms of the given node.
The subterms of the returned expression are replaced recursively.
mapSubtermsM :: (Subterm a, Monad m) => (a -> m a) -> a -> m a Source #
Bound Variables
boundVarsOf :: Expr -> Int -> Set QName Source #
Gets the names of variables that are bound by the given expression in its subterm at the given index.
For example, a case
-expression
case e { … ; Cᵢ x₁ … xₙ -> eᵢ ; … }
with subterms [e, e₁, …, eₙ]
binds the variables x₁ … xₙ
in it's
i+1
th subterm eᵢ
but no variables are bound in e
(i.e., if i = 0
).
Returns an empty map if the expression does not have such a subterm.
boundVarsWithTypeOf :: Expr -> Int -> Map QName (Maybe Type) Source #
Like boundVarsOf
but also returns the annotated type of the
variable pattern.
Returns an empty map if the expression does not have such a subterm.
boundVarsAt :: Expr -> Pos -> Set QName Source #
Gets the names of variables that are bound by lambda abstractions or
variable patterns in case
-expressions at the given position of an
expression.
Returns the empty set if the position is invalid.
boundVarsWithTypeAt :: Expr -> Pos -> Map QName (Maybe Type) Source #
Like boundVarsAt
but also returns the annotated type of the
variable pattern.
Returns an empty map if the position is invalid.