Elm Exactly
Results / elm /

FunctionValueChoiceTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule dm-topdec-valrule d-exp-lambdarule d-valbind-patrule d-app-closure

Translated into Lean for the mechanisation of the book: see its Lean translation.

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
Top-level values defined by an `if` that picks between two lambdas can be both called directly and passed to a local `foldl`, and a function returning a tuple from an `if` can be destructured.

`chooseIf` and `chooseElse` have no parameters of their own: each is an `if`
on `sumTo`, choosing multiplication or subtraction. `chooseIf` picks
multiplication, so `chooseIf 3 4` is 12 and `foldl chooseIf 1 [ 1, 2, 3 ]`
is 6. `chooseElse` picks subtraction, so `chooseElse 3 4` is -1 and
`foldl chooseElse 0 [ 1, 2, 3 ]` is 2 (`foldl` passes the element first,
then the accumulator).

`chooseC` returns its last two `Int` arguments as a pair, swapped when its first
argument is not positive. The test destructures it in a `let` along the `then`
branch (3 + 4 = 7) and the `else` branch (10 + 7 = 17), and logs the swapped
pair `(4,3)` directly.

Results

elm-0.19.1

passran in 49 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
chooseIf called: 12
chooseIf folded: 6
chooseElse called: -1
chooseElse folded: 2
chooseC then: 7
chooseC else: (4,3)
chooseC sum: 17

elm-0.19.2

passran in 50 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
chooseIf called: 12
chooseIf folded: 6
chooseElse called: -1
chooseElse folded: 2
chooseC then: 7
chooseC else: (4,3)
chooseC sum: 17

elm-0.19.3

passran in 86 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
chooseIf called: 12
chooseIf folded: 6
chooseElse called: -1
chooseElse folded: 2
chooseC else: (4,3)
chooseC sum: 17
chooseC then: 7

lamdera-1.5.0

passran in 50 ms
Output
chooseIf called: 12
chooseIf folded: 6
chooseElse called: -1
chooseElse folded: 2
chooseC else: (4,3)
chooseC sum: 17
chooseC then: 7

Discussions

Discuss this test·Search the discussions on GitHub

Discussions are on GitHub; one about this test has [t:elm/FunctionValueChoiceTest] in its title.

Source

The -- CHECK: lines give the expected output.

module FunctionValueChoiceTest exposing (main)
{-| Top-level values defined by an `if` that picks between two lambdas can be both called directly and passed to a local `foldl`, and a function returning a tuple from an `if` can be destructured.
`chooseIf` and `chooseElse` have no parameters of their own: each is an `if`
on `sumTo`, choosing multiplication or subtraction. `chooseIf` picks
multiplication, so `chooseIf 3 4` is 12 and `foldl chooseIf 1 [ 1, 2, 3 ]`
is 6. `chooseElse` picks subtraction, so `chooseElse 3 4` is -1 and
`foldl chooseElse 0 [ 1, 2, 3 ]` is 2 (`foldl` passes the element first,
then the accumulator).
`chooseC` returns its last two `Int` arguments as a pair, swapped when its first
argument is not positive. The test destructures it in a `let` along the `then`
branch (3 + 4 = 7) and the `else` branch (10 + 7 = 17), and logs the swapped
pair `(4,3)` directly.
-}
-- TAGS: elm-spec, dm-topdec-val, d-exp-lambda, d-valbind-pat, d-app-closure
-- CHECK: chooseIf called: 12
-- CHECK: chooseIf folded: 6
-- CHECK: chooseElse called: -1
-- CHECK: chooseElse folded: 2
-- CHECK: chooseC then: 7
-- CHECK: chooseC else: (4,3)
-- CHECK: chooseC sum: 17
import Html exposing (text)
sumTo : Int -> Int
sumTo n =
if n <= 0 then
0
else
n + sumTo (n - 1)
chooseIf : Int -> Int -> Int
chooseIf =
if sumTo 10 > 20 then
\a b -> a * b
else
\a b -> a - b
chooseElse : Int -> Int -> Int
chooseElse =
if sumTo 3 > 20 then
\a b -> a * b
else
\a b -> a - b
foldl : (a -> b -> b) -> b -> List a -> b
foldl f acc xs =
case xs of
[] ->
acc
x :: rest ->
foldl f (f x acc) rest
chooseC : Int -> Int -> Int -> ( Int, Int )
chooseC n f g =
if n > 0 then
( f, g )
else
( g, f )
main =
let
_ =
Debug.log "chooseIf called" (chooseIf 3 4)
_ =
Debug.log "chooseIf folded" (foldl chooseIf 1 [ 1, 2, 3 ])
_ =
Debug.log "chooseElse called" (chooseElse 3 4)
_ =
Debug.log "chooseElse folded" (foldl chooseElse 0 [ 1, 2, 3 ])
( p, q ) =
chooseC 1 3 4
_ =
Debug.log "chooseC then" (p + q)
_ =
Debug.log "chooseC else" (chooseC 0 3 4)
( r, t ) =
chooseC -1 7 10
_ =
Debug.log "chooseC sum" (r + t)
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_FunctionValueChoiceTest, in StandardElmMech/Generated/Elm_Fli.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.

