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

Safe HaskellSafe
LanguageHaskell2010

FreeC.IR.Syntax.Expr

Description

This module contains the definition of expressions for our intermediate language.

Synopsis

Documentation

data Expr Source #

An expression.

Constructors

Con

A constructor.

Var

A function or local variable.

App

Function or constructor application.

TypeAppExpr

Visible type application.

If

if expression.

Case

case expression.

Undefined

Error term undefined.

ErrorExpr

Error term error "message".

Trace

Effect trace "message" expr.

IntLiteral

An integer literal.

Lambda

A lambda abstraction.

Let

A let expression.

Instances
Eq Expr Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

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

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

Show Expr Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Pretty Expr Source #

Pretty instance for expressions.

If the expression contains type annotations, the output quickly becomes practically unreadable. Consider stripping type annotations before pretty printing (see FreeC.IR.Strip) to improve readability.

Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

pretty :: Expr -> Doc #

prettyList :: [Expr] -> Doc #

StripExprType Expr Source #

Strips the type annotation of the given expression and of its sub-expressions recursively.

Instance details

Defined in FreeC.IR.Strip

Similar Expr Source #

The similarity relation of expressions is governed by the the following inference rules

  • Expressions with type annotations are similar if the annotated expressions are similar and their annotations are similar.

     Γ ⊢ τ ≈ τ', Γ ⊢ e ≈ f
    ———————————————————————
     Γ ⊢ e :: τ ≈ f :: τ'
  • For all variables x and y, x and y are similar under a renaming Γ if they are mapped to each other by Γ.

     x ↦ y ∈ Γ
    ———————————
     Γ ⊢ x ≈ y
  • A free variable x is similar to itself.

     x ∈ free(Γ)
    —————————————
     Γ ⊢ x ≈ x
  • Two lambda abstractions are similar if their right-hand sides are similar under an extended Renaming that maps the variable patterns from the first lambda abstraction to the arguments of the second one. Furthermore, the types the lambda abstraction's arguments have been annotated with must be similar (if there are any).

     Γ ∪ { x₁ ↦ y₁, …, xₙ ↦ yₙ } ⊢ e ≈ f, Γ ⊢ p₁ ≈ q₁, …, Γ ⊢ pₙ ≈ qₙ
    ——————————————————————————————————————————————————————————————————
                    Γ ⊢ \p₁ … pₙ -> e ≈ \q₁ … qₙ -> f

    where xᵢ and yᵢ denote the names of the variables bound by the patterns pᵢ and qᵢ respectively.

  • Two application expressions are similar if the callees and the arguments are similar.

     Γ ⊢ e₁ ≈ f₁, Γ ⊢ e₂ ≈ f₂
    ——————————————————————————
        Γ ⊢ e₁ e₂ ≈ f₁ f₂
  • Two visible type application expressions are similar if the visibly applied expressions and the type arguments are similar.

     Γ ⊢ e ≈ f, Γ ⊢ τ ≈ τ'
    ———————————————————————
        Γ ⊢ e @τ ≈ f @τ'
  • Two case expressions are similar if the expressions they match are similar and their alternatives are pairwise similar. Note that the order of the alternatives is important.

     Γ ⊢ e ≈ f,      Γ ⊢ alt₁ ≈ alt'₁,      …,      Γ ⊢ altₙ ≈ alt'ₙ
    —————————————————————————————————————————————————————————————————
     Γ ⊢ case e of { alt₁; …; altₙ } ≈ case f of { alt'₁; …; alt'ₙ }
  • Two if expressions are similar if their conditions and branches are similar.

     Γ ⊢ e₁ ≈ f₁,       Γ ⊢ e₂ ≈ f₂,       Γ ⊢ e₃ ≈ f₃
    ———————————————————————————————————————————————————
     Γ ⊢ if e₁ then e₂ else e₃ ≈ if f₁ then f₂ else f₃
  • Two let expressions are similar if their bindings and the expression are similar under an extended Renaming Γ' that maps the variables bound by the first let expression to the variables bound by the second let expression.

     Γ' ⊢ e ≈ f,    Γ' ⊢ b₁ ≈ d₁,    …,    Γ' ⊢ bₙ ≈ dₙ
    ——————————————————————————————————————————————————————
     Γ ⊢ let { b₁; …; bₙ } in e ≈ let { d₁; …; dₙ } in f

    where Γ' = Γ ∪ { x₁ ↦ y₁, …, xₙ ↦ yₙ } and x₁, …, xₙ and y₁, …, yₙ denote the names of variables bound by b₁, …, bₙ and d₁, …, dₙ, respectively.

  • All AST nodes which do not contain any variables are similar to themselves under every Renaming Γ.

    ———————————  ———————————————————————————  ———————————————————————
     Γ ⊢ C ≈ C    Γ ⊢ undefined ≈ undefined    Γ ⊢ error s ≈ error s

    Where C is a constructor or integer literal and s is an arbitrary error message.

Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: Expr -> Expr -> Renaming -> Bool

HasRefs Expr Source #

Expression refer to the used variables and constructors as wells as the types used in type signatures and visible type applications.

