Elm Exactly
Results / elm-core /

TaskOnErrorCascadeTest

Tags:elm-spec

Checks, in A Standard Definition of Elm?:rule e-task-andthen-succeedrule e-task-andthen-failrule e-task-onerror-failrule e-task-andthen-inner

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
`Task.onError` recovers a failed `Task.andThen` chain, skipping the steps
between the failure and the handler, and the recovered task can be chained
further.

`segment step` adds 1 to `step`, then fails with `"boom"`, so the following
`andThen` that would add 100 never runs, and its `onError` handler logs
`recovered <step>` and succeeds with `step * 10`. `chain` runs segments 1, 2 and
3 in order and sums their results, so the worker logs the three recoveries in
order and then `final: 60`.

Among what is not tested: a handler that fails again, and a failure that reaches
`Task.attempt` unrecovered.

Results

elm-0.19.1

passran in 67 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
onerr: "recovered 1"
onerr: "recovered 2"
onerr: "recovered 3"
onerr: "final: 60"

elm-0.19.2

passran in 84 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
onerr: "recovered 1"
onerr: "recovered 2"
onerr: "recovered 3"
onerr: "final: 60"

elm-0.19.3

passran in 71 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
onerr: "recovered 1"
onerr: "recovered 2"
onerr: "recovered 3"
onerr: "final: 60"

lamdera-1.5.0

passran in 41 ms
Output
onerr: "recovered 1"
onerr: "recovered 2"
onerr: "recovered 3"
onerr: "final: 60"

Discussions

Discuss this test·Search the discussions on GitHub

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

Source

The -- CHECK: lines give the expected output.

