This is a follow-up question of Lean pass type as parameter
I tried jmc's suggestion, which seemed to work, but then I got stuck at another point. The original purpose of the question was to define the categories of groups and rings, but now apparently I am unable to define group morphisms:
class group :=
(set: Type)
(add: set → set → set)
infix + := group.add
class group_morphism (G H: group) :=
(f: G.set → H.set)
(additive: ∀ g h : G.set, f(g + h) = (f g) + (f h))
I get an error at the first +
. Lean seems to think that this refers to H.add
, whereas it is supposed to refer to G.add
.
The issue is that group should not be a class. If you look at the type of
group.add
, by#check @group.add
you will getThe square brackets around
c : group
indicate that this is an implicit argument that will be inferred by type class inference. You won't have to explicitly type this argument, but Lean will try to work out what it is. Type class inference works best for types where there is only one inhabitant you ever want to use.In mathlib the definition of group is closer to
On a particular type, there is usually only one group structure you want to refer to, so in mathlib, a type like
add_group int
has only one inhabitant you care about.Lean automatically chose
H
as the canonical representative of the typegroup
, but this is not the one you wanted.So usually when you deal with groups the type and the group structure are kept as separate objects, they are not put into a pair. However for category theory, the usual approach doesn't work, an object is a pair of a type and a group structure.
The setup in mathlib is closer to the following. The
coe_to_sort
tells Lean how to take agroup_obj
and interpret it as a Type without having explicitly write G.set, thegroup_obj_group
instance tells Lean how to automatically infer the group structure on the type of agroup_obj