The error terms undefined and error "msg" refer to the functions undefinedFuncName and errorFuncName respectively. The term trace "msg" expr refers to the function traceFuncName.

Instance details

Defined in FreeC.IR.Reference

Methods

refSet :: Expr -> RefSet

Parseable Expr Source #

Expressions can be parsed.

Instance details

Defined in FreeC.Frontend.IR.Parser

Methods

parseIR' :: Parser Expr Source #

ApplySubst Type Expr Source #

Applies the given type substitution to an expression.

Instance details

Defined in FreeC.IR.Subst

ApplySubst Expr Bind Source #

Applies the given expression substitution to an the right-hand side of a let binding.

The variable that is bound by the binding is not renamed by this instance. The ApplySubst instance for expressions renames all variables that are bound by let expressions.

Instance details

Defined in FreeC.IR.Subst

ApplySubst Expr Alt Source #

Applies the given expression substitution to the right-hand side of the given case-expression alternative.

Instance details

Defined in FreeC.IR.Subst

Methods

applySubst :: Subst Expr -> Alt -> Alt Source #

ApplySubst Expr Expr Source #

Applies the given expression substitution to an expression.

This function uses the Converter monad, because we need to create fresh identifiers. This is because we have to rename arguments of lambda abstractions and case-alternatives, such that no name conflict can occur.

Instance details

Defined in FreeC.IR.Subst

ApplySubst Expr FuncDecl Source #

Applies the given expression substitution to the right-hand side of a function declaration.

Instance details

Defined in FreeC.IR.Subst

exprType :: Expr -> Maybe Type Source #

Gets the type annotation of the given expression, but discards the forall.

Type annotations quantify their type variables usually only if they are used as expression type signatures. The type annotations generated during type inference never quantify their type arguments.

untypedCon :: SrcSpan -> ConName -> Expr Source #

Smart constructor for Con without the last argument.

untypedVar :: SrcSpan -> ConName -> Expr Source #

Smart constructor for Var without the last argument.

untypedApp :: SrcSpan -> Expr -> Expr -> Expr Source #

Smart constructor for app without the last argument.

The type annotation is inferred from the callee's type annotation. If it is annotated with a function type, the created expression is annotated with the function type's result type.

untypedTypeAppExpr :: SrcSpan -> Expr -> Type -> Expr Source #

Smart constructor for TypeAppExpr without the last argument.

The type annotation of the expression which is visibly applied is used to annotate the type of this expression.

app :: SrcSpan -> Expr -> [Expr] -> Expr Source #

Creates an expression for applying the given expression to the provided arguments.

The given source span is inserted into the generated function application expressions.

If the given expression's type is annotated with a function type, all generated application nodes are annotated with the corresponding result types. If no more argument types can be split off, the types of the remaining arguments are not annotated.

varApp Source #

Arguments

:: SrcSpan

The source span to insert into generated nodes.

-> VarName

The name of the function to apply.

-> [Expr]

The arguments to pass to the function.

-> Expr 

Creates an expression for applying the function with the given name.

The given source span is inserted into the generated function reference and every generated function application.

Since the type of the variable with the given name is not known, no type annotations will be generated.

conApp Source #

Arguments

:: SrcSpan

The source span to insert into generated nodes.

-> ConName

The name of the constructor to apply.

-> [Expr]

The arguments to pass to the constructor.

-> Expr 

Creates a data constructor application expression.

The given source span is inserted into the generated constructor reference and every generated constructor application.

Since the type of the constructor with the given name is not known, no type annotations will be generated.

visibleTypeApp :: SrcSpan -> Expr -> [Type] -> Expr Source #

Creates an expression for passing the type arguments of a function or constructor explicitly.

The given source span is inserted into every generated visible type application node.

If the given expression's type is annotated, all generated visible type application nodes are annotated with the same type.

getFuncName :: Expr -> Maybe VarName Source #

Returns the function name of a function application, or Nothing if the given expression is not a function application.

prettyExprPred :: Int -> Expr -> Doc Source #

Pretty prints an expression and adds parentheses if necessary.

The first argument indicates the precedence of the surrounding context. * 0 - Top level. No parentheses are necessary. * 1 - Parentheses are needed around if and lambda expressions. * 2 - Parentheses are also needed around function applications.

prettyExprPred' :: Int -> Expr -> Doc Source #

Like prettyExprPred but ignores outermost type annotations.

data Alt Source #

One alternative of a case expression.

Constructors

Alt 
Instances
Eq Alt Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

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

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

Show Alt Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Pretty Alt Source #

Pretty instance for case expression alternatives.

Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

pretty :: Alt -> Doc #

prettyList :: [Alt] -> Doc #

Similar Alt Source #

Two alternatives that match the same constructor C are similar if their right-hand sides are similar under an extended Renaming that maps the variable patterns from the first alternative to the corresponding variable patterns from the second alternative. Additionally, the type annotations of their variable patterns must be similar (if there are any).

 Γ ∪ { x₁ ↦ y₁, …, xₙ ↦ yₙ } ⊢ e ≈ f, Γ ⊢ p₁ ≈ q₁, …, Γ ⊢ pₙ ≈ qₙ
