You can create your own natural numbers ...
data Nat = Zero | Succ Nat deriving (Show, Eq, Ord) len :: [a] -> Nat len = foldr (const Succ) Zero toLazy :: Int -> Nat toLazy 0 = Zero toLazy n = Succ (toLazy (n-1)) a = len [1..] > toLazy 42
Then a is true, thanks to a lazy estimate. It is very important that len ββuses foldr, foldl does not work in infinite lists. And you can do arithmetic too (not verified):
instance Num Nat where (+) (Succ x) y = Succ (x + y) (+) Zero y = y (*) Zero y = Zero (*) x Zero = Zero (*) (Succ x) y = y + (x * y) fromInteger = toLazy abs = id negate = error "Natural only" signum Zero = Zero signum (Succ x) = Succ Zero
For example, 2 * infinity + 3 is infinite:
let infinity = Succ infinity *Main> 2 * infinity + 3 (Succ (Succ (Succ ^cCc (Succ (Succ (SuccInterrupted.
Second solution
Another solution is to use lists () as lazy natural numbers.
len = map (const ()) toLazy n = replicate n () a = len [1..] > toLazy 42
Check Hackage .
Edit: Added Num instance.
Edit2:
Translation of the second solution in Python:
import itertools def length(x): return itertools.imap(lambda: (), x) def to_lazy(n): return itertools.chain([()] * n)
To add numbers, use itertools.chain; to multiply, use itertools.product. It is not as beautiful as in Haskell, but it works.
In any other strict closure language, you can simulate laziness using type () -> a instead of a. Using this, you can port the first solution from Haskell to other languages. However, it will quickly become unreadable.
See also a good article on single-line Python .