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

Safe HaskellSafe
LanguageHaskell2010

FreeC.IR.Similar

Description

This module contains a definition for a custom equality of IR AST nodes. Two AST nodes are called similar if they are equal modulo renaming of bound variables. The source location information is also ignored.

Definition Renaming

A Renaming is a mapping { x₁ ↦ y₁, …, xₙ ↦ yₙ } from variables x₁, …, xₙ in one AST to variables y₁, …, yₙ in another AST. Note that variables and type variables are considered distict even if they have the same name.

Definition: Free Variables in Renaming

A variable x is leftFree in a Renaming Γ if there is no variable y such that x ↦ y ∈ Γ. Analogously a variable y is rightFree in a Renaming Γ if there is no variable x such that x ↦ y ∈ Γ. We write z ∈ free(Γ) if z is both leftFree and rightFree.

Notation

If two nodes n and m are similar under a Renaming Γ we write Γ ⊢ n ≈ m. Whether two nodes are equal or not can be determined from the inference system described in the comments for the Similar instances below.

Synopsis

Documentation

class Similar node Source #

A type class for AST nodes that can be tested for similarity.

Minimal complete definition

similar'

Instances
Similar Type Source #

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

  • Type constructors are only similar to themselves.

    ———————————
     Γ ⊢ T ≈ T
  • A free type variable α is similar to itself.

     α ∈ free(Γ)
    —————————————
     Γ ⊢ α ≈ α
  • Two type variables α and β are similar under a renaming Γ if they are mapped to each other by Γ.

     α ↦ β ∈ Γ
    ———————————
     Γ ⊢ α ≈ β
  • Two type applications are similar if the type constructor and type argument are similar.

     Γ ⊢ τ₁ ≈ τ'₁ , Γ ⊢ τ₂ ≈ τ'₂
    —————————————————————————————
         Γ ⊢ τ₁ τ₂ ≈ τ'₁ τ'₂
  • Two function types are similar if their argument and return types are similar.

     Γ ⊢ τ₁ ≈ τ'₁ , Γ ⊢ τ₂ ≈ τ'₂
    —————————————————————————————
      Γ ⊢ τ₁ -> τ₂ ≈ τ'₁ -> τ'₂
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: Type -> Type -> Renaming -> Bool

Similar TypeScheme Source #

Two type schemes are similar if their abstracted types are similar under an extend Renaming that maps the corresponding type arguments to each other.

    Γ ∪ { α₁ ↦ β₁, …, αₙ ↦ βₙ } ⊢ τ ≈ τ'
————————————————————————————————————————————
 Γ ⊢ forall α₁ … αₙ. τ ≈ forall β₁ … βₙ. τ'
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: TypeScheme -> TypeScheme -> Renaming -> Bool

Similar ConDecl Source #

Two constructor declarations are similar if their field types are similar.

 Γ ⊢ τ₁ ≈ τ'₁, …, Γ ⊢ τₙ ≈ τ'ₙ
———————————————————————————————
  Γ ⊢ C τ₁ … τₙ ≈ C τ'₁ … τ'ₙ
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: ConDecl -> ConDecl -> Renaming -> Bool

Similar TypeDecl Source #

Two type synonym declarations are similar if their right-hand sides are similar under an extended Renaming that maps the corresponding type arguments to each other.

     Γ ∪ { α₁ ↦ β₁, …, αₙ ↦ βₙ } ⊢ τ ≈ τ'
——————————————————————————————————————————————
 Γ ⊢ type T α₁ … αₙ = τ ≈ type T β₁ … βₙ = τ'

Two data type declarations are similar if their constructors are pairwise similar under an extended Renaming that maps the corresponding type arguments to each other. Note that the order of the constructors in important.

 Γ ∪ { α₁ ↦ β₁, …, αₙ ↦ βₙ } ⊢ con₁ ≈ con'₁, …,
 Γ ∪ { α₁ ↦ β₁, …, αₙ ↦ βₙ } ⊢ conₘ ≈ con'ₘ
