Inductive natprod: Type: = pair: nat -> nat -> natprod.
pair constructor to the two arguments of the nat typeEval simpl in (pair 1 2). (* ==> pair 1 2: natprod *)
Notation command. For example, we can get rid of the prefix form and define pairs of numbers like this:Notation "(x, y)": = (pair xy).
Definition fst (p: natprod): nat: = match p with | (x, y) => x end. Definition snd (p: natprod): nat: = match p with | (x, y) => y end. Theorem test: forall (ab: nat), (a, b) = (fst (a, b), snd (a, b)). Proof. reflexivity. Qed.
Inductive natlist: Type: = | nil: natlist | cons: nat -> natlist -> natlist. Notation "h :: t": = (cons ht) (at level 60, right associativity). Notation "[]": = nil. Notation "[h, .., t]": = (cons h .. (cons t nil) ..).
Definition list1: = 1 :: (2 :: (3 :: nil)). Definition list2: = [1,2,3].
Inductive list (X: Type): Type: = | nil: list X | cons: X -> list X -> list X.
nil and cons play the role of polymorphic constructors here. Similarly, we can define polymorphic functions.filter function filters the elements of a set by a predicate): Fixpoint evenb (n: nat): bool: =
match n with
| O => true
| SO => false
| S (S n ') => evenb n'
end.
Fixpoint filter {X: Type} (test: X → bool) (l: list X)
: (list X): =
match l with
| [] => []
| h :: t => if test h then h :: (filter test t)
else filter test t
end.
Example test: filter evenb [1,2,3,4,5] = [2,4].
Proof. reflexivity. Qed.
map applies this function to each element of the list, returning a list of results: Fixpoint map {XY: Type} (f: X → Y) (l: list X)
: (list Y): =
match l with
| [] => []
| h :: t => (fh) :: (map ft)
end.
Source: https://habr.com/ru/post/184446/
All Articles