Search code examples
f#quotations

Is it possible to differentiate between typed and untyped nested quotations?


For example, given <@ let x = <@ 1 @> in x @> and <@ let x = <@@ 1 @@> in x @>, I can match both with Patterns.Let(_, (Patterns.Quote(_) as q), _) -> q but I can't differentiate between the typed and untyped q.


Solution

  • Interesting. It seems that quotations are always stored in the typed form.

    The type of the <@@ 1 @@> sub-expression inside the quotation is always Expr<int>. However, the type of the variable x differs in your two quotations:

    match q1 with
    | Patterns.Let(v, (Patterns.Quote(_) as q), _) when v.Type = typeof<Expr> -> "untyped"
    | Patterns.Let(_, (Patterns.Quote(_) as q), _) -> "typed"
    | _ -> "other"
    

    But I'm not sure how to use this to differentiate between the two cases in general. It seems that you can only do that if you look at the context (and there are just too many possible context...)