Skip to content

Instantly share code, notes, and snippets.

@5HT
Created August 18, 2016 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5HT/de3dd4b2b2d13d2d3e19701d07a99856 to your computer and use it in GitHub Desktop.
Save 5HT/de3dd4b2b2d13d2d3e19701d07a99856 to your computer and use it in GitHub Desktop.
Functor in Coq
Require Import Coq.Program.Basics.
Require Import Coq.Program.Syntax.
Require Import Coq.Init.Datatypes.
Require Import Coq.Unicode.Utf8.
Open Local Scope program_scope.
Open Local Scope list_scope.
Open Local Scope type_scope.
Class Functor (φ : Type → Type) := {
fmap : forall {α β}, (α → β) → φ α → φ β;
(** Functor laws **)
fmap_identity : forall α (x : φ α), fmap id x = x;
fmap_composition : forall α β γ (f : β → γ) (g : α → β) (x : φ α),
fmap (f ∘ g) x = (fmap f ∘ fmap g) x
}.
Instance list_functor : Functor list := {
fmap α β := fix fmap f xs := match xs with
| [] => []
| x :: xs => (f x) :: (fmap f xs)
end
}.
Proof.
(** fmap_identity **)
intros.
induction x as [| x xs].
reflexivity.
rewrite IHxs. reflexivity.
(** fmap_composition **)
intros.
induction x as [| x xs].
reflexivity.
rewrite IHxs. reflexivity.
Qed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment