Skip to content

Many could be more lazy #247

@whxvd

Description

@whxvd

Thanks a lot for creating this book! It is a great learning resource.

With Many, as defined in chapter 4.3. (Example: Arithmetic in Monads), examples like

#eval (addsTo 15 (.replicate 1000 1)).take 0

quickly crash Lean (error: deep recursion). This can be remedied by changing the definition of Many.union to the one given for Many.orElse in section 5.4.2. (The Alternative Class).

The current definition of Many.bind is:

def Many.bind : Many α → (α → Many β) → Many β
| .none, _ => .none
| .more x xs, f => union (f x) (bind (xs ()) f)

The xs () in the recursive case defeats laziness. With union = orElse it becomes:

def Many.bind : Many α → (α → Many β) → Many β
| .none, _ => .none
| .more x xs, f => union (f x) (fun () => (bind (xs ()) f))

This popped up in #new members > Stuck on ManyT in "Functional Programming in Lean".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions