The Flow Book
  1. Why Flow

One week at the support desk

  1. A payment that timed out
  2. What the run was told
  3. Writing the payments library
  4. Waiting for the manager
  5. The next morning
  6. When the process dies mid-call
  7. Fixing a bug in a half-finished run
  8. Three carriers at once
  9. Answering calls from your own service

Projects

  1. What people build
  2. A research agent you can check

The language tour

  1. Values, functions and blocks
  2. Records, choices and match
  3. Modules, libraries and Python functions
  4. Result, ToolProblem and ?

Appendix

  1. The flow command
Language reference ↗

Values, functions and blocks

This part of the Book is for looking things up. The story chapters introduce Flow as the support desk needs it; here the same pieces are laid out one at a time, with a small program for each that you can run without any Tools. If you would rather learn the language before the story, start here and read on in order.

Most of what follows will look familiar. What is missing is less so, and it comes from the rule in Why Flow: everything a run does, apart from its Tool calls, is computed from its input and the answers it was given. When Flow replays a run, or picks one up the next morning, it runs your code again and needs the same values to come out. Values in Flow never change once they are made, and nothing in the language reads a clock, a file or a setting the program wasn't handed.

Where a run starts: main and its inputs

Save this as note.flow:

pub flow main(order: Text, dollars: Int) -> Text =    "Refunded ${dollars} dollars for order ${order}."

A function is declared with flow, takes typed parameters and says what it returns. Its body comes after = and is a single expression; here it is a piece of text with two values put into it by ${…}. pub makes the function visible outside its file, and a run can only start at a pub flow.

Each KEY=VALUE after the file gives one parameter. A Text parameter takes the value as written; any other is written the way Flow writes the value, so dollars=40 is the number 40, and a list is written amounts='[40, 15]':

flow run note.flow order=A-1042 dollars=40run ec7fa1fca16cRefunded 40 dollars for order A-1042.ec7fa1fca16c completednext: flow replay ec7fa1fca16c

The line in the middle is main's result, printed on standard output; a Text result is printed as its own characters, without quotes. The lines around it go to standard error: the run's id as soon as it starts, then how it ended.

An input that doesn't fit main never starts a run. Leave out dollars, or give it dollars=forty, which isn't a number, and the CLI refuses before the program takes a step:

Goes wrong
flow run note.flow order=A-1042Found 1 error error[bad-input]   × missing required input `dollars`  --> input  = parameter `dollars` requires `Int` here   ╭─[note.flow:1:28] 1 │ pub flow main(order: Text, dollars: Int) -> Text =   ·                            ────────────   ╰────   Why  entry `main` requires input field `dollars` of type `Int`; no run was started   Help  Example input shape; replace the sample value: dollars=0. This example shows only `dollars`, not  other required fields.   See the entry's inputs      flow check …/note.flow

The input is the first thing a run's record holds. Even this run, which calls nothing, has one:

flow show ec7fa1fca16crun ec7fa1fca16cstate: completed2 of 2 facts · 0 hidden   1 Program    ec7fa1fca16c ./note.main       order = "A-1042"       dollars = 40   2 End        Completed       value = "Refunded 40 dollars for order A-1042."host activity (3 records; not semantic facts):…

Everything between Start and End was computed from those two values, so there is nothing else to keep. A run that calls Tools has a Call and a Reply in between for each call, as every story chapter shows. The exact rules for the input are in Entry and parameter keys.

Blocks and let

A body that takes more than one step is a block: let lines in braces, then the expression that is the block's value. Save this as reply.flow:

pub flow main(name: Text, dollars: Int) -> Text = {    guard dollars > 0 else { return "Nothing to refund for ${name}." }    let note = "Sorry, ${name}."    let note = note + " We have refunded ${dollars} dollars."    note}
flow run reply.flow name=Dana dollars=40run d3a4a863ea5fSorry, Dana. We have refunded 40 dollars.d3a4a863ea5f completednext: flow replay d3a4a863ea5f

