Elm Exactly
Results / elm /

AliasForwardTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule sm-topdec-valuerule s-valbind-varrule s-atexp-oprule d-atexp-oprule d-app-basic-partialrule d-app-basic-apply

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 as another function, `dup = twice` and `plus = (+)`, behave like the function they name.

`dup` is called at `Int` and at `String` and is passed as a value to a
local `map`. `plus` is called fully applied through `inc`, passed to a local
`foldl` at `Int` and at `Float`, and partially applied in `map`. The `Float`
result is reduced to an `Int` before it is logged.

Results

elm-0.19.1

passran in 72 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
dupInt: 3
dupStr: "a!!"
foldInt: 6
foldFloat: 45
mapPartial: 5
value: 4

elm-0.19.2

passran in 61 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
dupInt: 3
dupStr: "a!!"
foldInt: 6
foldFloat: 45
mapPartial: 5
value: 4

elm-0.19.3

passran in 44 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
dupInt: 3
dupStr: "a!!"
foldInt: 6
foldFloat: 45
mapPartial: 5
value: 4

lamdera-1.5.0

passran in 50 ms
Output
dupInt: 3
dupStr: "a!!"
foldInt: 6
foldFloat: 45
mapPartial: 5
value: 4

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module AliasForwardTest exposing (main)
{-| Top-level values defined as another function, `dup = twice` and `plus = (+)`, behave like the function they name.
`dup` is called at `Int` and at `String` and is passed as a value to a
local `map`. `plus` is called fully applied through `inc`, passed to a local
`foldl` at `Int` and at `Float`, and partially applied in `map`. The `Float`
result is reduced to an `Int` before it is logged.
-}
import Html exposing (text)
-- TAGS: elm-spec, sm-topdec-value, s-valbind-var, s-atexp-op, d-atexp-op, d-app-basic-partial, d-app-basic-apply
-- CHECK: dupInt: 3
-- CHECK: dupStr: "a!!"
-- CHECK: foldInt: 6
-- CHECK: foldFloat: 45
-- CHECK: mapPartial: 5
-- CHECK: value: 4
twice : (a -> a) -> a -> a
twice f x =
f (f x)
dup : (a -> a) -> a -> a
dup =
twice
plus : number -> number -> number
plus =
(+)
inc : Int -> Int
inc n =
plus n 1
bang : String -> String
bang s =
s ++ "!"
foldl : (a -> b -> b) -> b -> List a -> b
foldl f acc xs =
case xs of
[] ->
acc
x :: rest ->
foldl f (f x acc) rest
map : (a -> b) -> List a -> List b
map f xs =
case xs of
[] ->
[]
x :: rest ->
f x :: map f rest
sum : List number -> number
sum xs =
foldl (+) 0 xs
main =
let
_ =
Debug.log "dupInt" (dup inc 1)
_ =
Debug.log "dupStr" (dup bang "a")
_ =
Debug.log "foldInt" (foldl plus 0 [ 1, 2, 3 ])
_ =
Debug.log "foldFloat" (round (foldl plus 0.5 [ 1.5, 2.5 ] * 10))
_ =
Debug.log "mapPartial" (sum (map (plus 1) [ 1, 2 ]))
_ =
Debug.log "value" (sum (map dup [ inc ] |> map (\g -> g 2)))
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_AliasForwardTest, in StandardElmMech/Generated/Elm.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
dupInt33t_elm_AliasForwardTest
dupStr"a!!""a!!"t_elm_AliasForwardTest
foldInt66t_elm_AliasForwardTest
foldFloat4545t_elm_AliasForwardTest
mapPartial55t_elm_AliasForwardTest
value44t_elm_AliasForwardTest
/-- tests/elm/src/AliasForwardTest.elm -/
def g_elm_AliasForwardTest : Program := {
  name := "AliasForwardTest", source := "tests/elm/src", native := true,
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "twice" [pv "f", pv "x"] (ap (v "f") [.atexp (.paren (ap (v "f") [v "x"]))]),
    .fn "dup" [] (v "twice"),
    .fn "plus" [] (.atexp (.op "+")),
    .fn "plus$Int" [] (.atexp (.op "+")),
    .fn "inc" [pv "n"] (ap (v "plus$Int") [v "n", i 1]),
    .fn "bang" [pv "s"] (bin "++" (v "s") (str "!")),
    .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 "map" [pv "f", pv "xs"] (caseOf (v "xs") [(P (listP []), listE []), (.cons (P (pv "x")) (P (pv "rest")), bin "::" (ap (v "f") [v "x"]) (ap (v "map") [v "f", v "rest"]))]),
    .fn "sum" [pv "xs"] (ap (v "foldl") [.atexp (.op "+"), .atexp (.scon (.intF 0)), v "xs"]),
    .fn "sum$Int" [pv "xs"] (ap (v "foldl") [.atexp (.op "+"), i 0, v "xs"]),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "dupInt", .atexp (.paren (ap (v "dup") [v "inc", i 1]))]), .pat pw (ap (qv "Debug" "log") [str "dupStr", .atexp (.paren (ap (v "dup") [v "bang", str "a"]))]), .pat pw (ap (qv "Debug" "log") [str "foldInt", .atexp (.paren (ap (v "foldl") [v "plus", .atexp (.scon (.intF 0)), listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2)), .atexp (.scon (.intF 3))]]))]), .pat pw (ap (qv "Debug" "log") [str "foldFloat", .atexp (.paren (ap (v "round") [.atexp (.paren (bin "*" (ap (v "foldl") [v "plus", .atexp (.scon (.float 5 (-1))), listE [.atexp (.scon (.float 15 (-1))), .atexp (.scon (.float 25 (-1)))]]) (.atexp (.scon (.intF 10)))))]))]), .pat pw (ap (qv "Debug" "log") [str "mapPartial", .atexp (.paren (ap (v "sum") [.atexp (.paren (ap (v "map") [.atexp (.paren (ap (v "plus") [.atexp (.scon (.intF 1))])), listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2))]]))]))]), .pat pw (ap (qv "Debug" "log") [str "value", .atexp (.paren (ap (v "sum$Int") [.atexp (.paren (bin "|>" (ap (v "map") [v "dup", listE [v "inc"]]) (ap (v "map") [.atexp (.paren (lams [pv "g"] (ap (v "g") [i 2])))])))]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "dupInt: 3" .int,
    chk .check "dupStr: \"a!!\"" .string,
    chk .check "foldInt: 6" .float,
    chk .check "foldFloat: 45" .int,
    chk .check "mapPartial: 5" .float,
    chk .check "value: 4" .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_AliasForwardTest 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_AliasForwardTest 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_AliasForwardTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_AliasForwardTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality. One of these theorems is proved by native_decide ([native]).