Another limitation of F# quotations?

638 views Asked by At

Earlier today I encountered a limitation of F# quotations, and asked a question about it here: F# quotations: variable may escape scope

Now, I may have encountered another limitation when converting examples appearing in http://www.cs.rice.edu/~taha/publications/journal/dspg04a.pdf from MetaOcaml to F#.

This time I've this MetaOcaml snippet:

let rec peval2 p env fenv=
    match p with
    Program ([],e) -> eval2 e env fenv
    | Program (Declaration (s1,s2,e1)::tl,e) ->
         .<let rec f x = .~(eval2 e1 (ext env s2 .<x>.)
                                     (ext fenv s1 .<f>.))
           in .~(peval2 (Program(tl,e)) env (ext fenv s1 .<f>.))>.

and I converted it to

let rec peval2 p env fenv =
    match p with
    | Program ([], e) -> eval2 e env fenv
    | Program (Declaration (s1, s2, e1) :: tl, e) ->
        <@ let rec f x = %(eval2 e1 (ext env s2 <@ x @>)
                                    (ext fenv s1 <@ f @>))
           in %(peval2 (Program(tl, e)) env (ext fenv s1 <@ f @>)) @>

I get the following compile-time error: This expression was expected to have type int -> Expr<int> but here has type Expr<'a> with the two <@ f @>.

Intuitively, I think the error makes a lot of sense. But is there a way in F# to describe what I want in this case?

Code sample:

open Microsoft.FSharp.Quotations

type Exp =
    | Int of int
    | Var of string
    | App of string * Exp
    | Add of Exp * Exp
    | Sub of Exp * Exp
    | Mul of Exp * Exp
    | Div of Exp * Exp
    | Ifz of Exp * Exp * Exp

type Def = Declaration of string * string * Exp
type Prog = Program of Def list * Exp

exception Yikes

let env0 = fun x -> raise Yikes

let fenv0 = env0

let ext env x v = fun y -> if x = y then v else env y

let rec eval2 e env fenv =
    match e with
    | Int i -> <@ i @>
    | Var s -> env s
    | App (s, e2) -> <@ %(fenv s) %(eval2 e2 env fenv) @>
    | Add (e1, e2) -> <@ %(eval2 e1 env fenv) + %(eval2 e2 env fenv) @>
    | Sub (e1, e2) -> <@ %(eval2 e1 env fenv) - %(eval2 e2 env fenv) @>
    | Mul (e1, e2) -> <@ %(eval2 e1 env fenv) * %(eval2 e2 env fenv) @>
    | Div (e1, e2) -> <@ %(eval2 e1 env fenv) / %(eval2 e2 env fenv) @>
    | Ifz (e1, e2, e3) -> <@ if %(eval2 e1 env fenv) = 0
                             then %(eval2 e2 env fenv)
                             else %(eval2 e3 env fenv) @>

let rec peval2 p env fenv =
    match p with
    | Program ([], e) -> eval2 e env fenv
    | Program (Declaration (s1, s2, e1) :: tl, e) ->
        <@ let rec f x = %(eval2 e1 (ext env s2 <@ x @>)
                                    (ext fenv s1 <@ f @>))
           in %(peval2 (Program(tl, e)) env (ext fenv s1 <@ f @>)) @>
1

There are 1 answers

1
Tomas Petricek On BEST ANSWER

I think you're hitting the same problem as in the previous question - when I copied the necessary declarations from the paper, I got:

error FS0446: The variable 'f' is bound in a quotation but is used as part of a spliced expression. This is not permitted since it may escape its scope.

This makes sense - capturing variables bound in quotation inside spliced expression is not allowed in F# and this is definitely done in the code snippet.

I'm not exactly sure why you're getting a different error messagae - if you can post a minimal complete sample, than that can be answered, but you'll still going to hit this variable capture limitation. (Which is probably something that's used quite a lot in MetaOCaml).