Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating F# code

T4 is the "official" code generation engine for C#/VB.NET. But F# doesn't support it (this is from April, but I couldn't find any newer mentions). So what is a good way to generate F# code?

EDIT:

I want to implement 2-3 finger trees in F#. I already have implemented them in C#, so this should be a nice comparison. The "digits" and nodes of the tree can be represented as arrays, so

type 't FingerTree = Empty | Single of 't | Deep of 't array * (('t FingerTree) array) lazy * 't array

However, the maximum size of these arrays is very small, so it'd be nice to have

type 't Digit = Digit1 of 't | Digit2 of 't*'t | Digit3 of 't*'t*'t | Digit4 of 't*'t*'t*'t
type 't Node = Node2 of 't FingerTree * 't FingerTree | Node3 of 't FingerTree * 't FingerTree * 't FingerTree 
type 't FingerTree = Empty | Single of 't | Deep of 't Digit * ('t Node) lazy * 't Digit

to avoid bounds checking, etc.

But then writing all functions on Digit and Node by hand becomes more difficult, and it's better to generate them. And a T4-like approach looks perfect for it...

like image 780
Alexey Romanov Avatar asked Feb 02 '10 08:02

Alexey Romanov


People also ask

What is the generating function formula?

The generating function associated to the class of binary sequences (where the size of a sequence is its length) is A(x) = ∑n≥0 2nxn since there are an = 2n binary sequences of size n. (k n ) xn = (1 + x)k.

What is meant by generating function?

In mathematics, a generating function is a way of encoding an infinite sequence of numbers (an) by treating them as the coefficients of a formal power series. This series is called the generating function of the sequence.

What is generating function in statistics?

A generating function of a real-valued random variable is an expected value of a certain transformation of the random variable involving another (deterministic) variable.


1 Answers

Because F# doesn't support custom tools in solution explorer, you can place your T4 files in a C# or Visual Basic project and redirect their output to your F# project. Here is how you can do it with T4 Toolbox:

<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#
    FSharpTemplate template = new FSharpTemplate();
    template.Output.Project = @"..\Library1\Library1.fsproj";
    template.Output.File = "Module2.fs";
    template.Render();
#>
<#+
class FSharpTemplate: Template
{
    public override string TransformText()
    {
#>
// Learn more about F# at http://fsharp.net

module Module2
<#+
        return this.GenerationEnvironment.ToString();
    }
}

#>
like image 181
Oleg Sych Avatar answered Sep 27 '22 21:09

Oleg Sych