2014-11-28 12:06:46 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
|
|
|
|
|
|
General operations on functions.
|
|
|
|
|
-/
|
2015-05-23 06:16:36 +00:00
|
|
|
|
import logic.cast
|
|
|
|
|
|
2014-07-24 23:29:39 +00:00
|
|
|
|
namespace function
|
|
|
|
|
|
2014-10-10 23:33:58 +00:00
|
|
|
|
variables {A : Type} {B : Type} {C : Type} {D : Type} {E : Type}
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition compose [reducible] [unfold-f] (f : B → C) (g : A → B) : A → C :=
|
2014-10-10 23:33:58 +00:00
|
|
|
|
λx, f (g x)
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition compose_right [reducible] [unfold-f] (f : B → B → B) (g : A → B) : B → A → B :=
|
2015-04-09 17:54:28 +00:00
|
|
|
|
λ b a, f b (g a)
|
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition compose_left [reducible] [unfold-f] (f : B → B → B) (g : A → B) : A → B → B :=
|
2015-04-09 17:54:28 +00:00
|
|
|
|
λ a b, f (g a) b
|
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition id [reducible] [unfold-f] (a : A) : A :=
|
2014-10-10 23:33:58 +00:00
|
|
|
|
a
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition on_fun [reducible] [unfold-f] (f : B → B → C) (g : A → B) : A → A → C :=
|
2014-10-10 23:33:58 +00:00
|
|
|
|
λx y, f (g x) (g y)
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition combine [reducible] [unfold-f] (f : A → B → C) (op : C → D → E) (g : A → B → D)
|
|
|
|
|
: A → B → E :=
|
2014-10-10 23:33:58 +00:00
|
|
|
|
λx y, op (f x y) (g x y)
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition const [reducible] [unfold-f] (B : Type) (a : A) : B → A :=
|
2014-07-29 02:58:57 +00:00
|
|
|
|
λx, a
|
2014-07-28 04:01:59 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition dcompose [reducible] [unfold-f] {B : A → Type} {C : Π {x : A}, B x → Type}
|
2014-11-28 12:06:46 +00:00
|
|
|
|
(f : Π {x : A} (y : B x), C y) (g : Πx, B x) : Πx, C (g x) :=
|
2014-07-29 02:58:57 +00:00
|
|
|
|
λx, f (g x)
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition flip [reducible] [unfold-f] {C : A → B → Type} (f : Πx y, C x y) : Πy x, C x y :=
|
2014-07-29 02:58:57 +00:00
|
|
|
|
λy x, f x y
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-02-17 02:52:41 +00:00
|
|
|
|
definition app [reducible] {B : A → Type} (f : Πx, B x) (x : A) : B x :=
|
2014-07-29 02:58:57 +00:00
|
|
|
|
f x
|
2014-07-24 23:29:39 +00:00
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition curry [reducible] [unfold-f] : (A × B → C) → A → B → C :=
|
2015-04-11 23:45:07 +00:00
|
|
|
|
λ f a b, f (a, b)
|
|
|
|
|
|
2015-05-07 05:23:39 +00:00
|
|
|
|
definition uncurry [reducible] [unfold-c 5] : (A → B → C) → (A × B → C) :=
|
2015-04-11 23:45:07 +00:00
|
|
|
|
λ f p, match p with (a, b) := f a b end
|
|
|
|
|
|
|
|
|
|
theorem curry_uncurry (f : A → B → C) : curry (uncurry f) = f :=
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
theorem uncurry_curry (f : A × B → C) : uncurry (curry f) = f :=
|
|
|
|
|
funext (λ p, match p with (a, b) := rfl end)
|
|
|
|
|
|
2014-07-24 23:29:39 +00:00
|
|
|
|
precedence `∘'`:60
|
|
|
|
|
precedence `on`:1
|
|
|
|
|
precedence `$`:1
|
|
|
|
|
|
|
|
|
|
infixr ∘ := compose
|
|
|
|
|
infixr ∘' := dcompose
|
|
|
|
|
infixl on := on_fun
|
|
|
|
|
infixr $ := app
|
|
|
|
|
notation f `-[` op `]-` g := combine f op g
|
|
|
|
|
|
2015-04-03 22:43:44 +00:00
|
|
|
|
lemma left_inv_eq {finv : B → A} {f : A → B} (linv : finv ∘ f = id) : ∀ x, finv (f x) = x :=
|
|
|
|
|
take x, show (finv ∘ f) x = x, by rewrite linv
|
|
|
|
|
|
2015-04-17 03:52:18 +00:00
|
|
|
|
lemma right_inv_eq {finv : B → A} {f : A → B} (rinv : f ∘ finv = id) : ∀ x, f (finv x) = x :=
|
|
|
|
|
take x, show (f ∘ finv) x = x, by rewrite rinv
|
2015-04-03 22:43:44 +00:00
|
|
|
|
|
2015-04-17 03:52:18 +00:00
|
|
|
|
definition injective (f : A → B) : Prop := ∀ a₁ a₂, f a₁ = f a₂ → a₁ = a₂
|
|
|
|
|
|
|
|
|
|
definition surjective (f : A → B) : Prop := ∀ b, ∃ a, f a = b
|
|
|
|
|
|
|
|
|
|
definition has_left_inverse (f : A → B) : Prop := ∃ finv : B → A, finv ∘ f = id
|
|
|
|
|
|
|
|
|
|
definition has_right_inverse (f : A → B) : Prop := ∃ finv : B → A, f ∘ finv = id
|
|
|
|
|
|
|
|
|
|
lemma injective_of_has_left_inverse {f : A → B} : has_left_inverse f → injective f :=
|
|
|
|
|
assume h, take a b, assume faeqfb,
|
2015-04-03 22:43:44 +00:00
|
|
|
|
obtain (finv : B → A) (inv : finv ∘ f = id), from h,
|
|
|
|
|
calc a = finv (f a) : by rewrite (left_inv_eq inv)
|
|
|
|
|
... = finv (f b) : faeqfb
|
|
|
|
|
... = b : by rewrite (left_inv_eq inv)
|
|
|
|
|
|
2015-04-17 03:52:18 +00:00
|
|
|
|
lemma surjective_of_has_right_inverse {f : A → B} : has_right_inverse f → surjective f :=
|
|
|
|
|
assume h, take b,
|
|
|
|
|
obtain (finv : B → A) (inv : f ∘ finv = id), from h,
|
|
|
|
|
let a : A := finv b in
|
|
|
|
|
have h : f a = b, from calc
|
|
|
|
|
f a = (f ∘ finv) b : rfl
|
|
|
|
|
... = id b : by rewrite (right_inv_eq inv)
|
|
|
|
|
... = b : rfl,
|
|
|
|
|
exists.intro a h
|
2015-05-23 06:16:36 +00:00
|
|
|
|
|
|
|
|
|
theorem compose.assoc (f : C → D) (g : B → C) (h : A → B) : (f ∘ g) ∘ h = f ∘ (g ∘ h) :=
|
|
|
|
|
funext (take x, rfl)
|
|
|
|
|
|
|
|
|
|
theorem compose.left_id (f : A → B) : id ∘ f = f :=
|
|
|
|
|
funext (take x, rfl)
|
|
|
|
|
|
|
|
|
|
theorem compose.right_id (f : A → B) : f ∘ id = f :=
|
|
|
|
|
funext (take x, rfl)
|
|
|
|
|
|
|
|
|
|
theorem compose_const_right (f : B → C) (b : B) : f ∘ (const A b) = const A (f b) :=
|
|
|
|
|
funext (take x, rfl)
|
|
|
|
|
|
|
|
|
|
theorem hfunext {A : Type} {B : A → Type} {B' : A → Type} {f : Π x, B x} {g : Π x, B' x}
|
|
|
|
|
(H : ∀ a, f a == g a) : f == g :=
|
|
|
|
|
let HH : B = B' := (funext (λ x, heq.type_eq (H x))) in
|
|
|
|
|
cast_to_heq (funext (λ a, heq.to_eq (heq.trans (cast_app HH f a) (H a))))
|
2014-08-07 23:59:08 +00:00
|
|
|
|
end function
|
2015-02-17 02:52:41 +00:00
|
|
|
|
|
|
|
|
|
-- copy reducible annotations to top-level
|
2015-05-07 05:23:39 +00:00
|
|
|
|
export [reduce-hints] [unfold-hints] function
|