Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding nested pattern matching (possibly with maybe monad)

Tags:

f#

How could nested pattern matching, such as the following example, be re-written so that None is specified only once? I think the Maybe monad solves this problem. Is there something similar in the F# core library? Or, is there an alternative approach?

match a with
| Some b ->
    let c = b.SomeProperty
    match c with
    | Some d ->
        let e = d.SomeProperty
        //and so on...
    | None -> ()
| None -> ()
like image 838
Daniel Avatar asked Nov 01 '10 21:11

Daniel


2 Answers

you can solve this using built-in capabilities: Option.bind

type A = 
    member this.X : B option = Unchecked.defaultof<_>
and B =
    member this.Y : С option = Unchecked.defaultof<_>
and С =
    member this.Z : string option = Unchecked.defaultof<_>


let a : A = Unchecked.defaultof<_>
let v = 
    match 
        a.X 
        |> Option.bind (fun v -> v.Y) 
        |> Option.bind (fun v -> v.Z) with
    | Some s -> s
    | None -> "<none>"  

Frankly, I doubt that introducing full-fledged 'maybe' implementation (via computation expressions) here can shorten the code.

EDIT: Dream mode - on

I think that version with Option.bind can be made smaller if F# has more lightweight syntax for the special case: lambda that refer to some member of its argument:

"123" |> fun s -> s.Length // current version
"123" |> #.Length // hypothetical syntax

This is how the sample can be rewritten in Nemerle that already has such capabilities:

using System;
using Nemerle.Utility; // for Accessor macro : generates property for given field

variant Option[T]
{
    | Some {value : T}
    | None
}

module OptionExtensions
{
    public Bind[T, U](this o : Option[T], f : T -> Option[U]) : Option[U]
    {
        match(o)
        {
            | Option.Some(value) => f(value)
            | Option.None => Option.None()
        }
    }
}

[Record] // Record macro: checks existing fields and creates constructor for its initialization  
class A
{   
    [Accessor]
    value : Option[A];
}

def print(_)
{
    // shortened syntax for functions with body -> match over arguments
    | Option.Some(_) => Console.WriteLine("value");
    | Option.None => Console.WriteLine("none");
}

def x = A(Option.Some(A(Option.Some(A(Option.None())))));
print(x.Value.Bind(_.Value)); // "value"
print(x.Value.Bind(_.Value).Bind(_.Value)); // "none"
like image 124
desco Avatar answered Sep 23 '22 00:09

desco


I like desco's answer; one should always favor built-in constructs. But FWIW, here's what a workflow version might look like (if I understand the problem correctly):

type CE () =

  member this.Bind (v,f) =
    match v with
      | Some(x) -> f x
      | None -> None

  member this.Return v = v


type A (p:A option) =

  member this.P 
    with get() = p


let f (aIn:A option) = CE () {
  let! a = aIn 
  let! b = a.P 
  let! c = b.P 
  return c.P }

let x = f (Some(A(None)))

let y = f (Some(A(Some(A(Some(A(Some(A(None)))))))))

printfn "Your breakpoint here."
like image 32
TechNeilogy Avatar answered Sep 22 '22 00:09

TechNeilogy