Closed
Description
Add a rec
(like Fix
) operator which would open the door to any recursive data structure, like list, set or map.
This would also add two reserved function names:
roll : f (rec f) -> rec f
unroll : rec f -> f (rec f)
Example from IRC (lets assume a + b = L a | R b
, and functions getLeft
, getRight
):
type listf a b = () + a * b end
type list a = rec (listf a) end
def nil : list a = roll (L ()) end
def cons : a -> list a = \x. \xs. roll (R (x,xs)) end
def head : list a -> a = \xs. fst (getRight (unroll xs)) end
def tail : list a -> list a = \xs. snd (getRight (unroll xs)) end
> let l = cons 1 (cons 2 (cons 3 nil)) in head l, tail l
(1, cons 2 (cons 3 nil)) : int * rec
Special case for list
As the list is particularly useful it might be provided to players early and internally stored as rec
, with special rules for parsing and pretty-printing. Something like [1,2,3]
and hardcoding the type synonym list
.
In gameplay, this could be introduced as a "stable form" of something more advanced. Having the appropriate device installed would load the basic list functions into the dictionary. Curious players could then type > cons
and wonder what it will later be useful for. 🙂
Related:
- would require Add type synonyms (or just generalize to System Fω) #153 to get the
rec
applied to correct types - Add sum types to the language #46 to make it useful (though there is the magical
streamf a = (int, () -> a)
) - Change
run
toimport
and typecheck it properly #495 (so less savvy players couldload data.list.sw
which they would find on Wiki)