3 ms·
A few years ago, I wrote a compiler for FRACTRAN written in Haskell[0] (translated from Common Lisp) with an associated blog post[1]. Using the tagless-final st
by siraben 3y ago
A few years ago, I wrote a compiler for FRACTRAN written in Haskell[0] (translated from Common Lisp) with an associated blog post[1]. Using the tagless-final style you can write a simple program like this:
sumTo :: FracComp repr => Integer -> [repr [Rational]]
sumTo n = [addi "c" 0, addi "n" n, while (jge "n" 0) [adds "c" "n", subi "n" 1]]
And the compiler would output this:
λ> runAssembler (sumTo 10) -- Program length: 31
Right [847425747 % 2,13 % 3,19 % 13,11 % 3,11 % 29,31 % 11,41 % 31,23
% 11,23 % 47,2279 % 23,59 % 301,59 % 41,67 % 413,329 % 67,61 % 59,73 %
61,83 % 73,71 % 61,71 % 97,445 % 71,707 % 89,103 % 5353,103 % 83,109 %
5459,5141 % 109,107 % 103,113 % 749,113 % 19,131 % 113,29 % 131,127 %
113]
λ> runAsm (sumTo 10)
[(Prime 97,55),(Prime 107,1)]
[0] https://github.com/siraben/hasktran https://github.com/siraben/hasktran
[1] https://siraben.dev/2020/02/26/translating_cl.html https://siraben.dev/2020/02/26/translating_cl.html