FSharp.Core


DerivedPatterns Module

Contains a set of derived F# active patterns to analyze F# expression objects

Active patterns

Active pattern Description

(|AndAlso|_|) input

Full Usage: (|AndAlso|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: (Expr * Expr) option When successful, the pattern binds the left and right parts of the input expression

An active pattern to recognize expressions of the form a && b

input : Expr

The input expression to match against.

Returns: (Expr * Expr) option

When successful, the pattern binds the left and right parts of the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ true && false @> with
 | AndAlso (a, b) -> (a, b)
 | _ -> failwith "unexpected"
Evaluates to <@ true @>, <@ false @>.

(|Applications|_|) input

Full Usage: (|Applications|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: (Expr * Expr list list) option When successful, the pattern binds the function and curried arguments of the input expression

An active pattern to recognize expressions that represent the application of a (possibly curried or tupled) first class function value

input : Expr

The input expression to match against.

Returns: (Expr * Expr list list) option

When successful, the pattern binds the function and curried arguments of the input expression

Example

 open FSharp.Quotations.Patterns
 open FSharp.Quotations.DerivedPatterns

 match <@ (fun f -> f (1, 2) 3) @> with
 | Lambda(_, Applications (f, curriedArgs)) ->
     curriedArgs |> List.map (fun args -> args.Length)
 | _ -> failwith "unexpected"
Evaluates to [2; 1].

(|Bool|_|) input

Full Usage: (|Bool|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: bool option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant boolean expressions

input : Expr

The input expression to match against.

Returns: bool option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ true @> with
 | Bool v -> v
 | _ -> failwith "unexpected"
Evaluates to true.

(|Byte|_|) input

Full Usage: (|Byte|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: byte option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant byte expressions

input : Expr

The input expression to match against.

Returns: byte option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8uy @> with
 | Byte v -> v
 | _ -> failwith "unexpected"
Evaluates to 8uy.

(|Char|_|) input

Full Usage: (|Char|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: char option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant unicode character expressions

input : Expr

The input expression to match against.

Returns: char option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 'a' @> with
 | Char v -> v
 | _ -> failwith "unexpected"
Evaluates to 'a'.

(|Decimal|_|) input

Full Usage: (|Decimal|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: decimal option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant decimal expressions

input : Expr

The input expression to match against.

Returns: decimal option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8.0M @> with
 | Decimal v -> v
 | _ -> failwith "unexpected"
Evaluates to 8.0M.

(|Double|_|) input

Full Usage: (|Double|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: float option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant 64-bit floating point number expressions

input : Expr

The input expression to match against.

Returns: float option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 1.0 @> with
 | Double v -> v
 | _ -> failwith "unexpected"
Evaluates to 1.0.

(|Int16|_|) input

Full Usage: (|Int16|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: int16 option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant int16 expressions

input : Expr

The input expression to match against.

Returns: int16 option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8s @> with
 | Int16 v -> v
 | _ -> failwith "unexpected"
Evaluates to 8s.

(|Int32|_|) input

Full Usage: (|Int32|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: int32 option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant int32 expressions

input : Expr

The input expression to match against.

Returns: int32 option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8 @> with
 | Int32 v -> v
 | _ -> failwith "unexpected"
Evaluates to 8.

(|Int64|_|) input

Full Usage: (|Int64|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: int64 option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant int64 expressions

input : Expr

The input expression to match against.

Returns: int64 option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8L @> with
 | Int64 v -> v
 | _ -> failwith "unexpected"
Evaluates to 8L.

(|Lambdas|_|) input

Full Usage: (|Lambdas|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: (Var list list * Expr) option When successful, the pattern binds the curried variables and body of the input expression

An active pattern to recognize expressions that represent a (possibly curried or tupled) first class function value

input : Expr

The input expression to match against.

Returns: (Var list list * Expr) option

When successful, the pattern binds the curried variables and body of the input expression

Example

 open FSharp.Quotations.Patterns
 open FSharp.Quotations.DerivedPatterns

 match <@ (fun (a1, a2) b -> ()) @> with
 | Lambdas(curriedVars, _) ->
     curriedVars |> List.map (List.map (fun arg -> arg.Name))
 | _ -> failwith "unexpected"
Evaluates to [["a1"; "a2"]; ["b"]].

(|MethodWithReflectedDefinition|_|) methodBase

Full Usage: (|MethodWithReflectedDefinition|_|) methodBase

Parameters:
    methodBase : MethodBase - The description of the method.

Returns: Expr option The expression of the method definition if found, or None.

An active pattern to recognize methods that have an associated ReflectedDefinition

methodBase : MethodBase

The description of the method.

Returns: Expr option

The expression of the method definition if found, or None.

Example

 open FSharp.Quotations
 open FSharp.Quotations.Patterns
 open FSharp.Quotations.DerivedPatterns

 [<ReflectedDefinition>]
 let f x = (x, x)

 let inpExpr = <@ f 4 @>

 let implExpr =
     match inpExpr with
     | Call(None, MethodWithReflectedDefinition implExpr, [ _argExpr ]) -> implExpr
     | _ -> failwith "unexpected"
Evaluates implExpr to a quotation with the same structure as <@ fun (x: int) -> (x, x) @>, which is the implementation of the method f. Note that the correct generic instantaition has been applied to the implementation to reflect the the type at the callsite.

(|OrElse|_|) input

Full Usage: (|OrElse|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: (Expr * Expr) option When successful, the pattern binds the left and right parts of the input expression

An active pattern to recognize expressions of the form a || b

input : Expr

The input expression to match against.

Returns: (Expr * Expr) option

When successful, the pattern binds the left and right parts of the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ true || false @> with
 | OrElse (a, b) -> (a, b)
 | _ -> failwith "unexpected"
Evaluates to <@ true @>, <@ false @>.

(|PropertyGetterWithReflectedDefinition|_|) propertyInfo

Full Usage: (|PropertyGetterWithReflectedDefinition|_|) propertyInfo

Parameters:
    propertyInfo : PropertyInfo - The description of the property.

Returns: Expr option The expression of the method definition if found, or None.

An active pattern to recognize property getters or values in modules that have an associated ReflectedDefinition

propertyInfo : PropertyInfo

The description of the property.

Returns: Expr option

The expression of the method definition if found, or None.

Example

 open FSharp.Quotations
 open FSharp.Quotations.Patterns
 open FSharp.Quotations.DerivedPatterns

 [<ReflectedDefinition>]
 type C<'T>() =
    member x.Identity = x

 let inpExpr = <@ C<int>().Identity @>

 let implExpr =
     match inpExpr with
     | PropertyGet(Some _, PropertyGetterWithReflectedDefinition implExpr, [ ]) -> implExpr
     | _ -> failwith "unexpected"
Evaluates implExpr to a quotation with the same structure as <@ fun (x: C<int>) () -> x @>, which is the implementation of the property Identity. Note that the correct generic instantaition has been applied to the implementation to reflect the the type at the callsite.

(|PropertySetterWithReflectedDefinition|_|) propertyInfo

Full Usage: (|PropertySetterWithReflectedDefinition|_|) propertyInfo

Parameters:
    propertyInfo : PropertyInfo - The description of the property.

Returns: Expr option The expression of the method definition if found, or None.

An active pattern to recognize property setters that have an associated ReflectedDefinition

propertyInfo : PropertyInfo

The description of the property.

Returns: Expr option

The expression of the method definition if found, or None.

Example

 open FSharp.Quotations
 open FSharp.Quotations.Patterns
 open FSharp.Quotations.DerivedPatterns

 [<ReflectedDefinition>]
 type C<'T>() =
    member x.Count with set (v: int) = ()

 let inpExpr = <@ C<int>().Count <- 3 @>

 let implExpr =
     match inpExpr with
     | PropertySet(Some _, PropertySetterWithReflectedDefinition implExpr, [], _valueExpr) -> implExpr
     | _ -> failwith "unexpected"
Evaluates implExpr to a quotation with the same structure as <@ fun (x: C<int>) (v: int) -> () @>, which is the implementation of the setter for the property Count. Note that the correct generic instantaition has been applied to the implementation to reflect the the type at the callsite.

(|SByte|_|) input

Full Usage: (|SByte|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: sbyte option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant signed byte expressions

input : Expr

The input expression to match against.

Returns: sbyte option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8y @> with
 | SByte v -> v
 | _ -> failwith "unexpected"
Evaluates to 8y.

(|Single|_|) input

Full Usage: (|Single|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: float32 option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant 32-bit floating point number expressions

input : Expr

The input expression to match against.

Returns: float32 option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 1.0f @> with
 | Single v -> v
 | _ -> failwith "unexpected"
Evaluates to 1.0f.

(|SpecificCall|_|) templateParameter

Full Usage: (|SpecificCall|_|) templateParameter

Parameters:
    templateParameter : Expr - The input template expression to specify the method to call.

Returns: Expr -> (Expr option * Type list * Expr list) option The optional target object (present if the target is an instance method), the generic type instantiation (non-empty if the target is a generic instantiation), and the arguments to the function or method.

A parameterized active pattern to recognize calls to a specified function or method. The returned elements are the optional target object (present if the target is an instance method), the generic type instantiation (non-empty if the target is a generic instantiation), and the arguments to the function or method.

templateParameter : Expr

The input template expression to specify the method to call.

Returns: Expr -> (Expr option * Type list * Expr list) option

The optional target object (present if the target is an instance method), the generic type instantiation (non-empty if the target is a generic instantiation), and the arguments to the function or method.

Example

Match a specific call to Console.WriteLine taking one string argument:

 open FSharp.Quotations
 open FSharp.Quotations.Patterns
 open FSharp.Quotations.DerivedPatterns

 let inpExpr = <@ Console.WriteLine("hello") @>

 match inpExpr with
 | SpecificCall <@ Console.WriteLine("1") @> (None, [], [ argExpr ]) -> argExpr
 | _ -> failwith "unexpected"
Evaluates to a quotation with the same structure as <@ "hello" @>.

Example

Calls to this active pattern can be partially applied to pre-evaluate some aspects of the matching. For example:

 open FSharp.Quotations
 open FSharp.Quotations.Patterns
 open FSharp.Quotations.DerivedPatterns

 let (|ConsoleWriteLineOneArg|_|) = (|SpecificCall|_|) <@ Console.WriteLine("1") @>

 let inpExpr = <@ Console.WriteLine("hello") @>

 match inpExpr with
 | ConsoleWriteLineOneArg (None, [], [ argExpr ]) -> argExpr
 | _ -> failwith "unexpected"
Evaluates to a quotation with the same structure as <@ "hello" @>.

(|String|_|) input

Full Usage: (|String|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: string option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant string expressions

input : Expr

The input expression to match against.

Returns: string option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ "a" @> with
 | String v -> v
 | _ -> failwith "unexpected"
Evaluates to "a".

(|UInt16|_|) input

Full Usage: (|UInt16|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: uint16 option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant unsigned int16 expressions

input : Expr

The input expression to match against.

Returns: uint16 option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8us @> with
 | UInt16 v -> v
 | _ -> failwith "unexpected"
Evaluates to 8us.

(|UInt32|_|) input

Full Usage: (|UInt32|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: uint32 option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant unsigned int32 expressions

input : Expr

The input expression to match against.

Returns: uint32 option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8u @> with
 | UInt32 v -> v
 | _ -> failwith "unexpected"
Evaluates to 8u.

(|UInt64|_|) input

Full Usage: (|UInt64|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: uint64 option When successful, the pattern binds the constant value from the input expression

An active pattern to recognize constant unsigned int64 expressions

input : Expr

The input expression to match against.

Returns: uint64 option

When successful, the pattern binds the constant value from the input expression

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ 8UL @> with
 | UInt64 v -> v
 | _ -> failwith "unexpected"
Evaluates to 8UL.

(|Unit|_|) input

Full Usage: (|Unit|_|) input

Parameters:
    input : Expr - The input expression to match against.

Returns: unit option When successful, the pattern does not bind any results

An active pattern to recognize () constant expressions

input : Expr

The input expression to match against.

Returns: unit option

When successful, the pattern does not bind any results

Example

 open FSharp.Quotations.DerivedPatterns

 match <@ () @> with
 | Unit v -> v
 | _ -> failwith "unexpected"
Evaluates to true.