Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe's #88

Open
71104 opened this issue May 30, 2016 · 1 comment
Open

Maybe's #88

71104 opened this issue May 30, 2016 · 1 comment

Comments

@71104
Copy link
Owner

71104 commented May 30, 2016

#83 removed null. There are still important use cases for nullary values where undefined is unsuitable because it wouldn't type.

For example, binary trees:

# assumption: io.read returns undefined at EOF

let Node = fix fn Node, this, label ->
  if label = undefined
  then undefined
  else let this.label = label,
    this.left = new Node io.read,
    this.right = new Node io.read,
    this.height = fn this ->
      if this = undefined
      then 0

      # type error here: this.left and this.right do not necessarily have a "height" field
      else 1 + {this.left.height, this.right.height}.max

  in this

in

let io.read_node = fn io -> (new Node io.read) in

io.read_node.height

The best solution is to replace null with Haskell-style maybe's. The above would become:

# assumption: io.read returns a maybe

let Null.height = fn this -> 0 in

let Node = fix fn Node, this, label ->
  let this.label = label,
    this.left = io.read (new Node) Null,
    this.right = io.read (new Node) Null,
    this.height = fn this ->
      1 + {this.left.height, this.right.height}.max
  in this
in

let io.read_node = fn io -> (io.read (new Node) Null) in

io.read_node.height

Implement the following terms:

  • just ≡ λ v, f, z . f v
  • nothing ≡ λ f, z . z
  • fmap ≡ λ f, m . m (λ v . just (f v)) nothing
  • bind ≡ λ m, f . m f nothing

Incidentally, nothing is alpha-equivalent to false.

@71104
Copy link
Owner Author

71104 commented May 30, 2016

#89 might actually obsolete all the use cases of "nullable" values, but maybe's would still be useful to handle failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant