Type-Driven Development with Idris presents this exercise:
same_cons : {xs : List a} -> {ys : List a} -> xs = ys -> x :: xs = x :: ys
However, I attempted to implement it via:
data EqList : (xs : List a) -> (ys : List a) -> Type where
Same : (xs: List a) -> EqList xs xs
sameS : (xs : List a) -> (ys : List a) -> (x: a) -> (eq : EqList xs ys) -> EqList (x :: xs) (x :: ys)
sameS xs xs x (Same xs) = Same (x :: xs)
same_cons : {xs : List a} -> {ys : List a} -> (eq : EqList xs ys) -> EqList (x :: xs) (x :: ys)
same_cons {xs} {ys} eq = sameS xs ys _ eq
I inferred the x
in EqList (x :: xs) (x :: ys)
because I'm confused how to get an x
if xs
and ys
are empty.
Also, the above compiles, but it failed when I tried to call it:
*Exercises> same_cons (Same [1,2,3])
(input):Can't infer argument x to same_cons