component "Syntax and primitives · Weft manual" on browser
state contents: u1 = 0 public
view opened "Chapter list open" = contents
view closed "Chapter list closed" = not contents
action open_contents "Browse chapters" when not contents
  set contents = 1
  ensures can_close = enabled(close_contents)
end
action close_contents "Hide chapters" when contents
  set contents = 0
  ensures can_open = enabled(open_contents)
end
claim navigation = enabled(open_contents) == shown(closed) and enabled(close_contents) == shown(opened)
page "This is the core language accepted by the C compiler. In syntax forms, uppercase words are placeholders and square brackets mark optional parts. Examples in lowercase are literal Weft."
  a "Skip to chapter" href "#chapter" class "skip"
  header class "bar frame"
    div class "brandgroup"
      a href "/" class "brand" label "Weft home"
        span class "mark"
        span "Weft"
      a "Manual" href "/docs/" class "booklink"
    nav label "Site navigation"
      a "Language home" href "/"
      a "Get the compiler ↗" href "/#download" class "get"
  div class "layout frame"
    aside class "rail"
      div class "railinner"
        p "Language manual" class "railtitle"
        press open_contents when closed class "menubutton openmenu"
        press close_contents when opened class "menubutton"
        nav label "Chapters" class "chapters"
          a "Overview" href "/docs/"
          a "Your first component" href "/docs/start/"
          a "Language design" href "/docs/design/"
          a "Syntax and primitives" href "/docs/language/" class "current"
          a "Proving UI behavior" href "/docs/behavior/"
          a "Proving information flow" href "/docs/information-flow/"
          a "Pages and styling" href "/docs/pages/"
          a "WebAssembly and hosts" href "/docs/wasm/"
          a "Tools and diagnostics" href "/docs/toolchain/"
    main id chapter class "reader"
      p "Language reference" class "kind"
      h1 "Syntax and primitives"
      p "This is the core language accepted by the C compiler. In syntax forms, uppercase words are placeholders and square brackets mark optional parts. Examples in lowercase are literal Weft." class "lead"
      nav label "On this page" class "onpage"
        a "Lexical rules" href "#files"
        a "Declarations" href "#decls"
        a "Statements" href "#statements"
        a "Expressions" href "#expressions"
        a "Contexts" href "#contexts"
      section id files
        h2 "Files, names, and literals"
        a "Download the core and page grammar (EBNF)" href "/docs/core.ebnf"
        p "A source file defines one component. Its header comes first. It needs at least one state field and one action. After the header, declarations can refer to names declared later. Field, view, action, and observer declaration order determines their order in the host interface."
        p "Put each declaration or action statement on one physical line. Blank lines and full lines starting with # after optional whitespace are ignored. A trailing # on a declaration is an unexpected token. Ordinary action indentation is conventional. Page trees use indentation to express structure."
        p "Identifiers start with a lowercase ASCII letter and continue with lowercase letters, digits, or underscores. Use distinct names within each declaration category. The expression names value, true, false, old, shown, enabled, ite, and, or, and not are reserved for named fields, actions, views, and assertions. Labels use double-quoted JSON strings with UTF-8 text and JSON escapes. A NUL is rejected."
        p "Types are u1 through u8. A uN contains unsigned integers from 0 through 2ᴺ − 1. A constant field initializer uses decimal digits. Expression literals also accept 0b binary, 0o octal, and 0x hexadecimal notation and digit separators. Every literal must fit in 0…255. Boolean literals true and false have width one."
        pre
          code "state mode: u3 = 5 public\nview low \"Low bits\" = mode & 0b011\nview busy \"Active\" = mode != 0x00"
      section id decls
        h2 "Component declarations"
        dl
          dt "component \"TITLE\" on OWNER"
          dd "Choose the owner and human-readable title. A browser page requires the owner name browser. Other owner names work with the native host."
          dt "observer PEER"
          dd "Declare a distinct output recipient. It must differ from the owner. A component may have zero observers."
          dt "state FIELD: uN = INITIAL public|private"
          dd "Introduce a field. INITIAL is a decimal value within the declared width, or external for a typed initializer supplied by the owner."
          dt "view NAME \"LABEL\" = EXPR"
          dd "Expose a numeric expression at the owner. A view reads state fields. Its inferred width also applies when a claim refers to it."
          dt "action NAME \"LABEL\" [input uN public|private] when PRED"
          dd "Introduce a guarded event, optionally carrying a typed payload. Close its body with end. The guard must be u1 and can read only state and literals through ordinary expressions."
          dt "claim NAME = PRED"
          dd "Declare a state invariant. This can refer to fields, enabled actions, and shown views."
          dt "channel PEER on ACTION only when PRED"
          dd "Allow a present message to that peer only for the given action and pre-state predicate. At most one channel rule per peer. Peers without this declaration still have their automatic information-flow checks."
          dt "protect FIELD from PEER[, PEER ...] [with input ACTION]"
          dd "Declare one coalition policy for a private field. List distinct declared observers. If present, ACTION must have private input. All listed observers are checked jointly. At most one explicit rule per field."
          dt "page \"DESCRIPTION\" … end · style \"SELECTOR\" … end"
          dd "Add an optional browser page and its style blocks. Element names, attributes, bindings, and breakpoints are listed in the page reference."
        a "Page and style grammar" href "/docs/pages/"
      section id statements
        h2 "Action statements"
        dl
          dt "set FIELD = EXPR"
          dd "Assign once per field per action. All right-hand sides read the pre-state plus the optional value payload. Each assignment must fit the destination width."
          dt "send EXPR to PEER"
          dd "Put a value in that observer’s output slot on an enabled step. Zero is a message. Absence is represented separately."
          dt "split FIELD_OR_VALUE to LEFT, RIGHT"
          dd "Use one fresh mask of the source width. The first peer gets the mask and the second gets its XOR with the source. The source is one field or the action payload value. Use two distinct declared recipients."
          dt "ensures NAME = PRED"
          dd "Declare a postcondition for enabled dispatch. Plain fields, shown, and enabled refer to the resulting state. old(FIELD) names a pre-state field and value still names the supplied payload."
        p "An action may contain zero writes or sends. Each recipient has one output slot per action. Two sends to the same peer, a send overlapping a split destination, or a second split are rejected. Other peers can receive independent sends in that action."
      section id expressions
        h2 "Expressions and operator order"
        dl
          dt "Atoms"
          dd "Use a field identifier, true, false, a numeric literal, or parentheses. value is available inside an action with a payload, including its postconditions."
          dt "ite(test, yes, no)"
          dd "Select a result with a u1 test. Branch widths widen to the larger width. Both branches are pure expressions."
          dt "&   ^   |"
          dd "Bitwise AND, XOR, and OR, from tighter to looser binding. Operands widen without losing bits. The result has the larger operand width."
          dt "==   !=   <   <=   >   >="
          dd "Unsigned comparisons return u1. A single expression cannot chain comparisons. Write (a < b) and (b < c)."
          dt "not   and   or"
          dd "Logical operators require u1 operands and return u1. not binds below comparisons and above and. and binds above or. Write not (mode == 2) when that grouping makes the rule clearer."
          dt "shown(VIEW) · enabled(ACTION)"
          dd "Read the declared view value or the guard bit. These references are allowed in claims, postconditions, and channel conditions. They cannot define a view or action guard recursively."
          dt "old(FIELD)"
          dd "Read the value immediately before the current enabled step. This form takes a field name and is available only in ensures."
        p "From tightest to loosest, ordinary expressions group atoms and calls, &, ^, |, one comparison, not, and, then or. Operators at the same binary bitwise or logical level associate to the left. Parentheses override the grouping."
        p "Literals use the smallest width that represents them, with a minimum of one bit. Assigning a u2 expression to a u4 field is valid. Assigning any u4 expression to a u1 field is rejected, even for wide & 1. Write (wide & 1) != 0 to produce a one-bit predicate. Expression nesting and tree depth are each limited to 1,024."
      section id contexts
        h2 "Choose an expression by context"
        pre
          code "view selected \"Selected\" = vote\nclaim display_matches = shown(selected) == vote\n\naction choose \"Set choice\" input u2 private when not sealed\n  set vote = value\n  ensures stored = vote == value\n  ensures same_phase = sealed == old(sealed)\nend"
        p "This excerpt assumes vote is u2 and sealed is u1. The action guard describes whether the control is available before a payload exists. To accept only some payloads in a transition, express the conditional update with ite in the body. The host validates that the payload fits u2 before dispatch."
      nav label "Adjacent chapters" class "pager"
        a "← Language design" href "/docs/design/"
        a "Proving UI behavior →" href "/docs/behavior/"
  footer class "foot frame"
    a "Weft" href "/" class "brand"
    a "Page source" href "./page.weft"
    a "Build receipt" href "./verification.json"