————————————————————————————————————————————————————————————————————————
 Γ ⊢ data D α₁ … αₙ = con₁ | … | conₘ
   ≈ data D β₁ … βₙ = con'₁ | … | con'ₘ
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: TypeDecl -> TypeDecl -> Renaming -> Bool

Similar Pragma Source #

Two pragmas are similar if they are equal except for the source span.

Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: Pragma -> Pragma -> Renaming -> Bool

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

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

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

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

Similar FuncDecl Source #

Two function declarations are similar if their right-hand sides are similar under an extended Renaming that maps the corresponding type arguments and arguments to each other. If the arguments are annotated, their type annotations must be similar under an extended Renaming that maps the corresponding type arguments to each other.

 Γ ∪ { α₁ ↦ β₁, …, αₘ ↦ βₘ, x₁ ↦ y₁, …, xₙ ↦ yₙ } ⊢ e ≈ f
 Γ ∪ { α₁ ↦ β₁, …, αₘ ↦ βₘ } ⊢ p₁ ≈ q₁, …,
 Γ ∪ { α₁ ↦ β₁, …, αₘ ↦ βₘ } ⊢ pₙ ≈ qₙ
——————————————————————————————————————————————————————————
 Γ ⊢ g @α₁ … @αₘ p₁ … pₙ = e ≈ g @β₁ … @βₘ q₁ … qₙ = f

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

If the function has an explicit return type annotation, the annotated return typed must be similar as well.

 Γ ∪ { α₁ ↦ β₁, …, αₘ ↦ βₘ, x₁ ↦ y₁, …, xₙ ↦ yₙ } ⊢ e ≈ f
 Γ ∪ { α₁ ↦ β₁, …, αₘ ↦ βₘ } ⊢ p₁ ≈ q₁, …,
 Γ ∪ { α₁ ↦ β₁, …, αₘ ↦ βₘ } ⊢ pₙ ≈ qₙ,
 Γ ∪ { α₁ ↦ β₁, …, αₘ ↦ βₘ } ⊢ τ ≈ τ',
——————————————————————————————————————————————————————————
 Γ ⊢ g @α₁ … @αₘ p₁ … pₙ :: τ = e ≈ g @β₁ … @βₘ q₁ … qₙ :: τ' = f
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: FuncDecl -> FuncDecl -> Renaming -> Bool

Similar TypeSig Source #

Two type signatures are similar if they annotate the type of the same functions with similar types.

 Γ ⊢ τ ≈ τ'
————————————————————————————————————————
 Γ ⊢ f₁, … , fₙ :: τ ≈ f₁, … , fₙ :: τ'
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: TypeSig -> TypeSig -> Renaming -> Bool

Similar TopLevelDecl Source #

Only top-level declarations of the same type can be similar.

Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: TopLevelDecl -> TopLevelDecl -> Renaming -> Bool

Similar ImportDecl Source #

Two import declarations are similar if they import the same module.

Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: ImportDecl -> ImportDecl -> Renaming -> Bool

Similar node => Similar [node] Source #

Two lists of nodes are similar if the corresponding elements are similar and each element has a corresponding element (i.e., the lists are of the same length).

  Γ ⊢ x₁ ≈ y₁, …, Γ ⊢ xₙ ≈ yₙ
———————————————————————————————
 Γ ⊢ [x₁, …, xₙ] ≈ [y₁, …, yₙ]
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: [node] -> [node] -> Renaming -> Bool

Similar node => Similar (Maybe node) Source #

Two optional nodes are similar when they are either both not present or both are present and similar.

                               Γ ⊢ x ≈ y
———————————————————————  —————————————————————
 Γ ⊢ Nothing ≈ Nothing    Γ ⊢ Just x ≈ Just y
Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: Maybe node -> Maybe node -> Renaming -> Bool

Similar contents => Similar (ModuleOf contents) Source #

Two modules are similar if their names and contents are similar.

Instance details

Defined in FreeC.IR.Similar

Methods

similar' :: ModuleOf contents -> ModuleOf contents -> Renaming -> Bool

similar :: Similar node => node -> node -> Bool Source #

Tests whether two AST nodes are similar, i.e., if they are equal except for renaming of bound variables.

notSimilar :: Similar node => node -> node -> Bool Source #

The negation of similar.