Changes between Version 26 and Version 27 of ViewPatterns


Ignore:
Timestamp:
Apr 17, 2007 2:05:06 PM (8 years ago)
Author:
simonpj
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ViewPatterns

    v26 v27  
    668668=== [http://blogs.msdn.com/dsyme/archive/2006/08/16/ActivePatterns.aspx F# Active Patterns] ===
    669669
    670 Simon started this design discussion after talking to Don Syme about F#'s '''active patterns''',
    671 which serve a very similar purpose.
     670Simon started this design discussion after talking to Don Syme about F#'s '''active patterns''', which serve a very similar purpose. These combine both “total” discrimination (views) and “partial” discrimination (implicit maybe) into one mechanism. It does this by embedding the names of discriminators in the names of matching functions, via “values with structured names”.  Sample uses include matching on .NET objects and XML.
    672671
    673672Here is [http://blogs.msdn.com/dsyme/archive/2007/04/07/draft-paper-on-f-active-patterns.aspx a full paper describing the design], by Don Syme, Gregory Neverov, and James Margetson (April 2007).
    674673
    675 F# 1.1.12 included a preliminary version
    676 of extensible pattern matching where values effectively of type
    677 {{{
    678   val Pattern: int -> int option
    679 }}}
    680 could be used as query functions. Some examples are documented
    681 nicely at [[http://tomasp.net/blog/quotvis-reloaded.aspx : Tomas Petricek's website]].
    682 
     674The feature is implemented in F# 1.9. Some code snippets are below.
     675{{{
     676    let (|Rect|) (x:complex) = (x.RealPart, x.ImaginaryPart)
     677    let (|Polar|) (x:complex) = (x.Magnitude , x.Phase)
     678
     679    let mulViaRect c1 c2 =
     680        match c1,c2 with
     681        | Rect(ar,ai), Rect(br,bi) -> Complex.mkRect(ar*br - ai*bi, ai*br + bi*ar)
     682
     683    let mulViaPolar c1 c2 =
     684        match c1,c2 with
     685        | Polar (r1,th1),Polar (r2,th2) -> Complex.mkPolar(r1*r2, th1+th2)
     686
     687    let mulViaRect2  (Rect(ar,ai))   (Rect(br,bi))   = Complex.mkRect(ar*br - ai*bi,
     688                                                                      ai*br + bi*ar)
     689    let mulViaPolar2 (Polar(r1,th1)) (Polar(r2,th2)) = Complex.mkPolar(r1*r2, th1+th2)
     690}}}
     691And for views:
     692{{{
     693    open System
     694   
     695    let (|Named|Array|Ptr|Param|) (typ : System.Type) =
     696        if typ.IsGenericType        then Named(typ.GetGenericTypeDefinition(),
     697                                               typ.GetGenericArguments())
     698        elif not typ.HasElementType then Named(typ, [| |])
     699        elif typ.IsArray            then Array(typ.GetElementType(),
     700                                               typ.GetArrayRank())
     701        elif typ.IsByRef            then Ptr(true,typ.GetElementType())
     702        elif typ.IsPointer          then Ptr(false,typ.GetElementType())
     703        elif typ.IsGenericParameter then Param(typ.GenericParameterPosition,
     704                                               typ.GetGenericParameterConstraints())
     705        else failwith "MSDN says this can't happen"
     706
     707    let rec freeVarsAcc typ acc =
     708        match typ with
     709        | Named (con, args) -> Array.fold_right freeVarsAcc args acc
     710        | Array (arg, rank) -> freeVarsAcc arg acc
     711        | Ptr (_,arg)       -> freeVarsAcc arg acc
     712        | Param(pos,cxs)    -> Array.fold_right freeVarsAcc cxs (typ :: acc)
     713}}}
    683714
    684715=== [http://lambda-the-ultimate.org/node/1960 Emir, Odersky, Williams: Matching objects with patterns] ===