Skip to content

Recursive types #154

Closed
Closed
@xsebek

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:

Metadata

Assignees

Labels

C-ProjectA larger project, more suitable for experienced contributors.L-Language designIssues relating to the overall design of the Swarm language.L-Type systemIssues related to the Swarm language type system.S-ModerateThe fix or feature would substantially improve user experience.Z-FeatureA new feature to be added to the game.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions