MLPolyR supports extensible programming, which provide a solution to the expression problem [1].
Each component in a reference architecture can be mapped to a MLPolyR program unit (called module). As functors in SML, MLPolyR provides parameterized mechanisms called templates which takes concrete modules as arguments and instantiates a composite module as in the following code. For example, the template InterpFun takes two components Checker and Evaluator and instantiates a corresponding interpreter.
Template InterpFun requires (C, E) = {
val interp = E.eval o C.check
}
template InterpOptFun requires (C, O, E) = {
val interp = E.eval o O.opt o C.check
}
Then, we implement components in terms of modules. For example, the module Checker implements the component Checker in the following code.
module Checker = {
fun bases (check, env) =
cases `VAR x => env x
| `NUM n => ()
| `PLUS (e1, e2) =>
(check (env, e1); check (env, e2))
| `LET (x, e1, e2) =>
(check (env, e1);
check (Envt.bind ((), x, env), e2))
fun check e =
let fun run (env, e) =
match e with bases (run, env)
in (run (Envt.empty, e); e)
end
}
As the language grows, we need to define the module EChecker which add static semantics for If0. Differently from the SML module system, this extension does not add any redundant code since the MLPolyR module system is based on the extensible polymorphic records.
module EChecker = {
fun bases (check, env) =
cases `IF0 (e1, e2, e3) =>
(check (env, e1);
check (env, e2);
check (env, e3))
default: Checker.bases (check, env)
fun check e =
let fun run (env, e) =
match e with bases (run, env)
in (run (Envt.empty, e); e)
end
}
Similarly to that of SML, we can instantiation of all four interpreters by importing the proper components. For example, an optimized extended interpreter I’opt can be obtained by importing EChecker, EOptimizer and EBigStep to the template InterpOptFun.
module I = InterpFun imports (Checker, BigStep) module Im = InterpFun imports (Checker, Machine) module Iopt = InterpOptFun imports (Checker, Optimizer, BigStep) module I’opt = InterpOptFun imports (EChecker, EOptimizer, EBigStep)
Reference
[1] MLPolyR Project
Go to home
