Variance
Every binder in Arend has a variance: it is either invariant or covariant. Binders are invariant by default. Covariant binders are used to work with categories: a function that is covariant in a parameter maps directed paths (morphisms) between its arguments to directed paths between the results (see directed paths). An invariant parameter can only be used invariantly, that is, the function respects only (undirected) paths.
For example, the following function is covariant in x, so it can be applied to the components of a directed path p : x ~> y:
\func pair {C :⁺ \Cat} (x :⁺ C) : \Sigma⁺ C C => (x, x)
\func pair-map {C :⁺ \Cat} {x y :⁺ C} (p :⁺ x ~> y) : pair x ~> pair y
=> dpath (\lam i => pair (p i))Syntax
Covariant binders are marked with the symbol ⁺.
Each of the following tokens also has an ASCII spelling:
| Unicode | ASCII | Meaning |
|---|---|---|
| (x :⁺ A) | (x :+ A) | a covariant parameter |
| A ->⁺ B | A ->+ B | a function type covariant in its argument |
| \Pi (x : A) ->⁺ B | \Pi (x : A) ->+ B | all parameters of the telescope are covariant |
| \lam x =>⁺ e | \lam x =>+ e | all parameters of the lambda are covariant |
| \let x =>⁺ e \in e' | \let x =>+ e \in e' | a covariant let clause |
| \Sigma⁺ (x : A) (y : B) | \Sigma+ (x : A) (y : B) | all components are covariant |
| \case e :⁺ T \with ... | \case e :+ T \with ... | a covariant case argument |
Goals and other messages of the typechecker always print the Unicode forms.
Covariant parameters are allowed in the following places:
- Parameters of functions (\func, \sfunc, \lemma, \axiom, \type, \cons), data types, and constructors. Elsewhere (for example, in a \meta), the typechecker reports “Covariant parameters are not allowed here”.
- Parameters of \Pi-types and lambdas.
- Parameters of fields of records and classes, such as \class C | f (n :⁺ Nat) : Nat. Fields themselves are always invariant.
Components of a \Sigma-type all have the same variance: a component cannot be annotated individually, and you should write \Sigma⁺ instead.
Covariant positions
A covariant variable can be used only in covariant positions. The typechecker keeps track of the covariant variables that are currently available. Invariant variables are always available. The following subterms are checked in an invariant position, in which covariant variables of the context are not available (but covariant variables bound inside the subterm are):
- types of invariant parameters of a definition;
- domains of \Pi-types and types of lambda parameters, even if the parameter is covariant; the only exception is a \Pi-type that is the type of a parameter of a definition: domains of its covariant parameters may refer to covariant variables;
- components of an invariant \Sigma-type and of tuples of this type;
- arguments for invariant parameters of functions, constructors, data types, fields, and variables of \Pi-types;
- implementations of fields in \new expressions, class extensions, and \cowith definitions;
- \let and \have clauses written with => rather than =>⁺;
- \case arguments that are not marked with :⁺ or \elim.
Codomains of \Pi-types, components of \Sigma⁺-types, types of covariant parameters, and result types of definitions are covariant positions. If a covariant variable occurs in an invariant position, the typechecker reports “Covariant variable ‘x’ is used in an invariant position”. For example:
\data D (n :⁺ Nat)
\func ok (n :⁺ Nat) : \Type => D n
\data E (n : Nat)
\func error (n :⁺ Nat) : \Type => E n -- n is passed to an invariant parameter
\func ok2 {C : \Cat} {a : C} (x :⁺ C) (p :⁺ a ~> x) => p
\func error2 {C : \Cat} {a : C} (x :⁺ C) (p : a ~> x) => p -- the type of an invariant parameter refers to xImplicit arguments for invariant parameters cannot be inferred from covariant variables.
Covariant variables cannot be used contravariantly. For example, if C is a covariant parameter, then (f :⁺ (C ->⁺ Nat) ->⁺ D) is not a valid parameter since C occurs in the domain of the domain of a \Pi-type. Similarly, (f :⁺ C -> D ->⁺ C) is not valid since the domain of an invariant parameter refers to C. If an expression would produce a \Pi-type whose domain depends on covariant parameters (for example, a partial application), the typechecker reports “The domain of a \Pi-type depends on covariant parameters”.
Lambdas and comparison
A lambda takes the variance of its parameters from the expected type: a parameter without a type or with a type specified with : becomes covariant if the corresponding parameter of the expected \Pi-type is covariant. A covariant lambda parameter cannot be checked against an invariant parameter (“Expected an invariant parameter”).
Two \Pi- or \Sigma-types are equal only if their parameters have the same variance. In particular, \Pi (x :⁺ Nat) -> Nat and Nat -> Nat are different types. A partial application of a definition is eta-expanded with the variance of the expected type, so if f (X : \Cat) (x :⁺ X) : Nat, then f X can be used both as an element of X ->⁺ Nat and of X -> Nat.
Pattern matching
When a covariant parameter is matched with a constructor pattern, the variables of the pattern are covariant if the corresponding parameters of the constructor are covariant; otherwise, they are invariant. If a parameter is invariant, all variables in its patterns are invariant.
\data D | con (n :⁺ Nat)
\func f (d :⁺ D) : D
| con n => con n
\func g (d :⁺ D) : Nat
| con (suc n) => n -- n is invariant since the parameter of suc is invariant
| con 0 => 0There are several restrictions on pattern matching on covariant parameters:
- A covariant parameter can be matched with a tuple pattern only if its type is a \Sigma⁺-type. Pattern matching on invariant \Sigma-types and records is not allowed for covariant parameters.
- Pattern matching on the directed interval DI is allowed only for invariant parameters.
- Pattern matching is not allowed on a covariant parameter if other parameters depend on it (“Covariant pattern matching on ‘D’ is not allowed since other parameters depend on the matched variable”). Such a parameter can be matched with a \case expression without \elim.
- If a data type lives in \Cat (for example, it is a directed higher inductive type) or has a covariant constructor parameter of a type in \Cat, then an invariant parameter of this type can be matched only if the result type of the function is a proposition.
Data types with covariant parameters
A data type with covariant parameters cannot have conditions or be truncated, and \use \level cannot be applied to it. Pattern matching in parameters of a data type is allowed only for invariant parameters.
Groupoidal level proofs
Functions defined with \lemma, \sfunc, and \axiom can be equipped with a proof that their result type is a groupoid (that is, every directed path in it is a path) over its covariant parameters:
\lemma f (x :⁺ A) : B x \level⁺ p => ...The ASCII spelling of \level⁺ is \level+. If A is the type of the covariant parameters (the \Sigma⁺-type of their types if there are several), then p must have the following type:
\Pi (h : DI ->⁺ A) (tl : B (h dleft)) (tr : B (h dright)) -> \Sigma (t : DPath (\lam i => B (h i)) tl tr) (\Pi (t' : DPath (\lam i => B (h i)) tl tr) -> t = t')In this case, the body of the function is checked as if the covariant parameters were invariant. For example, the following lemma typechecks only because of the \level⁺ annotation:
\data Empty
\func g (e : Empty) : Nat => 0
\lemma f (x :⁺ Empty) : Nat \level⁺ (\lam h tl tr => \case h dleft) => g x\level⁺ is not allowed for fields of classes.