2 people like it.

Quotation type erasure

An implementation of the inverse of Expr.Cast<_>, with erasure happening at reflection level.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.ExprShape

type Expr with
    static member Erase (e : Expr<'T>) =
        match e with
        | ShapeVar v -> Expr.Var v
        | ShapeLambda (v, body) -> Expr.Lambda(v, body)
        | ShapeCombination (o, exprs) -> RebuildShapeCombination(o, exprs)


Expr.Erase <@ 1 + 1 @> |> fun e -> e.GetType()
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
module ExprShape

from Microsoft.FSharp.Quotations
Multiple items
type Expr =
  override Equals : obj:obj -> bool
  member GetFreeVars : unit -> seq<Var>
  member Substitute : substitution:(Var -> Expr option) -> Expr
  member ToString : full:bool -> string
  member CustomAttributes : Expr list
  member Type : Type
  static member AddressOf : target:Expr -> Expr
  static member AddressSet : target:Expr * value:Expr -> Expr
  static member Application : functionExpr:Expr * argument:Expr -> Expr
  static member Applications : functionExpr:Expr * arguments:Expr list list -> Expr
  ...

Full name: Microsoft.FSharp.Quotations.Expr

--------------------
type Expr<'T> =
  inherit Expr
  member Raw : Expr

Full name: Microsoft.FSharp.Quotations.Expr<_>
static member Expr.Erase : e:Expr<'T> -> Expr

Full name: Script.Erase
val e : Expr<'T>
active recognizer ShapeVar: Expr -> Choice<Var,(Var * Expr),(obj * Expr list)>

Full name: Microsoft.FSharp.Quotations.ExprShape.( |ShapeVar|ShapeLambda|ShapeCombination| )
val v : Var
static member Expr.Var : variable:Var -> Expr
active recognizer ShapeLambda: Expr -> Choice<Var,(Var * Expr),(obj * Expr list)>

Full name: Microsoft.FSharp.Quotations.ExprShape.( |ShapeVar|ShapeLambda|ShapeCombination| )
val body : Expr
static member Expr.Lambda : parameter:Var * body:Expr -> Expr
active recognizer ShapeCombination: Expr -> Choice<Var,(Var * Expr),(obj * Expr list)>

Full name: Microsoft.FSharp.Quotations.ExprShape.( |ShapeVar|ShapeLambda|ShapeCombination| )
val o : obj
val exprs : Expr list
val RebuildShapeCombination : shape:obj * arguments:Expr list -> Expr

Full name: Microsoft.FSharp.Quotations.ExprShape.RebuildShapeCombination
static member Expr.Erase : e:Expr<'T> -> Expr
val e : Expr
System.Object.GetType() : System.Type
Raw view New version

More information

Link:http://fssnip.net/eo
Posted:5 years ago
Author:Eirik Tsarpalis
Tags: expr , upcast