Elm Exactly
Results / elm /

BoolShortCircuitParserIdiomTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule d-exp-caserule d-mrule-fail

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
A bounds check guarding an indexing call with `&&` (`idx < length src && unsafeIndex src idx /= ...`) stops the call from running past the end of the input.

`&&` must not evaluate its right operand when the left is `False`. The input
is a list of characters, and `length` and `drop` are local functions written
by pattern matching. The right operand would reach a branch that recurses
without end, so a past-end peek must print `"none"`, and an in-bounds peek
prints the character.

Results

elm-0.19.1

passran in 51 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
peek at past-end: "none"
peek at first: 'H'

elm-0.19.2

passran in 75 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
peek at past-end: "none"
peek at first: 'H'

elm-0.19.3

passran in 43 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
peek at past-end: "none"
peek at first: 'H'

lamdera-1.5.0

passran in 59 ms
Output
peek at past-end: "none"
peek at first: 'H'

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module BoolShortCircuitParserIdiomTest exposing (main)
{-| A bounds check guarding an indexing call with `&&` (`idx < length src && unsafeIndex src idx /= ...`) stops the call from running past the end of the input.
`&&` must not evaluate its right operand when the left is `False`. The input
is a list of characters, and `length` and `drop` are local functions written
by pattern matching. The right operand would reach a branch that recurses
without end, so a past-end peek must print `"none"`, and an in-bounds peek
prints the character.
-}
-- TAGS: elm-spec, d-exp-case, d-mrule-fail
-- CHECK: peek at past-end: "none"
-- CHECK: peek at first: 'H'
import Html exposing (text)
{-| Recurses without end; never called when the program is correct.
-}
stackBomb : Int -> Int
stackBomb n =
1 + stackBomb (n + 1)
{-| The number of elements of a list.
-}
length : List a -> Int
length xs =
case xs of
[] ->
0
_ :: rest ->
1 + length rest
{-| The list without its first `n` elements.
-}
drop : Int -> List a -> List a
drop n xs =
if n <= 0 then
xs
else
case xs of
[] ->
[]
_ :: rest ->
drop (n - 1) rest
{-| Returns the character at `index`, or calls `stackBomb` when `index` is past the end.
-}
unsafeIndex : List Char -> Int -> Char
unsafeIndex chars index =
case drop index chars of
c :: _ ->
c
[] ->
-- Unreachable under correct short-circuit semantics.
if stackBomb 0 == 0 then
'?'
else
'?'
{-| Returns the character at `idx`, or `Nothing` when `idx` is past the end.
-}
peek : List Char -> Int -> Maybe Char
peek src idx =
if idx < length src && unsafeIndex src idx /= '\u{0000}' then
Just (unsafeIndex src idx)
else
Nothing
main =
let
pastEnd : String
pastEnd =
case peek [ 'H', 'i' ] 2 of
Just c ->
"CHAR:" ++ Debug.toString c
Nothing ->
"none"
first : Char
first =
case peek [ 'H', 'i' ] 0 of
Just c ->
c
Nothing ->
'?'
_ =
Debug.log "peek at past-end" pastEnd
_ =
Debug.log "peek at first" first
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_BoolShortCircuitParserIdiomTest, in StandardElmMech/Generated/Elm_As.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
peek at past-end"none""none"t_elm_BoolShortCircuitParserIdiomTest
peek at first'H''H't_elm_BoolShortCircuitParserIdiomTest
/-- tests/elm/src/BoolShortCircuitParserIdiomTest.elm -/
def g_elm_BoolShortCircuitParserIdiomTest : Program := {
  name := "BoolShortCircuitParserIdiomTest", source := "tests/elm/src", native := true,
  exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
  types := [],
  aliases := [],
  defs := [
    .fn "stackBomb" [pv "n"] (bin "+" (i 1) (ap (v "stackBomb") [.atexp (.paren (bin "+" (v "n") (i 1)))])),
    .fn "length" [pv "xs"] (caseOf (v "xs") [(P (listP []), i 0), (.cons (P pw) (P (pv "rest")), bin "+" (i 1) (ap (v "length") [v "rest"]))]),
    .fn "drop" [pv "n", pv "xs"] (ifte (bin "<=" (v "n") (i 0)) (v "xs") (caseOf (v "xs") [(P (listP []), listE []), (.cons (P pw) (P (pv "rest")), ap (v "drop") [.atexp (.paren (bin "-" (v "n") (i 1))), v "rest"])])),
    .fn "unsafeIndex" [pv "chars", pv "index"] (caseOf (ap (v "drop") [v "index", v "chars"]) [(.cons (P (pv "c")) (P pw), v "c"), (P (listP []), ifte (bin "==" (ap (v "stackBomb") [i 0]) (i 0)) (.atexp (chA '?')) (.atexp (chA '?')))]),
    .fn "peek" [pv "src", pv "idx"] (ifte (andalso (bin "<" (v "idx") (ap (v "length") [v "src"])) (bin "/=" (ap (v "unsafeIndex") [v "src", v "idx"]) (.atexp (chA '\x00')))) (ap (c "Just") [.atexp (.paren (ap (v "unsafeIndex") [v "src", v "idx"]))]) (c "Nothing")),
    .fn "main" [] (letDefs [.fn "pastEnd" [] (caseOf (ap (v "peek") [listE [.atexp (chA 'H'), .atexp (chA 'i')], i 2]) [(pcon "Just" [pv "c"], bin "++" (str "CHAR:") (ap (qv "Debug" "toString") [v "c"])), (P (pcA "Nothing"), str "none")]), .fn "first" [] (caseOf (ap (v "peek") [listE [.atexp (chA 'H'), .atexp (chA 'i')], i 0]) [(pcon "Just" [pv "c"], v "c"), (P (pcA "Nothing"), .atexp (chA '?'))]), .pat pw (ap (qv "Debug" "log") [str "peek at past-end", v "pastEnd"]), .pat pw (ap (qv "Debug" "log") [str "peek at first", v "first"])] (ap (v "text") [str "done"])) ],
  checks := [
    chk .check "peek at past-end: \"none\"" .string,
    chk .check "peek at first: 'H'" .char ] }

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