Elm Exactly
Results / elm /

DeepEqualityTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:basis Basics.eqbasis Basics.neq

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
`==` and `/=` give the exact answer on values nested more than 100 levels deep.

The fixtures are built by recursion on `n`: `nat n` is `n` applications of `S` to `Z`, `nest n
leaf` is `n` `Wrap`s around `Leaf leaf`, and `chain n bottom` is `n` records linked through
`next = Just ...`, ending in one whose `v` is `bottom`.

  - `nat 150 vs 200`: two naturals of different depth are not equal.
  - `nat 150 vs 150`: two naturals of the same depth are equal.
  - `nest 120` and `nest 500 /=`: values that differ only in the leaf, 120 and 500 levels down,
    are not equal.
  - `record chain 300`: record chains that differ only in the innermost `v` are not equal.

Two Dicts that differ deep down are checked by DictDeepEqualityTest.

Results

elm-0.19.1

passran in 62 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
nat 150 vs 200: "False"
nat 150 vs 150: "True"
nest 120: "False"
nest 500 /=: "True"
record chain 300: "False"

elm-0.19.2

passran in 91 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
nat 150 vs 200: "False"
nat 150 vs 150: "True"
nest 120: "False"
nest 500 /=: "True"
record chain 300: "False"

elm-0.19.3

passran in 67 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
nat 150 vs 200: "False"
nat 150 vs 150: "True"
nest 120: "False"
nest 500 /=: "True"
record chain 300: "False"

lamdera-1.5.0

passran in 91 ms
Output
nat 150 vs 200: "False"
nat 150 vs 150: "True"
nest 120: "False"
nest 500 /=: "True"
record chain 300: "False"

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module DeepEqualityTest exposing (main)
{-| `==` and `/=` give the exact answer on values nested more than 100 levels deep.
The fixtures are built by recursion on `n`: `nat n` is `n` applications of `S` to `Z`, `nest n
leaf` is `n` `Wrap`s around `Leaf leaf`, and `chain n bottom` is `n` records linked through
`next = Just ...`, ending in one whose `v` is `bottom`.
- `nat 150 vs 200`: two naturals of different depth are not equal.
- `nat 150 vs 150`: two naturals of the same depth are equal.
- `nest 120` and `nest 500 /=`: values that differ only in the leaf, 120 and 500 levels down,
are not equal.
- `record chain 300`: record chains that differ only in the innermost `v` are not equal.
Two Dicts that differ deep down are checked by DictDeepEqualityTest.
-}
-- TAGS: elm-spec, b:Basics.eq, b:Basics.neq
-- MODE-GUARD: a Char equality must give the same answer as under --optimize
-- NO-SKIP: an edge case every compiler must run
-- CHECK: nat 150 vs 200: "False"
-- CHECK: nat 150 vs 150: "True"
-- CHECK: nest 120: "False"
-- CHECK: nest 500 /=: "True"
-- CHECK: record chain 300: "False"
import Html exposing (text)
type Nat
= Z
| S Nat
nat : Int -> Nat
nat n =
if n <= 0 then
Z
else
S (nat (n - 1))
type Nest
= Leaf Int
| Wrap Nest
nest : Int -> Int -> Nest
nest n leaf =
if n <= 0 then
Leaf leaf
else
Wrap (nest (n - 1) leaf)
type Chain
= Chain { next : Maybe Chain, v : Int }
chain : Int -> Int -> Chain
chain n bottom =
if n <= 0 then
Chain { next = Nothing, v = bottom }
else
Chain { next = Just (chain (n - 1) bottom), v = 0 }
bool : Bool -> String
bool b =
if b then
"True"
else
"False"
main =
let
_ =
Debug.log "nat 150 vs 200" (bool (nat 150 == nat 200))
_ =
Debug.log "nat 150 vs 150" (bool (nat 150 == nat 150))
_ =
Debug.log "nest 120" (bool (nest 120 1 == nest 120 2))
_ =
Debug.log "nest 500 /=" (bool (nest 500 1 /= nest 500 2))
_ =
Debug.log "record chain 300" (bool (chain 300 1 == chain 300 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_DeepEqualityTest, in StandardElmMech/Generated/Elm_CovTailRecPairParameters.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
nat 150 vs 200"False""False"t_elm_DeepEqualityTest
nat 150 vs 150"True""True"t_elm_DeepEqualityTest
nest 120"False""False"t_elm_DeepEqualityTest
nest 500 /="True""True"t_elm_DeepEqualityTest
record chain 300"False""False"t_elm_DeepEqualityTest
/-- tests/elm/src/DeepEqualityTest.elm -/
def g_elm_DeepEqualityTest : Program := {
  name := "DeepEqualityTest", source := "tests/elm/src",
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [("Chain", [("Chain", 1)]), ("Nat", [("Z", 0), ("S", 1)]), ("Nest", [("Leaf", 1), ("Wrap", 1)])],
  aliases := [],
  defs := [
    .fn "nat" [pv "n"] (ifte (bin "<=" (v "n") (i 0)) (c "Z") (ap (c "S") [.atexp (.paren (ap (v "nat") [.atexp (.paren (bin "-" (v "n") (i 1)))]))])),
    .fn "nest" [pv "n", pv "leaf"] (ifte (bin "<=" (v "n") (i 0)) (ap (c "Leaf") [v "leaf"]) (ap (c "Wrap") [.atexp (.paren (ap (v "nest") [.atexp (.paren (bin "-" (v "n") (i 1))), v "leaf"]))])),
    .fn "chain" [pv "n", pv "bottom"] (ifte (bin "<=" (v "n") (i 0)) (ap (c "Chain") [recE [("next", c "Nothing"), ("v", v "bottom")]]) (ap (c "Chain") [recE [("next", ap (c "Just") [.atexp (.paren (ap (v "chain") [.atexp (.paren (bin "-" (v "n") (i 1))), v "bottom"]))]), ("v", i 0)]])),
    .fn "bool" [pv "b"] (ifte (v "b") (str "True") (str "False")),
    .fn "main" [] (letDefs [.pat pw (ap (qv "Debug" "log") [str "nat 150 vs 200", .atexp (.paren (ap (v "bool") [.atexp (.paren (bin "==" (ap (v "nat") [i 150]) (ap (v "nat") [i 200])))]))]), .pat pw (ap (qv "Debug" "log") [str "nat 150 vs 150", .atexp (.paren (ap (v "bool") [.atexp (.paren (bin "==" (ap (v "nat") [i 150]) (ap (v "nat") [i 150])))]))]), .pat pw (ap (qv "Debug" "log") [str "nest 120", .atexp (.paren (ap (v "bool") [.atexp (.paren (bin "==" (ap (v "nest") [i 120, i 1]) (ap (v "nest") [i 120, i 2])))]))]), .pat pw (ap (qv "Debug" "log") [str "nest 500 /=", .atexp (.paren (ap (v "bool") [.atexp (.paren (bin "/=" (ap (v "nest") [i 500, i 1]) (ap (v "nest") [i 500, i 2])))]))]), .pat pw (ap (qv "Debug" "log") [str "record chain 300", .atexp (.paren (ap (v "bool") [.atexp (.paren (bin "==" (ap (v "chain") [i 300, i 1]) (ap (v "chain") [i 300, i 2])))]))])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "nat 150 vs 200: \"False\"" .string,
    chk .check "nat 150 vs 150: \"True\"" .string,
    chk .check "nest 120: \"False\"" .string,
    chk .check "nest 500 /=: \"True\"" .string,
    chk .check "record chain 300: \"False\"" .string ] }

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_DeepEqualityTest 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_DeepEqualityTest 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_DeepEqualityTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_DeepEqualityTest 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]).