end
style "*"
  box_sizing "border-box"
end
style "html"
  scroll_padding_top "24px"
end
style "body"
  margin "0"
  background "#f7f5ec"
  color "#20291e"
  font_family "\"DM Sans\", \"Helvetica Neue\", Arial, sans-serif"
  line_height "1.62"
end
style "a"
  color "#2d553a"
  text_underline_offset ".19em"
  text_decoration_thickness "1px"
end
style "a:hover"
  color "#944420"
end
style "a:focus-visible, button:focus-visible"
  outline "3px solid #067685"
  outline_offset "4px"
end
style ".frame"
  width "min(1248px, 91%)"
  margin "0 auto"
end
style ".skip"
  position "absolute"
  top "-90px"
  left "12px"
  background "white"
  padding ".75rem"
  z_index "5"
end
style ".skip:focus"
  top "8px"
end
style ".bar, .brandgroup, .bar nav, .brand, .foot, .pager"
  display "flex"
  align_items "center"
end
style ".bar"
  min_height "78px"
  justify_content "space-between"
  border_bottom "1px solid #bfc4b2"
end
style ".brand, .bar a, .foot a"
  color "inherit"
  text_decoration "none"
end
style ".brand"
  gap ".55rem"
  font_size "1.55rem"
  font_weight "750"
  letter_spacing "-.065em"
end
style ".mark"
  width "26px"
  height "28px"
  background "repeating-linear-gradient(128deg,transparent 0 3px,#263622 3px 6px),repeating-linear-gradient(48deg,transparent 0 5px,#617859 5px 8px)"
  transform "skewY(-6deg)"
end
style ".booklink"
  border_left "1px solid #b9bfac"
  margin_left "1.1rem"
  padding_left "1.1rem"
  font_size "1rem"
end
style ".bar nav"
  font_size ".9rem"
  gap "1.4rem"
end
style ".get"
  border "1px solid #879078"
  border_radius "30px"
  padding ".38rem .85rem"
end
style ".layout"
  display "grid"
  grid_template_columns "minmax(190px, 245px) minmax(0, 1fr)"
  gap "clamp(2.5rem, 6vw, 7rem)"
  align_items "start"
  padding "clamp(2.5rem, 5vw, 5rem) 0 4rem"
end
style ".rail"
  position "sticky"
  top "1.5rem"
  max_height "calc(100vh - 3rem)"
  overflow_y "auto"
end
style ".railtitle, .kind, .codehead"
  font_family "\"IBM Plex Mono\", Menlo, monospace"
  text_transform "uppercase"
  letter_spacing ".095em"
  font_size ".72rem"
  font_weight "650"
  color "#4b6245"
end
style ".railtitle"
  margin "0 0 .9rem .75rem"
end
style ".chapters"
  display "grid"
  gap "3px"
  border_left "1px solid #bcc3af"
end
style ".chapters a"
  padding ".37rem .75rem"
  color "#424f3f"
  font_size ".91rem"
  text_decoration "none"
end
style ".chapters a:hover, .chapters .current"
  color "#203a27"
  background "#e5ead9"
end
style ".chapters .current"
  box_shadow "inset 3px 0 #426348"
  font_weight "700"
end
style ".menubutton"
  display "none"
end
style ".reader"
  max_width "80ch"
  min_width "0"
end
style ".reader h1, .reader h2, .reader h3, .reader p, .reader pre, .reader dl"
  margin "0"
end
style ".reader h1, .reader h2"
  font_family "\"Instrument Serif\", \"Iowan Old Style\", Georgia, serif"
  font_weight "400"
  letter_spacing "-.045em"
end
style ".reader h1"
  font_size "clamp(3.2rem, 5.5vw, 5.65rem)"
  line_height "1.03"
  margin ".6rem 0 1rem"
  text_wrap "balance"
end
style ".reader .lead"
  font_size "clamp(1.1rem, 1.5vw, 1.33rem)"
  color "#475543"
  max_width "65ch"
  line_height "1.5"
end
style ".reader section"
  margin_top "2.3rem"
  border_top "1px solid #c6cbb9"
  padding_top "1.4rem"
end
style ".reader h2"
  font_size "clamp(2rem, 3.3vw, 3rem)"
  line_height "1.12"
  margin_bottom ".8rem"
end
style ".reader h3"
  font_size "1.12rem"
  margin "1.4rem 0 .25rem"
  line_height "1.35"
end
style ".reader p + p, .reader p + a, .reader pre + a"
  margin_top ".75rem"
end
style ".reader p"
  max_width "72ch"
end
style ".reader p + a, .reader pre + a"
  display "inline-block"
end
style ".reader section > a"
  display "table"
  margin ".65rem 0"
end
style ".reader pre"
  padding ".95rem 1.2rem"
  background "#20372e"
  color "#f2f3df"
  border_radius "6px"
  overflow_x "auto"
  line_height "1.6"
  font_size ".87rem"
  margin ".9rem 0"
end
style ".reader code, .reader dt"
  font_family "\"IBM Plex Mono\", Menlo, Consolas, monospace"
end
style ".reader dt"
  font_weight "650"
  font_size ".92rem"
  margin_top "1rem"
  overflow_wrap "anywhere"
  color "#29442d"
end
style ".reader dd"
  margin ".22rem 0 .65rem"
  max_width "72ch"
end
style ".onpage, .pager"
  display "flex"
  flex_wrap "wrap"
  gap ".4rem 1rem"
end
style ".onpage"
  margin_top "1.1rem"
  font_size ".86rem"
end
style ".reader .note"
  background "#e5ead6"
  border_left "3px solid #6a835b"
  padding ".8rem 1rem"
  margin "1rem 0"
  color "#2c402c"
end
style ".routes"
  display "grid"
  grid_template_columns "repeat(2,minmax(0,1fr))"
  gap "0 2.2rem"
end
style ".routes article"
  padding ".85rem 0 1.05rem"
  border_bottom "1px solid #d1d6c6"
end
style ".routes h3"
  margin "0 0 .25rem"
end
style ".routes p"
  font_size ".93rem"
end
style ".pager"
  border_top "1px solid #9aa890"
  padding_top "1.2rem"
  margin_top "2.7rem"
  justify_content "space-between"
  font_size ".94rem"
  font_weight "600"
end
style ".foot"
  gap "1.2rem"
  border_top "1px solid #c8cdbd"
  padding "1rem 0 1.4rem"
  font_size ".85rem"
end
style ".foot .brand"
  margin_right "auto"
  font_size "1.15rem"
end
style ".layout" below 760
  grid_template_columns "minmax(0,1fr)"
  gap "1.25rem"
  padding "1.2rem 0 2.5rem"
end
style ".rail" below 760
  position "static"
  max_height "none"
  overflow "visible"
end
style ".railtitle" below 760
  display "none"
end
style ".menubutton" below 760
  display "inline-block"
  background "#e5ead9"
  color "#263d2b"
  border "1px solid #a4b299"
  border_radius "5px"
  font "inherit"
  min_height "44px"
  padding ".3rem .8rem"
  cursor "pointer"
end
style ".rail:has(.openmenu:enabled:not([hidden])) .chapters" below 760
  display "none"
end
style ".chapters" below 760
  margin_top ".5rem"
  grid_template_columns "repeat(2,minmax(0,1fr))"
end
style ".reader pre" below 760
  font_size ".81rem"
end
style ".bar" below 760
  min_height "62px"
end
style ".bar nav a:first-child" below 500
  display "none"
end
style ".routes" below 500
  grid_template_columns "1fr"
end
style ".frame" below 500
  width "91%"
end
style ".reader h1" below 500
  font_size "clamp(2.8rem,11vw,3.5rem)"
end
style ".foot" below 500
  gap ".9rem"
end