CheckExpected (CHECK)Lean evaluatorProved by
chooseIf called1212t_elm_FunctionValueChoiceTest
chooseIf folded66t_elm_FunctionValueChoiceTest
chooseElse called-1-1t_elm_FunctionValueChoiceTest
chooseElse folded22t_elm_FunctionValueChoiceTest
chooseC then77t_elm_FunctionValueChoiceTest
chooseC else(4,3)(4,3)t_elm_FunctionValueChoiceTest
chooseC sum1717t_elm_FunctionValueChoiceTest
/-- tests/elm/src/FunctionValueChoiceTest.elm -/
def g_elm_FunctionValueChoiceTest : Program := {
  name := "FunctionValueChoiceTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "sumTo" [pv "n"] (ifte (bin "<=" (v "n") (i 0)) (i 0) (bin "+" (v "n") (ap (v "sumTo") [.atexp (.paren (bin "-" (v "n") (i 1)))]))),
    .fn "chooseIf" [] (ifte (bin ">" (ap (v "sumTo") [i 10]) (i 20)) (lams [pv "a", pv "b"] (bin "*" (v "a") (v "b"))) (lams [pv "a", pv "b"] (bin "-" (v "a") (v "b")))),
    .fn "chooseElse" [] (ifte (bin ">" (ap (v "sumTo") [i 3]) (i 20)) (lams [pv "a", pv "b"] (bin "*" (v "a") (v "b"))) (lams [pv "a", pv "b"] (bin "-" (v "a") (v "b")))),
    .fn "foldl" [pv "f", pv "acc", pv "xs"] (caseOf (v "xs") [(P (listP []), v "acc"), (.cons (P (pv "x")) (P (pv "rest")), ap (v "foldl") [v "f", .atexp (.paren (ap (v "f") [v "x", v "acc"])), v "rest"])]),
    .fn "chooseC" [pv "n", pv "f", pv "g"] (ifte (bin ">" (v "n") (i 0)) (tup2 (v "f") (v "g")) (tup2 (v "g") (v "f"))),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "chooseIf called", .atexp (.paren (ap (v "chooseIf") [i 3, i 4]))]), .pat pw (ap (qv "Debug" "log") [str "chooseIf folded", .atexp (.paren (ap (v "foldl") [v "chooseIf", i 1, listE [i 1, i 2, i 3]]))]), .pat pw (ap (qv "Debug" "log") [str "chooseElse called", .atexp (.paren (ap (v "chooseElse") [i 3, i 4]))]), .pat pw (ap (qv "Debug" "log") [str "chooseElse folded", .atexp (.paren (ap (v "foldl") [v "chooseElse", i 0, listE [i 1, i 2, i 3]]))]), .pat (ptup2 (P (pv "p")) (P (pv "q"))) (ap (v "chooseC") [i 1, i 3, i 4]), .pat pw (ap (qv "Debug" "log") [str "chooseC then", .atexp (.paren (bin "+" (v "p") (v "q")))]), .pat pw (ap (qv "Debug" "log") [str "chooseC else", .atexp (.paren (ap (v "chooseC") [i 0, i 3, i 4]))]), .pat (ptup2 (P (pv "r")) (P (pv "t"))) (ap (v "chooseC") [.atexp (neg (A (i 1))), i 7, i 10]), .pat pw (ap (qv "Debug" "log") [str "chooseC sum", .atexp (.paren (bin "+" (v "r") (v "t")))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "chooseIf called: 12" .int,
    chk .check "chooseIf folded: 6" .int,
    chk .check "chooseElse called: -1" .int,
    chk .check "chooseElse folded: 2" .int,
    chk .check "chooseC then: 7" .int,
    chk .check "chooseC else: (4,3)" .data,
    chk .check "chooseC sum: 17" .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_FunctionValueChoiceTest 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_FunctionValueChoiceTest 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_FunctionValueChoiceTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_FunctionValueChoiceTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.