Is there a built-in function for user readable quotes from F #?

When quoting

<@ 1 + 1 @>

I want 1 + 1

instead

"Call (None, Int32 op_Addition[Int32,Int32,Int32](Int32, Int32), [Value (1), Value (1)])"

+5
source share
3 answers

You will have to write it yourself. See the F # quotations visualizer code as a guide for transforming an abstract syntax quote tree.

+6
source

I implemented a quote decompiler as part of a larger open source project, Unquote . It can decompile many simple expressions specified in F # as single-line strings without syntax highlighting (see the main page of the project for a list of decompiler functions). For example,

> decompile <@ (11 + 3) / 2 = String.length ("hello world".Substring(4, 5)) @>;;
val it : string =
  "(11 + 3) / 2 = String.length ("hello world".Substring(4, 5))"

@Kurt Schelfthout , F # Quotations . , , F #. , , Unquote F # :

> decompile <@ match true with | true -> "hi" | _ -> "bye"  @>;;
val it : string =
  "let matchValue = true in if matchValue then "hi" else "bye""

> decompile <@ seq {yield 1; yield 2}  @>;;
val it : string =
  "seq (Seq.delay (fun unitVar -> Seq.append (Seq.singleton 1) (Seq.delay (fun unitVar -> Seq.singleton 2))))"

Infix ( ), , , ( , ). Unquote .

+5

, , . , , . if switch ( , ). - , , .

Then there is a rabbit hole of ambiguities that you have to solve, with conventions such as the pipe operator, launches a new line, allows you to run a new line, indentation, infix, prefix, special cases, such as (: :) operator, etc.

In general, doable, but not trivial. Like decompilation.

0
source

All Articles