Search code examples
f#pattern-matchingactive-patternf#+

Active Pattern on FSharpPlus trySscanf


I am trying to create an active pattern Scan around FSharpPlus's trySscanf, so that the following works:

let res = // res = 42
  match "Hello 42 World" with
  | Scan "Hello %i World" n -> n

The way I understand incomplete active patterns to work, I simply need to return an option, trySscanf already returns an option so I tried the following:

let (|Scan|_|) = trySscanf

When that didn't work I tried a more explicit

let (|Scan|_|) pattern input = trySscanf pattern input

They both fail with the following compilation error:

Type constraint mismatch when applying the default type 'obj' for a type inference variable. No overloads match for method 'TryParseArray'.
Known return type: (string [] -> obj option)
Known type parameters: < obj , Internals.TryParseArray >
Available overloads:
 - static member Internals.TryParseArray.TryParseArray :  ^t * obj -> (string [] ->  ^t option) when (Control.TryParse or  ^t) : (static member TryParse :  ^t * Control.TryParse -> (string ->  ^t option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : ('t1 * 't2' * 't3 * 't4 * 't5 * 't6 * 't7) * Internals.TryParseArray -> (string [] -> ( ^a7 *  ^a8 *  ^a9 *  ^a10 *  ^a11 *  ^a12 *  ^a13) option) when (Control.TryParse or  ^a7) : (static member TryParse :  ^a7 * Control.TryParse -> (string ->  ^a7 option)) and (Control.TryParse or  ^a8) : (static member TryParse :  ^a8 * Control.TryParse -> (string ->  ^a8 option)) and (Control.TryParse or  ^a9) : (static member TryParse :  ^a9 * Control.TryParse -> (string ->  ^a9 option)) and (Control.TryParse or  ^a10) : (static member TryParse :  ^a10 * Control.TryParse -> (string ->  ^a10 option)) and (Control.TryParse or  ^a11) : (static member TryParse :  ^a11 * Control.TryParse -> (string ->  ^a11 option)) and (Control.TryParse or  ^a12) : (static member TryParse :  ^a12 * Control.TryParse -> (string ->  ^a12 option)) and (Control.TryParse or  ^a13) : (static member TryParse :  ^a13 * Control.TryParse -> (string ->  ^a13 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : ('t1 * 't2' * 't3 * 't4 * 't5 * 't6) * Internals.TryParseArray -> (string [] -> ( ^a6 *  ^a7 *  ^a8 *  ^a9 *  ^a10 *  ^a11) option) when (Control.TryParse or  ^a6) : (static member TryParse :  ^a6 * Control.TryParse -> (string ->  ^a6 option)) and (Control.TryParse or  ^a7) : (static member TryParse :  ^a7 * Control.TryParse -> (string ->  ^a7 option)) and (Control.TryParse or  ^a8) : (static member TryParse :  ^a8 * Control.TryParse -> (string ->  ^a8 option)) and (Control.TryParse or  ^a9) : (static member TryParse :  ^a9 * Control.TryParse -> (string ->  ^a9 option)) and (Control.TryParse or  ^a10) : (static member TryParse :  ^a10 * Control.TryParse -> (string ->  ^a10 option)) and (Control.TryParse or  ^a11) : (static member TryParse :  ^a11 * Control.TryParse -> (string ->  ^a11 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : ('t1 * 't2' * 't3 * 't4 * 't5) * Internals.TryParseArray -> (string [] -> ( ^a5 *  ^a6 *  ^a7 *  ^a8 *  ^a9) option) when (Control.TryParse or  ^a5) : (static member TryParse :  ^a5 * Control.TryParse -> (string ->  ^a5 option)) and (Control.TryParse or  ^a6) : (static member TryParse :  ^a6 * Control.TryParse -> (string ->  ^a6 option)) and (Control.TryParse or  ^a7) : (static member TryParse :  ^a7 * Control.TryParse -> (string ->  ^a7 option)) and (Control.TryParse or  ^a8) : (static member TryParse :  ^a8 * Control.TryParse -> (string ->  ^a8 option)) and (Control.TryParse or  ^a9) : (static member TryParse :  ^a9 * Control.TryParse -> (string ->  ^a9 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : ('t1 * 't2' * 't3 * 't4) * Internals.TryParseArray -> (string [] -> ( ^a4 *  ^a5 *  ^a6 *  ^a7) option) when (Control.TryParse or  ^a4) : (static member TryParse :  ^a4 * Control.TryParse -> (string ->  ^a4 option)) and (Control.TryParse or  ^a5) : (static member TryParse :  ^a5 * Control.TryParse -> (string ->  ^a5 option)) and (Control.TryParse or  ^a6) : (static member TryParse :  ^a6 * Control.TryParse -> (string ->  ^a6 option)) and (Control.TryParse or  ^a7) : (static member TryParse :  ^a7 * Control.TryParse -> (string ->  ^a7 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : ('t1 * 't2' * 't3) * Internals.TryParseArray -> (string [] -> ( ^a3 *  ^a4 *  ^a5) option) when (Control.TryParse or  ^a3) : (static member TryParse :  ^a3 * Control.TryParse -> (string ->  ^a3 option)) and (Control.TryParse or  ^a4) : (static member TryParse :  ^a4 * Control.TryParse -> (string ->  ^a4 option)) and (Control.TryParse or  ^a5) : (static member TryParse :  ^a5 * Control.TryParse -> (string ->  ^a5 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : ('t1 * 't2) * Internals.TryParseArray -> (string [] -> ( ^a2 *  ^a3) option) when (Control.TryParse or  ^a2) : (static member TryParse :  ^a2 * Control.TryParse -> (string ->  ^a2 option)) and (Control.TryParse or  ^a3) : (static member TryParse :  ^a3 * Control.TryParse -> (string ->  ^a3 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : Internals.Id<'t1> * Internals.TryParseArray -> (string [] -> Internals.Id< ^a1> option) when (Control.TryParse or  ^a1) : (static member TryParse :  ^a1 * Control.TryParse -> (string ->  ^a1 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : Tuple< ^t1> * Internals.TryParseArray -> (string [] -> Tuple< ^t1> option) when (Control.TryParse or  ^t1) : (static member TryParse :  ^t1 * Control.TryParse -> (string ->  ^t1 option)) // Argument at index 1 doesn't match
 - static member Internals.TryParseArray.TryParseArray : t: ^t * Internals.TryParseArray -> (string [] ->  ^t option) when  ^t : (member get_Item1 :  ^t ->  ^t1) and  ^t : (member get_Item2 :  ^t ->  ^t2) and  ^t : (member get_Item3 :  ^t ->  ^t3) and  ^t : (member get_Item4 :  ^t ->  ^t4) and  ^t : (member get_Item5 :  ^t ->  ^t5) and  ^t : (member get_Item6 :  ^t ->  ^t6) and  ^t : (member get_Item7 :  ^t ->  ^t7) and  ^t : (member get_Rest :  ^t ->  ^tr) and (Control.TryParse or  ^t1) : (static member TryParse :  ^t1 * Control.TryParse -> (string ->  ^t1 option)) and (Control.TryParse or  ^t2) : (static member TryParse :  ^t2 * Control.TryParse -> (string ->  ^t2 option)) and (Control.TryParse or  ^t3) : (static member TryParse :  ^t3 * Control.TryParse -> (string ->  ^t3 option)) and (Control.TryParse or  ^t4) : (static member TryParse :  ^t4 * Control.TryParse -> (string ->  ^t4 option)) and (Control.TryParse or  ^t5) : (static member TryParse :  ^t5 * Control.TryParse -> (string ->  ^t5 option)) and (Control.TryParse or  ^t6) : (static member TryParse :  ^t6 * Control.TryParse -> (string ->  ^t6 option)) and (Control.TryParse or  ^t7) : (static member TryParse :  ^t7 * Control.TryParse -> (string ->  ^t7 option)) and (Internals.TryParseArray or  ^tr) : (static member TryParseArray :  ^tr * Internals.TryParseArray -> (string [] ->  ^tr option)) // Argument 't' doesn't match
 - static member Internals.TryParseArray.TryParseArray : unit * Internals.TryParseArray -> (string [] -> unit) // Argument at index 1 doesn't match Consider adding further type constraintsF# Compiler(71)

Clearly, trySscanf has a bunch of overloads which I'm not taking into account, and I'm not sure how to do that.

Is what I'm trying to do even possible?


Interestingly, if I add the match expression, the compilation error disappears, however I may only use the PrintfFormat of the first match case I try:

let parseLine line =
    match line with
    | Scan "mem[%i] = %i" (address, value) -> Op address value
    | Scan "mask = %s" str -> Mask str

Last line signals an error:

This expression was expected to have type
    'int * int'    
but here has type
    'string'    (F# Compiler(1))

Solution

  • You just forgot to declare your function inline, do that and it will work just fine:

    #r "nuget: FSharpPlus"
    
    open FSharpPlus
    
    let inline (|Scan|_|) pattern input = trySscanf pattern input
    
    
    let res48 =
      match "Hello 42 6 World" with
      | Scan "Hello %i World" n -> n
      | Scan "Hello %i %i World" (n1, n2) -> n1 + n2
    

    The reason why you have declare it inline is because functions with constraints can only be declared inline, it's the same case as if you want to write a function that doubles the numeric input for every numeric type.