let gives a value a name for the rest of the block. There is no assignment: the second let note makes a new value from the old one and gives it the same name. The first value is still there, unchanged, for anything that already holds it. + joins two pieces of text, or two lists.

guard states what must be true to go on, and its else block has to leave the function. return does that from anywhere in the body, with the value that becomes the function's result:

flow run reply.flow name=Dana dollars=0run 6f70e30d3b04Nothing to refund for Dana.6f70e30d3b04 completednext: flow replay 6f70e30d3b04

Blocks, if and match are expressions with values, so if needs an else whenever its value is used. See blocks, return, if and guard.

Lists, and loops without a loop

Flow has no for or while. You work through a list by handing a function to map, filter or fold, or by writing a function that calls itself. Save this as batch.flow; it totals a day's refunds, unless some of them need the manager:

pub flow main(amounts: List<Int>, limit: Int) -> Text = {    let total = amounts.fold(0, flow(sum, dollars) = sum + dollars)    let large = amounts.filter(flow(dollars) = dollars > limit)    if large.length() > 0 {        "Ask the manager first: ${describe(large)}"    } else {        "Refund ${total} dollars in all"    }}pub flow one(dollars: Int) -> Text = main([dollars], 100)flow describe(amounts: List<Int>) -> Text =    amounts.map(flow(dollars) = "${dollars} dollars").join(", ")
flow run batch.flow amounts='[40, 15, 25]' limit=100run e5d539c876fcRefund 80 dollars in alle5d539c876fc completednext: flow replay e5d539c876fc
flow run batch.flow amounts='[40, 250, 120]' limit=100run e07bc47c02c2Ask the manager first: 250 dollars, 120 dollarse07bc47c02c2 completednext: flow replay e07bc47c02c2

flow(sum, dollars) = sum + dollars is a function without a name. It is a value like any other, so fold can take it and call it once per element, starting from 0. Its parameter types are worked out from where it is used. A named function is a value too, and can be passed the same way.

amounts.fold(…) is the same call as list.fold(amounts, …). Writing a value before the dot calls a function from the module that declares the value's type, with the value as the first argument. List is declared in the standard library's list module, so filter, map, length and join all come from there, and no import is needed for them. The standard library lists every function.

This costs you something if you are used to loops. A total is a fold, not a variable you add to, and a loop that ends on a condition is a function that calls itself until it is done. A call in the last position of a function doesn't use up stack, so a run can go around such a loop as many times as it needs (tail calls).

Helpers and entries

describe has no pub. It is a helper: only batch.flow can call it, and a run can't start there. one is a second pub flow in the same file, and --entry starts a run at it instead of main:

flow run batch.flow --entry one dollars=40run 1ac28c59aef5Refund 40 dollars in all1ac28c59aef5 completednext: flow replay 1ac28c59aef5

Every function that isn't written inside another one spells out its parameter types and its result type, even where Flow could work them out. That makes a function's first line enough to call it by, and a change to its body can't quietly change its type for every caller. Functions can be declared in any order in a file, and a block can declare its own helpers, which see the let names above them (local items).

A parameter can have a default value, as in limit: Int = 100. Parameters with defaults come after the others, and a call either leaves one out or passes it by name: main([40]) or main([40], limit: 50). On the command line, a run started without limit= gets the default too. A default has to be a constant, so it can't read anything the program wasn't handed (default values).

Records, choices and match covers the types you declare yourself, Modules, libraries and Python functions how files and Tool libraries come together, and Result, ToolProblem and ? what a Tool call gives back. Every rule on this page is stated exactly in Evaluation.

Exercises

  1. Run note.flow with note=late after the other two. The CLI refuses that too. Which part of the message tells you why?
  2. Rewrite batch.flow's total without fold: a helper that takes the list and a running sum and calls itself on the rest. You'll need match on a list from the next page, or amounts.get(0) and amounts.take_last(…).
  3. Run flow run batch.flow --entry describe amounts='[40]'. Then add pub to describe and run it again.
  4. Replay any of the runs above with flow replay. None of these programs calls a Tool, so what is there to check?