——————————————————————————————————————————————————————————————————
            Γ ⊢ C p₁ … pₙ -> e ≈ C q₁ … qₙ -> f

where xᵢ and yᵢ denote the names of the variables bound by the patterns pᵢ and qᵢ respectively.

Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: Alt -> Alt -> Renaming -> Bool

HasRefs Alt Source #

case expression alternatives refer to the matched constructor, the types the type annotations of the variable patterns refer to and the references of the right-hand side that are not bound by the variable patterns.

Instance details

Defined in FreeC.IR.Reference

Methods

refSet :: Alt -> RefSet

ApplySubst Type Alt Source #

Applies the given type substitution to the right-hand side of the given case-expression alternative.

Instance details

Defined in FreeC.IR.Subst

Methods

applySubst :: Subst Type -> Alt -> Alt Source #

ApplySubst Expr Alt Source #

Applies the given expression substitution to the right-hand side of the given case-expression alternative.

Instance details

Defined in FreeC.IR.Subst

Methods

applySubst :: Subst Expr -> Alt -> Alt Source #

data ConPat Source #

A constructor pattern used in an alternative of a case expression.

The main purpose of this data type is to add location information to a ConName.

Constructors

ConPat 
Instances
Eq ConPat Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Show ConPat Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Pretty ConPat Source #

Pretty instance for constructor patterns.

Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

pretty :: ConPat -> Doc #

prettyList :: [ConPat] -> Doc #

HasRefs ConPat Source #

Constructor patterns refer to the matched constructor.

Instance details

Defined in FreeC.IR.Reference

Methods

refSet :: ConPat -> RefSet

conPatToExpr :: ConPat -> Expr Source #

Converts a constructor pattern to a constructor expression.

data VarPat Source #

A variable pattern used as an argument to a function, lambda abstraction or constructor pattern.

The variable pattern can optionally have a type signature or be annotated by a !.

Instances
Eq VarPat Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Show VarPat Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Pretty VarPat Source #

Pretty instance for variable patterns.

Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

pretty :: VarPat -> Doc #

prettyList :: [VarPat] -> Doc #

HasDeclIdent VarPat Source #

Instance to get the name of a let-binding.

Instance details

Defined in FreeC.IR.Syntax.Expr

Similar VarPat Source #

Two variable patterns are similar if they either both don't have a type annotation or are annotated with similar types.

                     Γ ⊢ τ ≈ τ'
———————————  ——————————————————————————
 Γ ⊢ x ≈ y    Γ ⊢ (x :: τ) ≈ (y :: τ')

If one of the patterns has a bang pattern, the other one needs one as well.

                       Γ ⊢ τ ≈ τ'
—————————————  ————————————————————————————
 Γ ⊢ !x ≈ !y    Γ ⊢ !(x :: τ) ≈ !(y :: τ')
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: VarPat -> VarPat -> Renaming -> Bool

HasRefs VarPat Source #

Variable patterns refer to the types used in their type annotation.

Instance details

Defined in FreeC.IR.Reference

Methods

refSet :: VarPat -> RefSet

ApplySubst Type VarPat Source #

Applies the given type substitution to the type annotation of the given variable pattern.

Instance details

Defined in FreeC.IR.Subst

varPatName :: VarPat -> Name Source #

Gets the name of the given variable pattern.

varPatQName :: VarPat -> QName Source #

Gets the qualified name of the given variable pattern.

varPatToExpr :: VarPat -> Expr Source #

Converts a variable pattern to a variable expression.

toVarPat :: String -> VarPat Source #

Converts the given identifier to a variable pattern without type annotation.

data Bind Source #

A binding of a variable to an expression inside of a let-expression.

Constructors

Bind 
Instances
Eq Bind Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

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

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

Show Bind Source # 
Instance details

Defined in FreeC.IR.Syntax.Expr

Pretty Bind Source #

Pretty instance for let expression binds.

Instance details

Defined in FreeC.IR.Syntax.Expr

Methods

pretty :: Bind -> Doc #

prettyList :: [Bind] -> Doc #

HasDeclIdent Bind Source #

Instance to get the name of a let-binding.

Instance details

Defined in FreeC.IR.Syntax.Expr

Similar Bind Source #

Two let bindings are similar if their variable pattern and expression are are similar.

 Γ' ⊢ p ≈ q  , Γ' ⊢ e ≈ f
——————————————————————————
    Γ ⊢ p = e ≈ q = f
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: Bind -> Bind -> Renaming -> Bool

HasRefs Bind Source # 
Instance details

Defined in FreeC.IR.Reference

Methods

refSet :: Bind -> RefSet

ApplySubst Type Bind Source #

Applies the given type substitution to the variable pattern and expression of the given let binding.

Instance details

Defined in FreeC.IR.Subst

ApplySubst Expr Bind Source #

Applies the given expression substitution to an the right-hand side of a let binding.

The variable that is bound by the binding is not renamed by this instance. The ApplySubst instance for expressions renames all variables that are bound by let expressions.

Instance details

Defined in FreeC.IR.Subst