free-compiler-0.3.0.0: A Haskell to Coq compiler.

Safe HaskellSafe
LanguageHaskell2010

FreeC.IR.Subterm

Contents

Description

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

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

newtype Pos Source #

Describes a position of a subterm within a Haskell expression.

Constructors

Pos [Int] 
Instances
Eq Pos Source # 
Instance details

Defined in FreeC.IR.Subterm

Methods

(==) :: Pos -> Pos -> Bool Source #

(/=) :: Pos -> Pos -> Bool Source #

Show Pos Source # 
Instance details

Defined in FreeC.IR.Subterm

Pretty Pos Source #

Pretty prints a position.

Instance details

Defined in FreeC.IR.Subterm

Methods

pretty :: Pos -> Doc #

prettyList :: [Pos] -> Doc #

rootPos :: Pos Source #

Position of the root 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.

above :: Pos -> Pos -> Bool Source #

Tests whether a position is above another one.

below :: Pos -> Pos -> Bool Source #

Tests whether a position is below another one.

leftOf :: Pos -> Pos -> Bool Source #

Tests whether a position is left of another one.

rightOf :: Pos -> Pos -> Bool Source #

Tests whether a position is right of another one.

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.

replaceSubterm Source #

Arguments

:: 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+1th 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.