Merge pull request #914 from stefan-hoeck/lazy-list-impls

Eq, Ord, and Show instances for LazyList
This commit is contained in:
Edwin Brady 2021-01-16 14:07:26 +00:00 committed by GitHub
commit 96257f23c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,34 @@ data LazyList : Type -> Type where
--- Interface implementations ---
public export
Eq a => Eq (LazyList a) where
[] == [] = True
x :: xs == y :: ys = x == y && xs == ys
_ == _ = False
public export
Ord a => Ord (LazyList a) where
compare [] [] = EQ
compare [] (x :: xs) = LT
compare (x :: xs) [] = GT
compare (x :: xs) (y ::ys)
= case compare x y of
EQ => compare xs ys
c => c
export
Show a => Show (LazyList a) where
show [] = "[]"
show (h :: t) = "[" ++ show' "" h t ++ "]"
where
-- Idris didn't like the lazyness involved when using the
-- same implementation as for `List`, therefore, this was
-- adjusted to first force the head and tail of the list.
show' : String -> a -> LazyList a -> String
show' acc h Nil = acc ++ show h
show' acc h (x :: xs) = show' (acc ++ show h ++ ", ") x xs
public export
Semigroup (LazyList a) where
[] <+> ys = ys