lean4-htt/tests/lean/run/rational.lean
Leonardo de Moura e31b17484a feat: add instances from coe + homogeneous operator to heterogeneous operator
@Kha The new `rational.lean` test shows their usefulness. We just
define the monorphic version and a coercion, and get a bunch of `HAdd`
instances for free.
2020-12-01 15:30:10 -08:00

37 lines
586 B
Text

structure Rat where
num : Int
den : Nat
pos : den > 0
instance (n : Nat) : OfNat Rat n where
ofNat := {
num := n
den := 1
pos := decide!
}
instance : Add Rat where
add a b := {
num := a.num * b.den + b.num * a.den
den := a.den * b.den
pos := sorry
}
instance : Coe Int Rat where
coe n := {
num := n
den := 1
pos := decide!
}
def f1 (x : Rat) (n : Nat) : Rat :=
x + n + 1
def f2 (x : Rat) (n : Nat) : Rat :=
1 + x + n
def f3 (x : Rat) (n : Nat) : Rat :=
1 + n + x + 2
def f4 (x : Rat) (n : Nat) : Rat :=
n + 1 + x + n