Zulip Chat Archive

Stream: lean4

Topic: why this code fails?


Asei Inoue (Jul 06 2024 at 08:49):

structure NonEmptyList (α : Type) : Type where
  head : α
  tail : List α

variable {α : Type}

instance {x : α} {xs : List α} : CoeDep (List α) (x :: xs) (NonEmptyList α) where
  coe := {head := x, tail := xs}


/-
application type mismatch
  [1, 2].head
argument
  [1, 2]
has type
  List Nat : Type
but is expected to have type
  NonEmptyList ?m.435 : Type
-/
#check NonEmptyList.head [1, 2]

Floris van Doorn (Jul 06 2024 at 12:13):

Look at the precise type of docs#List.head

Floris van Doorn (Jul 06 2024 at 12:16):

#check NonEmptyList.head ([1, 2] : NonEmptyList Nat) does work.

Floris van Doorn (Jul 06 2024 at 12:16):

Do does #check NonEmptyList.head (α := Nat) [1, 2]

Floris van Doorn (Jul 06 2024 at 12:17):

I expect that CoeDep instances only work if the exact expected type is known. In your case, you only know it's NonEmptyList _, not NonEmptyList Nat

Floris van Doorn (Jul 06 2024 at 12:45):

Note: I wrote my first message because the error message is confusing. The [1, 2].head in the error message looks like List.head [1, 2].
(lean4#4670)

Asei Inoue (Jul 06 2024 at 14:31):

@Floris van Doorn Thank you!!

#check NonEmptyList.head (α := Nat) [1, 2]

Asei Inoue (Jul 12 2024 at 23:20):

(deleted)


Last updated: May 02 2025 at 03:31 UTC