Hiatus (agh) wrote,

  • Music:


Sunday, Chris and I finished our minijava compiler implementation in F#.  Whereas most of the class implemented it in Java or C#, we knew that all we needed was a functional language with lex and yacc companions, and a great runtime library.

The compiler converted java to x86 assembly code, and we did all of the implementation in F# (and lex and yacc with F# semantic actions) and then embedded ml.exe (MASM compiler) and link.exe (C++ linker) as resources in our binary to do the assembling and linking with the C runtime.

We had discriminated unions galore, and pattern matches to go with.  Perfect for a compiler, where there are a few types, but lots of operations you want to do on those types.  The Java folks were all using the Visitor pattern.  Also, once we got the AST converted to a list representaiton, we could use ML's excellent list library, especially map, iter, and fold_left and fold_right.

I also made some pretty sweet uses of curried functions.

Chris is on the F# team but didn't have the FP background that I have, so he tended to use mutable state and objects more than I did.  But he also knew a lot more about the language.  This was great because I learned a lot more than I would have without him, even if it lead to style inconsistencies.

One cool discovery of Chris's was the pass-forward operator, |>, which allows you to chain producers and consumers like you would do piping in a shell.  If this is in CAML, I never knew about it.
For example,
let outputMethodListing methodInstrs = methodInstrs |> List.map (fun (instr:Instruction) -> instr.ToString()) |> List.iter outputFile.Write

The expression to the left of the pass-forward operator is the last argument to the expression to its right.  It associates left, just like the | in the UNIX or windows shells.

F# is a convergence of OO and Functional, the F# runtime library and the .NET runtime library, type inference and generics.  So there are a bunch of ways to do anything, which is probably not a good thing.  I definitely don't feel facile with making implementation choices in F#.  

The upshot is that I appreciate now just how much I am an expert in C++, in that I know the various styles and their tradeoffs, and can make a series of choices that fit together holistically.

I'm using F# as my scripting language in my day job.  It's annoying to wait a second for the runtime to load everytime I run a script, but it's worth it because I like F# way better than perl.
Tags: code, f#

  • Windows 7

    Windows 7 is coming out in beta this weekend. It's a no-brainer upgrade from Vista, as it is, for my purposes, a strictly superior OS. And if you're…

  • Pro and Repro were sittin' in a boat... Pro fell out, who was left?

    So, I'm debugging this issue with the tooltips in the Windows common controls. A particular debugger is crashing when it's trying to display the…

  • Integrated experiences

    I had a very well-integrated day doing my compilers project and homework. I began by using F#'s fslex.exe and the F# compiler in Visual Studio to…

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.