Elm Exactly
Results / elm /

StandardCoreEvaluationExamplesTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-pat-asrule d-pat-consrule d-atpat-unitrule d-atexp-unitrule d-atexp-oprule d-app-basic-partialrule d-app-conrule d-mrule-failrule d-atexp-sconrule d-atpat-recordrule d-patrow-fieldbasis Basics.apRbasis Basics.modBybasis Basics.remainderBybasis Basics.comparederived form field-accessor

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
Small programs for rules of the dynamic semantics of the Core that other tests reach only in
passing, and examples from the Comments of A Standard Definition of Elm?, Chapter 6.

  - an as-pattern around a cons pattern with a wildcard (r:d-pat-as, r:d-pat-cons);
  - the unit pattern and the unit expression (r:d-atpat-unit, r:d-atexp-unit);
  - an operator used as a value, applied fully and partially (r:d-atexp-op,
    r:d-app-basic-partial), and a constructor awaiting arguments (r:d-app-con);
  - `(&&)` as a value takes both arguments as values (the Comment on r:d-atexp-op);
  - `|>`, whose text is in the dynamic basis;
  - a case rule that fails is not evaluated, so its `Debug.todo` is not reached (r:d-mrule-fail);
  - `Just 1`, a constructor applied to its argument (r:d-app-con);
  - `1` at Float after number specialisation (r:d-atexp-scon, ruling A87);
  - `modBy` and `remainderBy` with a negative dividend, the book's examples;
  - record equality does not depend on the order fields are written in;
  - strings compared in order, and the field accessor `.k` (r:d-atpat-record, r:d-patrow-field).

<https://elm-test-suite.netlify.app/standard/dynamic-core>

Results

elm-0.19.1

passran in 68 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
as-pattern: (1,[1,2])
unit pattern: 5
operator value: 3
partial basic value: <function>
partial constructor: <function>
(&&) is strict: False
pipe: 6
mrule-fail: 2
Just 1: Just 1
inc 0.5: 1.5
modBy 4 -5: 3
remainderBy 4 -5: -1
record field order: True
compare strings: LT
accessor: 7

elm-0.19.2

passran in 53 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
as-pattern: (1,[1,2])
unit pattern: 5
operator value: 3
partial basic value: <function>
partial constructor: <function>
(&&) is strict: False
pipe: 6
mrule-fail: 2
Just 1: Just 1
inc 0.5: 1.5
modBy 4 -5: 3
remainderBy 4 -5: -1
record field order: True
compare strings: LT
accessor: 7

elm-0.19.3

passran in 53 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
as-pattern: (1,[1,2])
unit pattern: 5
operator value: 3
partial basic value: <function>
partial constructor: <function>
(&&) is strict: False
pipe: 6
mrule-fail: 2
Just 1: Just 1
inc 0.5: 1.5
modBy 4 -5: 3
remainderBy 4 -5: -1
record field order: True
compare strings: LT
accessor: 7

lamdera-1.5.0

passran in 60 ms
Output
as-pattern: (1,[1,2])
unit pattern: 5
operator value: 3
partial basic value: <function>
partial constructor: <function>
(&&) is strict: False
pipe: 6
mrule-fail: 2
Just 1: Just 1
inc 0.5: 1.5
modBy 4 -5: 3
remainderBy 4 -5: -1
record field order: True
compare strings: LT
accessor: 7

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module StandardCoreEvaluationExamplesTest exposing (main)
{-| Small programs for rules of the dynamic semantics of the Core that other tests reach only in
passing, and examples from the Comments of A Standard Definition of Elm?, Chapter 6.
- an as-pattern around a cons pattern with a wildcard (r:d-pat-as, r:d-pat-cons);
- the unit pattern and the unit expression (r:d-atpat-unit, r:d-atexp-unit);
- an operator used as a value, applied fully and partially (r:d-atexp-op,
r:d-app-basic-partial), and a constructor awaiting arguments (r:d-app-con);
- `(&&)` as a value takes both arguments as values (the Comment on r:d-atexp-op);
- `|>`, whose text is in the dynamic basis;
- a case rule that fails is not evaluated, so its `Debug.todo` is not reached (r:d-mrule-fail);
- `Just 1`, a constructor applied to its argument (r:d-app-con);
- `1` at Float after number specialisation (r:d-atexp-scon, ruling A87);
- `modBy` and `remainderBy` with a negative dividend, the book's examples;
- record equality does not depend on the order fields are written in;
- strings compared in order, and the field accessor `.k` (r:d-atpat-record, r:d-patrow-field).
<https://elm-test-suite.netlify.app/standard/dynamic-core>
-}
-- TAGS: elm-spec, d-pat-as, d-pat-cons, d-atpat-unit, d-atexp-unit, d-atexp-op, d-app-basic-partial, d-app-con, d-mrule-fail, d-atexp-scon, d-atpat-record, d-patrow-field, b:Basics.apR, b:Basics.modBy, b:Basics.remainderBy, b:Basics.compare, df:field-accessor
-- CHECK: as-pattern: (1,[1,2])
-- CHECK: unit pattern: 5
-- CHECK: operator value: 3
-- CHECK: partial basic value: <function>
-- CHECK: partial constructor: <function>
-- CHECK: (&&) is strict: False
-- CHECK: pipe: 6
-- CHECK: mrule-fail: 2
-- CHECK: Just 1: Just 1
-- CHECK: inc 0.5: 1.5
-- CHECK: modBy 4 -5: 3
-- CHECK: remainderBy 4 -5: -1
-- CHECK: record field order: True
-- CHECK: compare strings: LT
-- CHECK: accessor: 7
import Html
type Tree
= Leaf
| Node Tree Int Tree
add1 : Int -> Int
add1 n =
n + 1
main =
let
_ =
Debug.log "as-pattern"
(case [ 1, 2 ] of
((x :: _) as whole) ->
( x, whole )
_ ->
( 0, [] )
)
_ =
Debug.log "unit pattern" ((\() -> 5) ())
_ =
Debug.log "operator value" ((+) 1 2)
_ =
Debug.log "partial basic value" ((+) 1)
_ =
Debug.log "partial constructor" (Node Leaf)
_ =
Debug.log "(&&) is strict" ((&&) False True)
_ =
Debug.log "pipe" (5 |> add1)
_ =
Debug.log "mrule-fail"
(case 1 of
0 ->
Debug.todo "x"
_ ->
2
)
_ =
Debug.log "Just 1" (Just 1)
_ =
Debug.log "inc 0.5"
(let
incF n =
n + 1
in
incF 0.5
)
_ =
Debug.log "modBy 4 -5" (modBy 4 -5)
_ =
Debug.log "remainderBy 4 -5" (remainderBy 4 -5)
_ =
Debug.log "record field order" ({ a = 1, b = 2 } == { b = 2, a = 1 })
_ =
Debug.log "compare strings" (compare "ab" "abc")
_ =
Debug.log "accessor" (.k { k = 7, j = 8 })
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_StandardCoreEvaluationExamplesTest, in StandardElmMech/Generated/Elm_SingleCtorPairBoolF.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.

/-- tests/elm/src/StandardCoreEvaluationExamplesTest.elm -/
def g_elm_StandardCoreEvaluationExamplesTest : Program := {
  name := "StandardCoreEvaluationExamplesTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", none⟩],
  types := [("Tree", [("Leaf", 0), ("Node", 3)])],
  aliases := [],
  defs := [
    .fn "add1" [pv "n"] (bin "+" (v "n") (i 1)),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "as-pattern", .atexp (.paren (caseOf (listE [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2))]) [(P (.paren (.as (P (.paren (.cons (P (pv "x")) (P pw)))) "whole")), tup2 (v "x") (v "whole")), (P pw, tup2 (.atexp (.scon (.intF 0))) (listE []))]))]), .pat pw (ap (qv "Debug" "log") [str "unit pattern", .atexp (.paren (ap (.atexp (.paren (lams [.unit] (.atexp (.scon (.intF 5)))))) [.atexp .unit]))]), .pat pw (ap (qv "Debug" "log") [str "operator value", .atexp (.paren (ap (.atexp (.op "+")) [.atexp (.scon (.intF 1)), .atexp (.scon (.intF 2))]))]), .pat pw (ap (qv "Debug" "log") [str "partial basic value", .atexp (.paren (ap (.atexp (.op "+")) [.atexp (.scon (.intF 1))]))]), .pat pw (ap (qv "Debug" "log") [str "partial constructor", .atexp (.paren (ap (c "Node") [c "Leaf"]))]), .pat pw (ap (qv "Debug" "log") [str "(&&) is strict", .atexp (.paren (ap (.atexp (.op "&&")) [c "False", c "True"]))]), .pat pw (ap (qv "Debug" "log") [str "pipe", .atexp (.paren (bin "|>" (i 5) (v "add1")))]), .pat pw (ap (qv "Debug" "log") [str "mrule-fail", .atexp (.paren (caseOf (i 1) [(P (pint 0), ap (qv "Debug" "todo") [str "x"]), (P pw, .atexp (.scon (.intF 2)))]))]), .pat pw (ap (qv "Debug" "log") [str "Just 1", .atexp (.paren (ap (c "Just") [.atexp (.scon (.intF 1))]))]), .pat pw (ap (qv "Debug" "log") [str "inc 0.5", .atexp (.paren (letDefs [.fn "incF" [pv "n"] (bin "+" (v "n") (.atexp (.scon (.intF 1))))] (ap (v "incF") [.atexp (.scon (.float 5 (-1)))])))]), .pat pw (ap (qv "Debug" "log") [str "modBy 4 -5", .atexp (.paren (ap (v "modBy") [i 4, .atexp (neg (A (i 5)))]))]), .pat pw (ap (qv "Debug" "log") [str "remainderBy 4 -5", .atexp (.paren (ap (v "remainderBy") [i 4, .atexp (neg (A (i 5)))]))]), .pat pw (ap (qv "Debug" "log") [str "record field order", .atexp (.paren (bin "==" (recE [("a", .atexp (.scon (.intF 1))), ("b", .atexp (.scon (.intF 2)))]) (recE [("b", .atexp (.scon (.intF 2))), ("a", .atexp (.scon (.intF 1)))])))]), .pat pw (ap (qv "Debug" "log") [str "compare strings", .atexp (.paren (ap (v "compare") [str "ab", str "abc"]))]), .pat pw (ap (qv "Debug" "log") [str "accessor", .atexp (.paren (ap (.atexp (accessor "k")) [recE [("k", .atexp (.scon (.intF 7))), ("j", .atexp (.scon (.intF 8)))]]))])] (ap (qv "Html" "text") [str "done"])) ],
  checks := [
    chk .check "as-pattern: (1,[1,2])" .data,
    chk .check "unit pattern: 5" .float,
    chk .check "operator value: 3" .float,
    chk .check "partial basic value: <function>" .function,
    chk .check "partial constructor: <function>" .function,
    chk .check "(&&) is strict: False" .data,
    chk .check "pipe: 6" .int,
    chk .check "mrule-fail: 2" .float,
    chk .check "Just 1: Just 1" .data,
    chk .check "inc 0.5: 1.5" .float,
    chk .check "modBy 4 -5: 3" .int,
    chk .check "remainderBy 4 -5: -1" .int,
    chk .check "record field order: True" .data,
    chk .check "compare strings: LT" .data,
    chk .check "accessor: 7" .float ] }

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_StandardCoreEvaluationExamplesTest 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_StandardCoreEvaluationExamplesTest 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_StandardCoreEvaluationExamplesTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_StandardCoreEvaluationExamplesTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.