feat(library/standard): add function 'helper' module
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
5296275c41
commit
5529ef1056
2 changed files with 78 additions and 0 deletions
55
library/standard/function.lean
Normal file
55
library/standard/function.lean
Normal file
|
@ -0,0 +1,55 @@
|
|||
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
||||
-- Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-- Author: Leonardo de Moura
|
||||
namespace function
|
||||
|
||||
section
|
||||
parameters {A : Type} {B : Type} {C : Type} {D : Type} {E : Type}
|
||||
|
||||
abbreviation compose (f : B → C) (g : A → B) : A → C
|
||||
:= λx, f (g x)
|
||||
|
||||
abbreviation id (a : A) : A
|
||||
:= a
|
||||
|
||||
abbreviation const (a : A) : B → A
|
||||
:= λx, a
|
||||
|
||||
abbreviation on_fun (f : B → B → C) (g : A → B) : A → A → C
|
||||
:= λx y, f (g x) (g y)
|
||||
|
||||
abbreviation combine (f : A → B → C) (op : C → D → E) (g : A → B → D) : A → B → E
|
||||
:= λx y, op (f x y) (g x y)
|
||||
end
|
||||
|
||||
abbreviation dcompose {A : Type} {B : A → Type} {C : Π {x : A}, B x → Type} (f : Π {x : A} (y : B x), C y) (g : Πx, B x) : Πx, C (g x)
|
||||
:= λx, f (g x)
|
||||
|
||||
abbreviation flip {A : Type} {B : Type} {C : A → B → Type} (f : Πx y, C x y) : Πy x, C x y
|
||||
:= λy x, f x y
|
||||
|
||||
abbreviation app {A : Type} {B : A → Type} (f : Πx, B x) (x : A) : B x
|
||||
:= f x
|
||||
|
||||
-- Yet another trick to anotate an expression with a type
|
||||
abbreviation is_typeof (A : Type) (a : A) : A
|
||||
:= a
|
||||
|
||||
precedence `∘`:60
|
||||
precedence `∘'`:60
|
||||
precedence `on`:1
|
||||
precedence `$`:1
|
||||
precedence `-[`:1
|
||||
precedence `]-`:1
|
||||
precedence `⟨`:1
|
||||
|
||||
infixr ∘ := compose
|
||||
infixr ∘' := dcompose
|
||||
infixl on := on_fun
|
||||
notation `typeof` t `:` T := is_typeof T t
|
||||
infixr $ := app
|
||||
notation f `-[` op `]-` g := combine f op g
|
||||
-- Trick for using any binary function as infix operator
|
||||
notation a `⟨` f `⟩` b := f a b
|
||||
|
||||
end
|
23
tests/lean/run/fun.lean
Normal file
23
tests/lean/run/fun.lean
Normal file
|
@ -0,0 +1,23 @@
|
|||
import function logic num bool
|
||||
using function num bool
|
||||
|
||||
|
||||
variable f : num → bool
|
||||
variable g : num → num
|
||||
|
||||
check f ∘ g ∘ g
|
||||
|
||||
check typeof id : num → num
|
||||
check num → num ⟨is_typeof⟩ id
|
||||
|
||||
variable h : num → bool → num
|
||||
|
||||
check flip h
|
||||
check flip h '0 zero
|
||||
|
||||
check typeof flip h '0 zero : num
|
||||
|
||||
variable f1 : num → num → bool
|
||||
variable f2 : bool → num
|
||||
|
||||
check (f1 on f2) '0 '1
|
Loading…
Reference in a new issue