Library intro

An overview of Rocq's type theory

Indexed inductive type
Inductive vector A : nat Type :=
| nil : vector A 0
| cons : (h:A) (n:nat), vector A n vector A (S n).

Mutual inductive type
Inductive even : nat Prop :=
| even_O : even 0
| even_S (n : nat) : odd n even (S n)
with odd : nat Prop :=
| odd_S (n : nat) : even n odd (S n).

Higher-order inductive type
Inductive tree (A : Type) : Type :=
| leaf : tree A
| node (children : nat tree A) : tree A.

Nested inductive type
Inductive rose (A : Type) : Type :=
| rleaf : rose A
| rnode (children : list (rose A)) : rose A.

Recursive definitions with nested pattern-matching
Fixpoint is_even (n : nat) : bool :=
  match n with
  | 0 ⇒ true
  | 1 ⇒ false
  | S (S n') ⇒ is_even n'
  end.

Primitive pattern matching encoding
Check fix is_even (n : nat) : bool :=
match n return bool with
| Otrue
| S n0match n0 return bool with
  | Ofalse
  | S n'is_even n'
  end
end.

Dependent pattern-matching
Fixpoint even_is_even (n : nat) : (is_even n = true) even n :=
  match n as x return is_even x = true even x with
  | 0 ⇒ fun _even_O
  | 1 ⇒ fun (e : false = true) ⇒
    (match e in _ = x return (if x then even 1 else True) with
    | eq_reflI end)
  | S (S n') ⇒ fun eeven_S _ (odd_S _ (even_is_even n' e))
  end.

Records
Set Primitive Projections.
Record sig (A : Type) (B : A Type) : Type :=
 { fst : A ; snd : B fst }. Definition pair : sig nat (fun _bool) :=
  {| fst := 0 ; snd := true |}. Arguments fst {A B}.
Arguments snd {A B}.
Definition fst_fun {A : Type} {B : A Type} (p : sig A B) : A := p.(fst).
Positive coinductive types
Module PosCoind.
CoInductive stream (A : Type) : Type :=
| scons (head : A) (tail : stream A) : stream A.

CoFixpoint repeat (A : Type) (a : A) : stream A := scons _ a (repeat A a).
End PosCoind.

Negative coinductive types
CoInductive stream (A : Type) : Type := { head : A ; tail : stream A } .

CoFixpoint repeat (A : Type) (a : A) : stream A :=
  {| head := a ; tail := repeat A a |}.

Elimination restrictions
Definition ex_falso (f : False) : nat := match f with end.
Fail Definition forbidden (o : True True) : nat :=
  match o with
  | or_introl _ ⇒ 0
  | or_intror _ ⇒ 1
  end.

Universe polymorphism
Definition id (A : Type) (a : A) := a.
Fail Check (id _ id).

#[universes(polymorphic)] Definition pid@{i} (A : Type@{i}) (a : A) : A := a.
Check (pid _ pid).

Dependent let-bindings
Definition test : 0 = 0 := let x := 0 in (eq_refl x : 0 = 0).
Fail Definition test' : 0 = 0 := (fun xeq_refl x : 0 = 0) 0.

Eta-conversion for function types
Check (fun f : nat nateq_refl) : f : nat nat, f = (fun xf x).

Eta-conversion for record types
Check (fun p : sig nat (fun _bool) ⇒ eq_refl) :
   p : sig nat (fun _bool), p = {| fst := p.(fst) ; snd := p.(snd) |}.