Abstract definitions

Definitions can be marked as abstract, for the purpose of hiding implementation details, or to speed up type-checking of other parts. In essence, abstract definitions behave like postulates, thus, do not reduce/compute. For instance, proofs whose content does not matter could be marked abstract, to prevent Agda from unfolding them (which might slow down type-checking).

As a guiding principle, all the rules concerning abstract are designed to prevent the leaking of implementation details of abstract definitions. Similar concepts of other programming language include (non-representative sample): UCSD Pascal’s and Java’s interfaces and ML’s signatures. (Especially when abstract definitions are used in combination with modules.)

Synopsis

  • Declarations can be marked as abstract using the block keyword abstract.

  • Outside of abstract blocks, abstract definitions do not reduce, they are treated as postulates, in particular:

    • Abstract functions never match, thus, do not reduce.

    • Abstract data types do not expose their constructors.

    • Abstract record types do not expose their fields nor constructor.

    • Other declarations cannot be abstract.

  • Inside abstract blocks, abstract definitions reduce while type checking definitions, but not while checking their type signatures. Otherwise, due to dependent types, one could leak implementation details (e.g. expose reduction behavior by using propositional equality).

    Consequently information from checking the body of a definition cannot leak into its type signature, effectively disabling type inference for abstract definitions. This means that all abstract definitions need a complete type signature.

  • The reach of the abstract keyword block extends recursively to the where-blocks of a function and the declarations inside of a record declaration, but not inside modules declared in an abstract block.

Examples

Integers can be implemented in various ways, e.g. as difference of two natural numbers:

  1. module Integer where
  2. abstract
  3. : Set
  4. = Nat × Nat
  5. 0 :
  6. 0 = 0 , 0
  7. 1 :
  8. 1 = 1 , 0
  9. _+ℤ_ : (x y : ℤ)
  10. (p , n) +ℤ (p' , n') = (p + p') , (n + n')
  11. -ℤ_ :
  12. -ℤ (p , n) = (n , p)
  13. _≡ℤ_ : (x y : ℤ) Set
  14. (p , n) ≡ℤ (p' , n') = (p + n') ≡ (p' + n)
  15. private
  16. postulate
  17. +comm : n m (n + m) (m + n)
  18. inv : x (x +ℤ (-ℤ x)) ≡ℤ 0
  19. inv (p , n) rewrite +comm (p + n) 0 | +comm p n = refl

Using abstract we do not give away the actual representation of integers, nor the implementation of the operations. We can construct them from 0ℤ, 1ℤ, _+ℤ_, and -ℤ, but only reason about equality ≡ℤ with the provided lemma invℤ.

The following property shape-of-0ℤ of the integer zero exposes the representation of integers as pairs. As such, it is rejected by Agda: when checking its type signature, proj₁ x fails to type check since x is of abstract type . Remember that the abstract definition of does not unfold in type signatures, even when in an abstract block! To work around this we have to define aliases for the projections functions:

  1. -- A property about the representation of zero integers:
  2. abstract
  3. private
  4. posZ : Nat
  5. posZ = proj
  6. negZ : Nat
  7. negZ = proj
  8. shape-of-0 : (x : ℤ) (is0 : x ≡ℤ 0ℤ) posZ x negZ x
  9. shape-of-0 (p , n) refl rewrite +comm p 0 = refl

By requiring shape-of-0ℤ to be private to type-check, leaking of representation details is prevented.

Scope of abstraction

In child modules, when checking an abstract definition, the abstract definitions of the parent module are transparent:

  1. module M1 where
  2. abstract
  3. x : Nat
  4. x = 0
  5. module M2 where
  6. abstract
  7. x-is-0 : x 0
  8. x-is-0 = refl

Thus, child modules can see into the representation choices of their parent modules. However, parent modules cannot see like this into child modules, nor can sibling modules see through each others abstract definitions. An exception to this is anonymous modules, which share abstract scope with their parent module, allowing parent or sibling modules to see inside their abstract definitions.

The reach of the abstract keyword does not extend into modules:

  1. module Parent where
  2. abstract
  3. module Child where
  4. y : Nat
  5. y = 0
  6. x : Nat
  7. x = 0 -- to avoid "useless abstract" error
  8. y-is-0 : Child.y 0
  9. y-is-0 = refl

The declarations in module Child are not abstract!

Abstract definitions with where-blocks

Definitions in a where block of an abstract definition are abstract as well. This means, they can see through the abstractions of their uncles:

  1. module Where where
  2. abstract
  3. x : Nat
  4. x = 0
  5. y : Nat
  6. y = x
  7. where
  8. xy : x 0
  9. xy = refl