PolyStepLoopFoldMIntTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule sm-typbindrule s-ty-conrule s-valbind-varrule d-exp-lambda
Translated into Lean for the mechanisation of the book: see its Lean translation.
A monadic fold, `foldM`, built on a polymorphic `Step`-driven state loop, sums a list of `Int`s and threads its state correctly. `IO a` is a function from an `Int` state to a pair of the new state and a result. `foldM` runs `loop` over a `( List a, b )` pair whose `Done` value is the accumulator `b`, here an `Int`. `addM` adds each element to the accumulator and increments the state, so folding `[ 1, 2, 3, 4, 5 ]` from state 99 gives a sum of 15 and a final state of 104.
Results
elm-0.19.1
passran in 53 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. sum: 15 final: 104
elm-0.19.2
passran in 49 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. sum: 15 final: 104
elm-0.19.3
passran in 62 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. sum: 15 final: 104
lamdera-1.5.0
passran in 57 msOutput
sum: 15 final: 104
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm/PolyStepLoopFoldMIntTest] in its title.
Source
The -- CHECK: lines give the expected output.
module PolyStepLoopFoldMIntTest exposing (main)
{-| A monadic fold, `foldM`, built on a polymorphic `Step`-driven state loop, sums
a list of `Int`s and threads its state correctly.
`IO a` is a function from an `Int` state to a pair of the new state and a
result. `foldM` runs `loop` over a `( List a, b )` pair whose `Done` value is
the accumulator `b`, here an `Int`. `addM` adds each element to the accumulator
and increments the state, so folding `[ 1, 2, 3, 4, 5 ]` from state 99 gives a
sum of 15 and a final state of 104.
-}
-- TAGS: elm-spec, sm-typbind, s-ty-con, s-valbind-var, d-exp-lambda
-- CHECK: sum: 15
-- CHECK: final: 104
import Html exposing (text)
type Step state a
= Loop state
| Done a
type alias IO a =
Int -> ( Int, a )
loop : (state -> IO (Step state a)) -> state -> IO a
loop callback loopState ioState =
case callback loopState ioState of
( newIOState, Loop newLoopState ) ->
loop callback newLoopState newIOState
( newIOState, Done a ) ->
( newIOState, a )
foldM : (b -> a -> IO b) -> b -> List a -> IO b
foldM f b list =
loop (foldMHelp f) ( list, b )
foldMHelp : (b -> a -> IO b) -> ( List a, b ) -> IO (Step ( List a, b ) b)
foldMHelp callback ( list, result ) =
case list of
[] ->
\s -> ( s, Done result )
x :: rest ->
\s ->
let
( s2, newAcc ) =
callback result x s
in
( s2, Loop ( rest, newAcc ) )
addM : Int -> Int -> IO Int
addM acc x s =
( s + 1, acc + x )
main =
let
( final, sum ) =
foldM addM 0 [ 1, 2, 3, 4, 5 ] 99
_ =
Debug.log "sum" sum
_ =
Debug.log "final" final
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_PolyStepLoopFoldMIntTest, in StandardElmMech/Generated/Elm_PartialAppCh.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.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
sum | 15 | 15 | t_elm_PolyStepLoopFoldMIntTest |
final | 104 | 104 | t_elm_PolyStepLoopFoldMIntTest |
/-- tests/elm/src/PolyStepLoopFoldMIntTest.elm -/
def g_elm_PolyStepLoopFoldMIntTest : Program := {
name := "PolyStepLoopFoldMIntTest", source := "tests/elm/src",
exposing := (.items [.var "main"]), imports := [⟨"Html", "Html", some (.items [.var "text"])⟩],
types := [("Step", [("Loop", 1), ("Done", 1)])],
aliases := [("IO", none)],
defs := [
.fn "loop" [pv "callback", pv "loopState", pv "ioState"] (caseOf (ap (v "callback") [v "loopState", v "ioState"]) [(P (ptup2 (P (pv "newIOState")) (pcon "Loop" [pv "newLoopState"])), ap (v "loop") [v "callback", v "newLoopState", v "newIOState"]), (P (ptup2 (P (pv "newIOState")) (pcon "Done" [pv "a"])), tup2 (v "newIOState") (v "a"))]),
.fn "foldMHelp" [pv "callback", ptup2 (P (pv "list")) (P (pv "result"))] (caseOf (v "list") [(P (listP []), lams [pv "s"] (tup2 (v "s") (ap (c "Done") [v "result"]))), (.cons (P (pv "x")) (P (pv "rest")), lams [pv "s"] (letDefs [.pat (ptup2 (P (pv "s2")) (P (pv "newAcc"))) (ap (v "callback") [v "result", v "x", v "s"])] (tup2 (v "s2") (ap (c "Loop") [tup2 (v "rest") (v "newAcc")]))))]),
.fn "foldM" [pv "f", pv "b", pv "list"] (ap (v "loop") [.atexp (.paren (ap (v "foldMHelp") [v "f"])), tup2 (v "list") (v "b")]),
.fn "addM" [pv "acc", pv "x", pv "s"] (tup2 (bin "+" (v "s") (i 1)) (bin "+" (v "acc") (v "x"))),
.fn "main" [] (letDefs [.pat (ptup2 (P (pv "final")) (P (pv "sum"))) (ap (v "foldM") [v "addM", i 0, listE [i 1, i 2, i 3, i 4, i 5], i 99]), .pat pw (ap (qv "Debug" "log") [str "sum", v "sum"]), .pat pw (ap (qv "Debug" "log") [str "final", v "final"])] (ap (v "text") [str "done"])) ],
checks := [
chk .check "sum: 15" .int,
chk .check "final: 104" .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_PolyStepLoopFoldMIntTest 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_PolyStepLoopFoldMIntTest 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_PolyStepLoopFoldMIntTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_PolyStepLoopFoldMIntTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.