module TaskOnErrorCascadeTest exposing (main)
{-| `Task.onError` recovers a failed `Task.andThen` chain, skipping the steps
between the failure and the handler, and the recovered task can be chained
further.
`segment step` adds 1 to `step`, then fails with `"boom"`, so the following
`andThen` that would add 100 never runs, and its `onError` handler logs
`recovered <step>` and succeeds with `step * 10`. `chain` runs segments 1, 2 and
3 in order and sums their results, so the worker logs the three recoveries in
order and then `final: 60`.
Among what is not tested: a handler that fails again, and a failure that reaches
`Task.attempt` unrecovered.
-}
-- TAGS: elm-spec, e-task-andthen-succeed, e-task-andthen-fail, e-task-onerror-fail, e-task-andthen-inner
-- CHECK: onerr: "recovered 1"
-- CHECK: onerr: "recovered 2"
-- CHECK: onerr: "recovered 3"
-- CHECK: onerr: "final: 60"
import Platform
import Task exposing (Task)
type Msg
= Done Int
type alias Model =
{}
segment : Int -> Task String Int
segment step =
Task.succeed step
|> Task.andThen (\x -> Task.succeed (x + 1))
|> Task.andThen (\_ -> Task.fail "boom")
|> Task.andThen (\x -> Task.succeed (x + 100))
|> Task.onError
(\_ ->
let
_ =
Debug.log "onerr" ("recovered " ++ Debug.toString step)
in
Task.succeed (step * 10)
)
chain : Task String Int
chain =
segment 1
|> Task.andThen (\a -> segment 2 |> Task.map (\b -> a + b))
|> Task.andThen (\ab -> segment 3 |> Task.map (\c -> ab + c))
handleResult : Result String Int -> Msg
handleResult r =
case r of
Ok v ->
Done v
Err _ ->
Done -1
init : () -> ( Model, Cmd Msg )
init _ =
( {}
, Task.attempt handleResult chain
)
update : Msg -> Model -> ( Model, Cmd Msg )
update (Done v) model =
let
_ =
Debug.log "onerr" ("final: " ++ Debug.toString v)
in
( model, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none
main : Program () Model Msg
main =
Platform.worker
{ init = init
, update = update
, subscriptions = subscriptions
}

Lean translation

This worker program is translated into Lean by the translator of the Lean mechanisation of A Standard Definition of Elm? (g_elm_core_TaskOnErrorCascadeTest, in StandardElmMech/Generated/Worker01.lean). Lean evaluates its modules by the rules of Chapter 7, starts it by r:p-init with no flags (null, A72), and runs it by the transitions of Chapter 9 under a host that sends nothing. Its checks become one generated theorem: some execution the book allows, under that host, has a log (the modules', then the start's, then each step's) that meets every check. The theorem trusts the parser and the translator's elaboration ([parse] [elab]), the test basis ([harness]) and the parameters of the basis ([params]), and is proved by native_decide ([native]).

Out of date. The test, or a module it imports, has changed since this translation was made; the translation, and so its theorems, describe the program as it was until the mechanisation is rebuilt.

CheckExpected (CHECK)Lean evaluatorProved by
onerr"recovered 1" (the test now says "final: 60")"recovered 1" or "recovered 2" or "recovered 3" or "final: 60"t_elm_core_TaskOnErrorCascadeTest
onerr"recovered 2" (the test now says "final: 60")"recovered 1" or "recovered 2" or "recovered 3" or "final: 60"t_elm_core_TaskOnErrorCascadeTest
onerr"recovered 3" (the test now says "final: 60")"recovered 1" or "recovered 2" or "recovered 3" or "final: 60"t_elm_core_TaskOnErrorCascadeTest
onerr"final: 60""recovered 1" or "recovered 2" or "recovered 3" or "final: 60"t_elm_core_TaskOnErrorCascadeTest
/-- tests/elm-core/src/TaskOnErrorCascadeTest.elm -/
def g_elm_core_TaskOnErrorCascadeTest : Program := {
  name := "TaskOnErrorCascadeTest", source := "tests/elm-core/src", native := true,
  exposing := (.items [.var "main"]), imports := [⟨"Platform", "Platform", none⟩, ⟨"Task", "Task", some (.items [.tycon "Task"])⟩],
  types := [("Msg", [("Done", 1)])],
  aliases := [("Model", some [])],
  defs := [
    .fn "segment" [pv "step"] (bin "|>" (bin "|>" (bin "|>" (bin "|>" (ap (qv "Task" "succeed") [v "step"]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pv "x"] (ap (qv "Task" "succeed") [.atexp (.paren (bin "+" (v "x") (i 1)))])))])) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (ap (qv "Task" "fail") [str "boom"])))])) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pv "x"] (ap (qv "Task" "succeed") [.atexp (.paren (bin "+" (v "x") (i 100)))])))])) (ap (qv "Task" "onError") [.atexp (.paren (lams [pw] (letDefs [.pat pw (ap (qv "Debug" "log") [str "onerr", .atexp (.paren (bin "++" (str "recovered ") (ap (qv "Debug" "toString") [v "step"])))])] (ap (qv "Task" "succeed") [.atexp (.paren (bin "*" (v "step") (i 10)))]))))])),
    .fn "chain" [] (bin "|>" (bin "|>" (ap (v "segment") [i 1]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pv "a"] (bin "|>" (ap (v "segment") [i 2]) (ap (qv "Task" "map") [.atexp (.paren (lams [pv "b"] (bin "+" (v "a") (v "b"))))]))))])) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pv "ab"] (bin "|>" (ap (v "segment") [i 3]) (ap (qv "Task" "map") [.atexp (.paren (lams [pv "c"] (bin "+" (v "ab") (v "c"))))]))))])),
    .fn "handleResult" [pv "r"] (caseOf (v "r") [(pcon "Ok" [pv "v"], ap (c "Done") [v "v"]), (pcon "Err" [pw], ap (c "Done") [.atexp (neg (A (i 1)))])]),
    .fn "init" [pw] (tup2 (recE []) (ap (qv "Task" "attempt") [v "handleResult", v "chain"])),
    .fn "update" [.paren (pcon "Done" [pv "v"]), pv "model"] (letDefs [.pat pw (ap (qv "Debug" "log") [str "onerr", .atexp (.paren (bin "++" (str "final: ") (ap (qv "Debug" "toString") [v "v"])))])] (tup2 (v "model") (qv "Cmd" "none"))),
    .fn "subscriptions" [pw] (qv "Sub" "none"),
    .fn "main" [] (ap (qv "Platform" "worker") [recE [("init", v "init"), ("update", v "update"), ("subscriptions", v "subscriptions")]]) ],
  checks := [
    chk .check "onerr: \"recovered 1\"" .string,
    chk .check "onerr: \"recovered 2\"" .string,
    chk .check "onerr: \"recovered 3\"" .string,
    chk .check "onerr: \"final: 60\"" .string ] }

/-- The worker test of TaskOnErrorCascadeTest. -/
def w_elm_core_TaskOnErrorCascadeTest : Prog.WorkerTest := {
  prog := g_elm_core_TaskOnErrorCascadeTest,
  flagsType := .unit,
  flags := .null,
  host := .none,
  checks := [] }

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