19
Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Embed Size (px)

DESCRIPTION

Goals Use SML.NET frontend to generate large terms in order to evaluate the efficiency of NBE in a non-trivial setting Apply NBE in the actual compiler

Citation preview

Page 1: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Normalisation by Evaluation for SML.NET

Sam LindleyUniversity of Edinburgh

Page 2: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Outline of the talk

• SML.NET• NBE for ml

• Implementing NBE for SML.NET• Other normalisation algorithms• Benchmarks• Conclusions and Future work

Page 3: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Goals

• Use SML.NET frontend to generate large terms in order to evaluate the efficiency of NBE in a non-trivial setting

• Apply NBE in the actual compiler

Page 4: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

SML.NET

• A whole program compiler• Uses the monadic intermediate language

MIL• Does lots of rewriting• Rewriting is currently a bottleneck

Page 5: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

SML.NET compilation

Frontend

Rewriting

Backend

ML

MIL

Optimised MIL

.NET bytecode

Page 6: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Rewriting phasesval opts = ["presimp ", "simp", "funscope", "arity", "simp", "eq", "simp", "mono", "simp", "units", "simp",

"arity", "tailrec", "simp", "inline", "funscope", "simp", "floathoist", "case", "lastsimp"]

Page 7: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Computational metalanguage (ml)

Types, := o | ! | T

Terms (ml)M,N := c | x

| x.M | M@N| hM,Ni | 1(M) | 2(M)

| [M] | let x = M in N

Page 8: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

ml rewrites(-rules)x.M)@N Ã M{N/x}i(hM1,M2i) Ã Mi

let x = [M] in N Ã N{M/x}

(commuting-conversions)let y = (let x = M in N) in P Ã let x = M in let y = N in P

(-expansions)M Ã x.M@xM Ã h1(M),2(M)iM Ã let x = M in [x]

Page 9: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Residualising semantics for ml

« o ¬ = ml

« ! ¬ = « ¬ ! « ¬« T¬ = (« ¬ ! ml) ! ml

« x ¬ = (x)

« x.M ¬ = v.« M ¬[x v]

« M@N ¬ = « M ¬ « N ¬

« [M] ¬ k = k « M ¬

« let x = M in N ¬ k= « M ¬(v.« N ¬[x v]k)

Page 10: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

NBE for ml

# : «¬ ! ml (‘reify’)#o e = e # !

f = x . #(f ("x)) (x “fresh”)

#T c = c (v . [#v])

" : ml ! «¬ (‘reflect’)"o e = e " !

e = v. "(e@(#v))

"T e = k . let x = e in k ("x) (x “fresh”)

norm e = #« e ¬« c ¬ = " c

Page 11: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Some variations

• Suppress expansions• -reduction instead of expansion• shift / reset instead of local continuations• State monad instead of continuations

monad (either local or global)

Page 12: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Changes from ml to MIL

• Church instead of Curry-typing• Distinction between values and

computations• Arity raising• Debugging info• Effect annotations• Extra term constructors: polymorphism,

sums, recursive types, exceptions, references, .NET interoperability, etc.

Page 13: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Adapting NBE for MIL (1)

• Ignore ‘unknown’ subterms• Map unknown values to * and unknown

computations to [*]• Define « * ¬ v = « * ¬, « 1(*) ¬ = « *

¬, etc.• Potentially wipes out a lot of the source

term

Page 14: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Adapting NBE for MIL (2)

• Treat unknown term constructors as uninterpreted constants

• Reflect at the appropriate type• Necessary to include types in the

semantics for -expansion

Page 15: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Spectrum of normalisation algorithms

• Naïve algorithm• Naïve algorithm + environments• Go via wnf using environments + closures• NBE

Page 16: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Benchmarks (ms)

naïve env env/wnf NBE

sort 105 1.24 0.44 0.84hamlet 5388 16.11 7.01 12.91

raytrace 779 4.94 2.18 3.81bootstrap 84817 85.01 67.48 137.19

unknown *

Page 17: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Benchmarks (ms)unknown uninterpreted constant

env env/wnf NBE

sort 151 117 101

hamlet 18105 7850 5407

raytrace 914 590 645

Page 18: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Future work / work in progress

• Polymorphism• Evaluate different approaches to sums

and recursive types• Exceptions• Effects• Target MIL instead of MIL • Incorporate NBE into the actual compiler

Page 19: Normalisation by Evaluation for SML.NET Sam Lindley University of Edinburgh

Conclusions

• NBE is fast• Carefully optimised normalisers can

sometimes be even faster (but start to look very much like NBE)

• NBE is a useful implementation tool• NBE algorithms can be derived by

program translation