TaskOnErrorInnerTest
Tags:elm-spec
Checks, in A Standard Definition of Elm?:rule e-task-onerror-innerrule e-task-onerror-failrule e-task-onerror-succeedrule e-task-andthen-inner
Translated into Lean for the mechanisation of the book: see its Lean translation.
`Task.onError` lets its task take steps before it succeeds or fails. A Standard Definition of Elm?, rule e-task-onerror-inner: the task inside `onError` reduces step by step, here through a sleep and an `andThen`, before rule e-task-onerror-fail hands its error to the handler or rule e-task-onerror-succeed passes its value through. <https://elm-test-suite.netlify.app/standard/rules/e-task-onerror-inner>
Results
elm-0.19.1
passran in 59 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. succeeded through: 5 failed then handled: 70
elm-0.19.2
passran in 55 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. succeeded through: 5 failed then handled: 70
elm-0.19.3
passran in 49 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. succeeded through: 5 failed then handled: 70
lamdera-1.5.0
passran in 58 msOutput
succeeded through: 5 failed then handled: 70
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/TaskOnErrorInnerTest] in its title.
Source
The -- CHECK: lines give the expected output.
module TaskOnErrorInnerTest exposing (main)
{-| `Task.onError` lets its task take steps before it succeeds or fails.
A Standard Definition of Elm?, rule e-task-onerror-inner: the task inside `onError` reduces step by
step, here through a sleep and an `andThen`, before rule e-task-onerror-fail hands its error to
the handler or rule e-task-onerror-succeed passes its value through. <https://elm-test-suite.netlify.app/standard/rules/e-task-onerror-inner>
-}
-- TAGS: elm-spec, e-task-onerror-inner, e-task-onerror-fail, e-task-onerror-succeed, e-task-andthen-inner
-- CHECK: failed then handled: 70
-- CHECK: succeeded through: 5
import Process
import Task
type Msg
= Failed Int
| Succeeded Int
failsLater : Task.Task Int Int
failsLater =
Process.sleep 1 |> Task.andThen (\_ -> Task.fail 7)
succeedsLater : Task.Task Int Int
succeedsLater =
Process.sleep 1 |> Task.andThen (\_ -> Task.succeed 5)
handled : Task.Task Int Int -> Task.Task Never Int
handled task =
task |> Task.onError (\e -> Task.succeed (e * 10))
init : () -> ( (), Cmd Msg )
init _ =
( (), Cmd.batch [ Task.perform Failed (handled failsLater), Task.perform Succeeded (handled succeedsLater) ] )
update : Msg -> () -> ( (), Cmd Msg )
update msg model =
case msg of
Failed n ->
let
_ =
Debug.log "failed then handled" n
in
( model, Cmd.none )
Succeeded n ->
let
_ =
Debug.log "succeeded through" n
in
( model, Cmd.none )
main : Program () () Msg
main =
Platform.worker
{ init = init
, update = update
, subscriptions = \_ -> Sub.none
}
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_TaskOnErrorInnerTest, 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 the kernel.
| Check | Expected (CHECK) | Lean evaluator | Proved by |
|---|---|---|---|
failed then handled | 70 | 70 | t_elm_core_TaskOnErrorInnerTest |
succeeded through | 5 | 5 | t_elm_core_TaskOnErrorInnerTest |
/-- tests/elm-core/src/TaskOnErrorInnerTest.elm -/
def g_elm_core_TaskOnErrorInnerTest : Program := {
name := "TaskOnErrorInnerTest", source := "tests/elm-core/src",
exposing := (.items [.var "main"]), imports := [⟨"Process", "Process", none⟩, ⟨"Task", "Task", none⟩],
types := [("Msg", [("Failed", 1), ("Succeeded", 1)])],
aliases := [],
defs := [
.fn "failsLater" [] (bin "|>" (ap (qv "Process" "sleep") [.atexp (.scon (.intF 1))]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (ap (qv "Task" "fail") [i 7])))])),
.fn "succeedsLater" [] (bin "|>" (ap (qv "Process" "sleep") [.atexp (.scon (.intF 1))]) (ap (qv "Task" "andThen") [.atexp (.paren (lams [pw] (ap (qv "Task" "succeed") [i 5])))])),
.fn "handled" [pv "task"] (bin "|>" (v "task") (ap (qv "Task" "onError") [.atexp (.paren (lams [pv "e"] (ap (qv "Task" "succeed") [.atexp (.paren (bin "*" (v "e") (i 10)))])))])),
.fn "init" [pw] (tup2 (.atexp .unit) (ap (qv "Cmd" "batch") [listE [ap (qv "Task" "perform") [c "Failed", .atexp (.paren (ap (v "handled") [v "failsLater"]))], ap (qv "Task" "perform") [c "Succeeded", .atexp (.paren (ap (v "handled") [v "succeedsLater"]))]]])),
.fn "update" [pv "msg", pv "model"] (caseOf (v "msg") [(pcon "Failed" [pv "n"], letDefs [.pat pw (ap (qv "Debug" "log") [str "failed then handled", v "n"])] (tup2 (v "model") (qv "Cmd" "none"))), (pcon "Succeeded" [pv "n"], letDefs [.pat pw (ap (qv "Debug" "log") [str "succeeded through", v "n"])] (tup2 (v "model") (qv "Cmd" "none")))]),
.fn "main" [] (ap (qv "Platform" "worker") [recE [("init", v "init"), ("update", v "update"), ("subscriptions", lams [pw] (qv "Sub" "none"))]]) ],
checks := [
chk .check "failed then handled: 70" .int,
chk .check "succeeded through: 5" .int ] }
/-- The worker test of TaskOnErrorInnerTest. -/
def w_elm_core_TaskOnErrorInnerTest : Prog.WorkerTest := {
prog := g_elm_core_TaskOnErrorInnerTest,
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_TaskOnErrorInnerTest 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_TaskOnErrorInnerTest 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_TaskOnErrorInnerTest states that the program has a meaning by r:p-program, with both premises, and ident_elm_core_TaskOnErrorInnerTest that its specialisation is unique and is the reduced program above, both with no hypothesis about principality.