CapturedStagedFuncCallTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule d-exp-apprule d-exp-lambdarule d-app-closure
Translated into Lean for the mechanisation of the book: see its Lean translation.
A function parameter captured by a local helper and applied to two arguments works when the function passed in takes one argument and returns a function. `makeAdder` is defined as `String -> (Int -> Int)`, returning a lambda. Passed to `applyBoth` as `f : String -> Int -> Int` and called as `f a b` inside a local function, it must receive both arguments, giving the same 15 as the direct call.
Results
elm-0.19.1
passran in 76 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. direct: 15 viaCapture: 15
elm-0.19.2
passran in 37 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. direct: 15 viaCapture: 15
elm-0.19.3
passran in 67 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. direct: 15 viaCapture: 15
lamdera-1.5.0
passran in 67 msOutput
direct: 15 viaCapture: 15
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/CapturedStagedFuncCallTest] in its title.
Source
The -- CHECK: lines give the expected output.
module CapturedStagedFuncCallTest exposing (main)
{-| A function parameter captured by a local helper and applied to two arguments works when the function passed in takes one argument and returns a function.
`makeAdder` is defined as `String -> (Int -> Int)`, returning a lambda. Passed to
`applyBoth` as `f : String -> Int -> Int` and called as `f a b` inside a local
function, it must receive both arguments, giving the same 15 as the direct
call.
-}
-- TAGS: elm-spec, d-exp-app, d-exp-lambda, d-app-closure
-- CHECK: direct: 15
-- CHECK: viaCapture: 15
import Html exposing (text)
{-| Takes one argument and returns a function adding 5 to its input when the key is `"hello"`, and 0 otherwise. -}
makeAdder : String -> (Int -> Int)
makeAdder key =
\value ->
(if key == "hello" then
5
else
0
)
+ value
{-| Applies `f` to `a` and `b` from inside a local function.
-}
applyBoth : (String -> Int -> Int) -> String -> Int -> Int
applyBoth f a b =
let
go () =
f a b
in
go ()
main =
let
_ =
Debug.log "direct" (makeAdder "hello" 10)
_ =
Debug.log "viaCapture" (applyBoth makeAdder "hello" 10)
in
text "done"
Lean translation
This program is translated into Lean by the translator of the Lean mechanisation of A Standard Definition of Elm?: the program it is, its modules in the reduced syntax of Chapter 7 with every integer constant at Int or Float by number specialisation, and its CHECK lines as written (g_elm_CapturedStagedFuncCallTest, in StandardElmMech/Generated/Elm_As.lean). Lean's evaluator runs the program by the rules of Chapters 7 and 9, and its checks become one generated theorem: some derivation of the program has a log that meets every check, or, for a test that stops at Debug.todo, the program has no meaning.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
direct | 15 | 15 | t_elm_CapturedStagedFuncCallTest |
viaCapture | 15 | 15 | t_elm_CapturedStagedFuncCallTest |
/-- tests/elm/src/CapturedStagedFuncCallTest.elm -/
def g_elm_CapturedStagedFuncCallTest : Program := {
name := "CapturedStagedFuncCallTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [],
aliases := [],
defs := [
.fn "makeAdder" [pv "key"] (lams [pv "value"] (bin "+" (.atexp (.paren (ifte (bin "==" (v "key") (str "hello")) (i 5) (i 0)))) (v "value"))),
.fn "applyBoth" [pv "f", pv "a", pv "b"] (letDefs [.fn "go" [.unit] (ap (v "f") [v "a", v "b"])] (ap (v "go") [.atexp .unit])),
.fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "direct", .atexp (.paren (ap (v "makeAdder") [str "hello", i 10]))]), .pat pw (ap (qv "Debug" "log") [str "viaCapture", .atexp (.paren (ap (v "applyBoth") [v "makeAdder", str "hello", i 10]))])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "direct: 15" .int,
chk .check "viaCapture: 15" .int ] }Reading the translation: v "x" is the variable x, c "K" the constructor K, i 42 the integer 42 at Int, .scon (.intF 2) the integer 2 at Float, and str "s" a string; ap f [a, b] applies f to a and then b, and bin "+" a b is a + b. lams, caseOf, ifte, listE, tup2 and recE build lambdas, case, if, lists, pairs and records by the book's derived forms; pv, pw and pcon are variable, wildcard and constructor patterns. defs are the program's top-level definitions and locals the other declarations of main's let, placed in a let around each check; ckk label value kind exp is one check. A name with $ (incF$Float) is a copy number specialisation made.
Static semantics (Chapters 4, 5 and 9): legal_elm_CapturedStagedFuncCallTest states that the program as written is legal, with no hypothesis: the proved inference W gives each module a principal derivation, which meets the syntactic restrictions and the further restrictions, with a type of main Chapter 9 allows. End to end: spec_elm_CapturedStagedFuncCallTest states that Lean computes the reduced program above from the source (derived forms, inference, the proved checker, number specialisation, type erasure), so the theorems about it do not trust the translator's elaboration ([elab]); mean_elm_CapturedStagedFuncCallTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_CapturedStagedFuncCallTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.