30th January 2011, 05:16 pm
The post Fixing lists defined a (commonly used) type of vectors, whose lengths are determined statically, by type. In Vec n a
, the length is n
, and the elements have type a
, where n
is a type-encoded unary number, built up from zero and successor (Z
and S
).
infixr 5 :<
data Vec ∷ * → * → * where
ZVec ∷ Vec Z a
(:<) ∷ a → Vec n a → Vec (S n) a
It was fairly easy to define foldr
for a Foldable
instance, fmap
for Functor
, and (⊛)
for Applicative
. Completing the Applicative
instance is tricky, however. Unlike foldr
, fmap
, and (⊛)
, pure
doesn’t have a vector structure to crawl over. It must create just the right structure anyway. I left this challenge as a question to amuse readers. In this post, I give a few solutions, including my current favorite.
You can find the code for this post and the two previous ones in a code repository.
Continue reading ‘Doing more with length-typed vectors’ »
The post Fixing lists defined a (commonly used) type of vectors, whose lengths are determined statically, by type. In Vec n a, the length is n, and the elements have...
30th January 2011, 10:14 am
In the post Memoizing polymorphic functions via unmemoization, I toyed with the idea of lists as tries. I don’t think [a]
is a trie, simply because [a]
is a sum type (being either nil or a cons), while tries are built out of the identity, product, and composition functors. In contrast, Stream
is a trie, being built solely with the identity and product functors. Moreover, Stream
is not just any old trie, it is the trie that corresponds to Peano (unary natural) numbers, i.e., Stream a ≅ N → a
, where
data N = Zero | Succ N
data Stream a = Cons a (Stream a)
If we didn’t already know the Stream
type, we would derive it systematically from N
, using standard isomorphisms.
Stream
is a trie (over unary numbers), thanks to it having no choice points, i.e., no sums in its construction. However, streams are infinite-only, which is not always what we want. In contrast, lists can be finite, but are not a trie in any sense I understand. In this post, I look at how to fix lists, so they can be finite and yet be a trie, thanks to having no choice points (sums)?
You can find the code for this post and the previous one in a code repository.
Edits:
Continue reading ‘Fixing lists’ »
In the post Memoizing polymorphic functions via unmemoization, I toyed with the idea of lists as tries. I don’t think [a] is a trie, simply because [a] is a sum...