Elm Exactly
Results / elm /

FunctionValuesChosenByIfCaseAndTupleTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-exp-caserule d-atpat-sconrule 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
Functions chosen at run time by `if`, `case` and a returned tuple are called correctly.

`chooseA` picks one of two function arguments through an `if`/`else if`
chain, one branch returning it through a `let`. `chooseB` returns its function
argument from a `case`, directly, through `identity`, through a `let`, or
wrapped in a lambda that adds one. `chooseC` returns two functions as a pair,
swapped when its first argument is not positive. `testValue` sums six calls
through these choices, including a `chooseA` result passed into `chooseB`:
5 - 5 + 18 + 14 + 2 + 4 = 38.

Results

elm-0.19.1

passran in 50 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
testValue: 38

elm-0.19.2

passran in 52 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
testValue: 38

elm-0.19.3

passran in 51 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
testValue: 38

lamdera-1.5.0

passran in 62 ms
Output
testValue: 38

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module FunctionValuesChosenByIfCaseAndTupleTest exposing (..)
{-| Functions chosen at run time by `if`, `case` and a returned tuple are called correctly.
`chooseA` picks one of two function arguments through an `if`/`else if`
chain, one branch returning it through a `let`. `chooseB` returns its function
argument from a `case`, directly, through `identity`, through a `let`, or
wrapped in a lambda that adds one. `chooseC` returns two functions as a pair,
swapped when its first argument is not positive. `testValue` sums six calls
through these choices, including a `chooseA` result passed into `chooseB`:
5 - 5 + 18 + 14 + 2 + 4 = 38.
-}
-- TAGS: elm-spec, d-exp-case, d-atpat-scon, d-valbind-pat, d-app-closure
-- CHECK: testValue: 38
import Basics exposing (..)
import Bytes exposing (..)
import Char exposing (..)
import Html exposing (text)
import List exposing (..)
import Maybe exposing (..)
import String exposing (..)
import Tuple exposing (..)
testValue : Int
testValue =
let
( p, q ) =
chooseC 1 (\x -> x + 1) (\x -> x * 2)
in
chooseA 3 (\x -> x + 1) (\x -> x - 1) 4 + chooseA 12 negate abs 5 + chooseB 2 (\x -> x * 3) 6 + chooseB 1 (chooseA 7 p q) 7 + p 1 + q 2
chooseC : Int -> (Int -> Int) -> (Int -> Int) -> ( Int -> Int, Int -> Int )
chooseC n f g =
if n > 0 then
( f, g )
else
( g, f )
chooseB : Int -> (Int -> Int) -> Int -> Int
chooseB n f =
case n of
0 ->
f
1 ->
identity f
2 ->
let
k =
f
in
k
_ ->
\x -> f x + 1
chooseA : Int -> (Int -> Int) -> (Int -> Int) -> Int -> Int
chooseA n f g =
if n > 10 then
f
else if n > 5 then
g
else if n > 2 then
f
else
let
h =
f
in
h
main =
let
_ =
Debug.log "testValue" testValue
in
Html.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_FunctionValuesChosenByIfCaseAndTupleTest, 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
testValue3838t_elm_FunctionValuesChosenByIfCaseAndTupleTest
/-- tests/elm/src/FunctionValuesChosenByIfCaseAndTupleTest.elm -/
def g_elm_FunctionValuesChosenByIfCaseAndTupleTest : Program := {
  name := "FunctionValuesChosenByIfCaseAndTupleTest", source := "tests/elm/src",
  exposing := .all, imports := [⟨"Basics", "Basics", some .all⟩, ⟨"Bytes", "Bytes", some .all⟩, ⟨"Char", "Char", some .all⟩, ⟨"Html", "Html", some (.items [.var "text"])⟩, ⟨"List", "List", some .all⟩, ⟨"Maybe", "Maybe", some .all⟩, ⟨"String", "String", some .all⟩, ⟨"Tuple", "Tuple", some .all⟩],
  types := [],
  aliases := [],
  defs := [
    .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 "chooseB" [pv "n", pv "f"] (caseOf (v "n") [(P (pint 0), v "f"), (P (pint 1), ap (v "identity") [v "f"]), (P (pint 2), letDefs [.fn "k" [] (v "f")] (v "k")), (P pw, lams [pv "x"] (bin "+" (ap (v "f") [v "x"]) (i 1)))]),
    .fn "chooseA" [pv "n", pv "f", pv "g"] (ifChain [(bin ">" (v "n") (i 10), v "f"), (bin ">" (v "n") (i 5), v "g"), (bin ">" (v "n") (i 2), v "f")] (letDefs [.fn "h" [] (v "f")] (v "h"))),
    .fn "testValue" [] (letDefs [.pat (ptup2 (P (pv "p")) (P (pv "q"))) (ap (v "chooseC") [i 1, .atexp (.paren (lams [pv "x"] (bin "+" (v "x") (i 1)))), .atexp (.paren (lams [pv "x"] (bin "*" (v "x") (i 2))))])] (bin "+" (bin "+" (bin "+" (bin "+" (bin "+" (ap (v "chooseA") [i 3, .atexp (.paren (lams [pv "x"] (bin "+" (v "x") (i 1)))), .atexp (.paren (lams [pv "x"] (bin "-" (v "x") (i 1)))), i 4]) (ap (v "chooseA") [i 12, v "negate", v "abs$Int", i 5])) (ap (v "chooseB") [i 2, .atexp (.paren (lams [pv "x"] (bin "*" (v "x") (i 3)))), i 6])) (ap (v "chooseB") [i 1, .atexp (.paren (ap (v "chooseA") [i 7, v "p", v "q"])), i 7])) (ap (v "p") [i 1])) (ap (v "q") [i 2]))),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "testValue", v "testValue"])] (ap (qv "Html" "text") [str "done"])) ],
  checks := [
    chk .check "testValue: 38" .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_FunctionValuesChosenByIfCaseAndTupleTest 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_FunctionValuesChosenByIfCaseAndTupleTest 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_FunctionValuesChosenByIfCaseAndTupleTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_FunctionValuesChosenByIfCaseAndTupleTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.