Skip to content

Instantly share code, notes, and snippets.

@avigad
Created March 11, 2017 02:06
Show Gist options
  • Save avigad/35d458b045883c44919596dbc63cd397 to your computer and use it in GitHub Desktop.
Save avigad/35d458b045883c44919596dbc63cd397 to your computer and use it in GitHub Desktop.
Using mk_inhabitant_using and interactive tactic
open tactic
open smt_tactic
meta def blast : tactic unit := using_smt $ intros >> try simp >> try eblast
notation `♮` := by abstract { blast }
universe variables u v
structure Category :=
(Obj : Type u)
(Hom : Obj → Obj → Type v)
(identity : Π X : Obj, Hom X X)
(compose : Π { X Y Z : Obj }, Hom X Y → Hom Y Z → Hom X Z)
(left_identity : ∀ { X Y : Obj } (f : Hom X Y), compose (identity X) f = f)
(right_identity : ∀ { X Y : Obj } (f : Hom X Y), compose f (identity Y) = f)
(associativity : ∀ { W X Y Z : Obj } (f : Hom W X) (g : Hom X Y) (h : Hom Y Z),
compose (compose f g) h = compose f (compose g h))
attribute [ematch,simp] Category.left_identity
attribute [ematch,simp] Category.right_identity
attribute [ematch] Category.associativity
universe variables u1 v1 u2 v2
structure Functor (C : Category.{ u1 v1 }) (D : Category.{ u2 v2 }) :=
(onObjects : C^.Obj → D^.Obj)
(onMorphisms : Π { X Y : C^.Obj },
C^.Hom X Y → D^.Hom (onObjects X) (onObjects Y))
(identities : ∀ (X : C^.Obj),
onMorphisms (C^.identity X) = D^.identity (onObjects X))
(functoriality : ∀ { X Y Z : C^.Obj } (f : C^.Hom X Y) (g : C^.Hom Y Z),
onMorphisms (C^.compose f g) = D^.compose (onMorphisms f) (onMorphisms g))
attribute [simp,ematch] Functor.identities
attribute [simp,ematch] Functor.functoriality
instance Functor_to_onObjects { C D : Category }: has_coe_to_fun (Functor C D) :=
{ F := λ f, C^.Obj → D^.Obj,
coe := Functor.onObjects }
structure NaturalTransformation { C D : Category } ( F G : Functor C D ) :=
(components: Π X : C^.Obj, D^.Hom (F X) (G X))
(naturality: ∀ { X Y : C^.Obj } (f : C^.Hom X Y),
D^.compose (F^.onMorphisms f) (components Y) = D^.compose (components X) (G^.onMorphisms f))
attribute [ematch] NaturalTransformation.naturality
instance NaturalTransformation_to_components { C D : Category } { F G : Functor C D } : has_coe_to_fun (NaturalTransformation F G) :=
{ F := λ f, Π X : C^.Obj, D^.Hom (F X) (G X),
coe := NaturalTransformation.components }
lemma NaturalTransformations_componentwise_equal
{ C D : Category }
{ F G : Functor C D }
( α β : NaturalTransformation F G )
( w : ∀ X : C^.Obj, α X = β X ) : α = β :=
begin
induction α with α_components α_naturality,
induction β with β_components β_naturality,
have hc : α_components = β_components, from funext w,
by subst hc
end
@[reducible] definition IdentityNaturalTransformation { C D : Category } (F : Functor C D) : NaturalTransformation F F :=
{
components := λ X, D^.identity (F X),
naturality := ♮
}
@[reducible] definition vertical_composition_of_NaturalTransformations
{ C D : Category }
{ F G H : Functor C D }
( α : NaturalTransformation F G )
( β : NaturalTransformation G H ) : NaturalTransformation F H :=
{
components := λ X, D^.compose (α X) (β X),
naturality := ♮
}
meta def mk_inhabitant_using (A : expr) (t : tactic unit) : tactic expr :=
do m ← mk_meta_var A,
gs ← get_goals,
set_goals [m],
t,
r ← instantiate_mvars m,
set_goals gs,
return r
namespace tactic
meta def apply_and_mk_decl (n : name) (tac : tactic unit) : tactic unit := do
t ← target,
val ← mk_inhabitant_using t tac,
add_aux_decl n t val tt,
apply val
namespace interactive
open lean.parser
open interactive
meta def apply_and_mk_decl (n : parse ident) (tac : itactic) : tactic unit :=
tactic.apply_and_mk_decl n tac
end interactive
end tactic
definition FunctorCategory ( C D : Category ) : Category :=
{
Obj := Functor C D,
Hom := λ F G, NaturalTransformation F G,
identity := λ F, IdentityNaturalTransformation F,
compose := @vertical_composition_of_NaturalTransformations C D,
left_identity := by apply_and_mk_decl FunctorCategory_left_identity
{ blast, apply NaturalTransformations_componentwise_equal, blast },
right_identity := begin blast, apply NaturalTransformations_componentwise_equal, blast end,
associativity := begin blast, apply NaturalTransformations_componentwise_equal, blast end
}
#check FunctorCategory_left_identity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment