I am writing a compiler for a simple subset of standard ML (just for fun) and I am stuck on how to represent the core language (I should mention that I write it in Haskell probably).
Basically what I have already is an expression type that looks like the following:
data Expr' e
= Let Name e e
| Fn Name e
| Val Value
| Var Name
| App e e
| If e e e
deriving (Show, Eq, Functor)
newtype Mu f = In { out :: f (Mu f) }
deriving instance Eq (f (Mu f)) => Eq (Mu f)
deriving instance Show (f (Mu f)) => Show (Mu f)
type Expr = Mu Expr'
type LocExpr = Cofree Expr' SourcePos
(I use Cofree to conveniently annotate the terms with source code position and other information). With this core language I can already transpile simple programs to Scheme (or other langauges), it kind of is like a dynamic programming language at the moment (probably not treating recursion properly at the moment, but it seems to work).
Now I want to support tuples as well, because I want to catch up with Kennedy's compiling with continuations, continued (https://www.microsoft.com/en-us/research/publication/compiling-with-continuations-continued/). If I understand the paper right, for his example of the untyped ML subset that he implements, he just uses pairs.
I guess however, that I could just add a term Tuple [e]
and indexing terms/case terms (where if I read Kennedy 2007 right, he just implements Tuple e e
) to the core language to represent a tuple. Unfortunately, I don't find much about what would be a good way to represent tuples there. I am just a hobbyist, so I might just not look in the right places for that information.
Another resource where I stumbled over a core language was Turon's thesis https://www.mpi-sws.org/~turon/turon-thesis.pdf which is obviously not exactly about SML, but there he also only implements pairs (which makes me think whether tuples might be implemented as (a, (b, unit))
or something.
Tuple [e]
representation? $\endgroup$Tuple [e]
doesn't seem much overhead overPair e e
, so I thought there could be something to it. $\endgroup$