by chriswarbo · 3y ago · 0 comments
Not quite; everything in Agda must either terminate or co-terminate. We can use co-termination to represent general recursion, e.g. using Delay:
data Delay : Type -> Type where
Now : {t: Type} -> t -> Delay t
Later : {t: Type} -> Delay t -> Delay t
It's trivial to write any program using Delay, e.g. if we have a function 'step: TuringMachine -> TuringMachine' which performs one step of any Turing machine, we can run any TuringMachine using: run : TuringMachine -> Delay TuringMachine
run thisState = case thisState == nextState of
true -> Now thisState -- We've entered a halting state
false -> Later (run nextState)
where nextState = step thisState
This is similar to "infinite lists" in lazy languages (like Haskell); e.g. if we call 'run' with a non-halting TuringMachine, we'll get 'Later (Later (Later (...)))'Ironically, I'm more familiar with using codata in Coq; even though it's notoriously under-supported (breaking subject-reduction, no alternative to cofix, etc.)
Yes, 'Delay t' stores a value "at the end" (in the 'Now' constructor), but doesn't store anything "on the way" (in the 'Later' constructor).
In contrast, 'List t' stores values "on the way" (in Cons), but doesn't store anything "at the end" (in Nil).
Another useful example is (Peano) natural numbers, which don't store any values "at the end" (Zero) or "on the way" (Successor).
Natural numbers are isomorphic to 'List ()' and 'Delay ()' (whose 'elements' contain no information)
> Lists have a consumable head
Slight nit-pick: only non-empty lists have a head. Non-empty lists are isomorphic to '(t, List t)'.
Consuming the head of a 'Delay t' is still useful, since they act like a counter; or (equivalently) as a notion of logical time.
I find this stuff fascinating, and explored it in more detail at http://www.chriswarbo.net/blog/2014-12-04-Nat_like_types.htm...