(* Delphin source code for SimpleDelphin Chapter * by Adam Poswolsky *) (* Natural Numbers (Example 2.1.1) *) sig %name N n nat>; (* Untyped $\lambda$-expressions using HOAS (Example 2.1.2) *) sig %name E x exp) -> exp> exp -> exp>; (* Untyped $\lambda$-expressions a la de Bruijn (Example 2.1.4) *) (* Variables *) sig %name X variable>; (* Expressions *) sig %name T t term> term -> term> term>; (* Untyped Combinators (Example 2.1.5) *) sig %name C u comb -> comb> ; (* Addition (Example 3.3.3) *) params = , ; fun plus : -> -> = fn => | => let val = plus in end; (* Closed Eager Evaluator (Example 3.3.4 and 5.2.3 *) params = .; fun eval : -> = fn => (case (eval , eval ) of (, ) => eval (* Match Non-Exhaustive Warning: We do not handle *) (* Termination Warning: not smaller than *) ) | => ; (* Open Evaluator (Example 3.3.5) * This is also for Example 5.2.2, but we * use a different world than in the code because we are limited to simply-typed examples * The important part is that the world contains "exp". *) params = , ; fun evalBeta : -> = fn => (case (evalBeta , evalBeta ) of (, ) => evalBeta (* Termination Warning: not smaller than *) | (, ) => ) | => (case ({} evalBeta ) of {} => ) | => ; (* * Sample execution of evalBeta * (Example 3.3.6 and 5.2.4) *) val sample1 = evalBeta ; (* OUTPUT: * val sample1 : * = *) (* Variable Counting (Example 3.3.7 *) params = , ; fun cntvar : -> = fn => plus (cntvar ) (cntvar ) | => (case ( {} cntvar ) of ({} ) => ) | [] => ; (* Sample execution of cntvar (Example 3.3.8) *) val sample2 = cntvar ; (* OUTPUT: * val sample2 : * = *) (* Lemma 3.4.2 is over any type A, $\tau$ and $\sigma$, * but we just show it for one example. *) params = *; fun lemma-part1 : ({} ( -> )) -> (({}) -> {}) = fn u1 => fn u2 => {} (u1 \x) (u2 \x) ; params = *; fun lemma-part2 : (({}) -> {}) -> ({} ( -> )) = fn u1 => fn {} ((E \x) => (u1 E) \x) ; params = *; fun lemma-part3 : ({} ( * )) -> (({}) * {}) = fn {}((u1 \x), (u2 \x)) => (u1, u2) ; params = *; fun lemma-part4 : (({}) * {}) -> ({} ( * )) = fn (u1, u2) => {} ((u1 \x), (u2 \x)); val exampleF : (({}) -> {}) = fn {} => {} | {} <_> => {} ; val lemmaTest = {} ((lemma-part2 exampleF) \x) ; (* OUTPUT: * val exampleF : ({} ) -> ({} ) * = fn ... * val lemmaTest : {} * = {} *) (* Combinators to $\lambda$-expressions (Example 3.7.1) *) params = . ; fun comb2exp : -> = fn => | => | => let val = comb2exp val = comb2exp in end; val id1 : = comb2exp ; (* OUTPUT: * val id1 : * = *) val reducedId1 = evalBeta id1; (* OUTPUT: * val reducedId1 : * = *) (* Bracket Abstraction (Example 3.7.2) *) params = , ; fun ba : comb> -> = fn <[x] x> => | <[x] MP (C1 x) (C2 x)> => let val = ba <[x] C1 x> val = ba <[x] C2 x> in end | <[x] C> => ; (* Untyped $\lambda$-expressions to Combinators (Example 3.7.3) *) type convParamFun = -> params = , ; fun exp2comb : convParamFun -> -> = fn W => fn => (case ({}{} exp2comb (W with => ) ) of ({}{} ) => ba <[u] C u>) | => let val = exp2comb W val = exp2comb W in end | => W ; (* Same as above with desugared "with" (Section 3.7.2) *) params = , ; fun exp2comb : convParamFun -> -> = fn W => fn => (case ({}{} exp2comb ((fn {}{}( => ) | {}{}( => (let val = W in {}{} end) \x \u )) \x \u) ) of ({}{} ) => ba <[u] C u>) | => let val = exp2comb W val = exp2comb W in end | => W ; (* Example Execution of exp2comb (Example 3.7.4) *) val idC = exp2comb (fn .) ; (* = *) (* OUTPUT: * val idC : * = *) (* HOAS to de Bruijn (Example 3.8.1) *) params = , ; fun toDebruijn : ( -> ) -> -> = fn W => fn => let val W' = fn => @ (W ) in case ({} toDebruijn (W' with => ) ) of {}() => end | => let val = toDebruijn W val = toDebruijn W in end | => @ (W ); (* de Bruijn to HOAS (Example 3.8.2) *) params = , ; fun toHOAS : ( -> ) -> -> = fn W => fn => (case ({} toHOAS ((fn {}( => ) | {}( => (let val = W in {} end)\x )) \x) ) of {} => ) | => let val = toHOAS W val = toHOAS W in end | => W ; (* Examples converting between representations (Example 3.8.3) *) val test1 = toDebruijn (fn .) ; (* OUTPUT: * val test1 : * = *) val test2 = toHOAS (fn .) test1; (* Match Non-Exhaustive Warning generated for "fn ." * OUTPUT: * val test2 : * = *)