~/bend-docscommunity

core.bend checks

raw source on the hub · import bend-schema-lib@0.4.0.0/core.bend as Core

1 import
import Base

Types

type NumberBits source · line 65 · raw

Data

type Json source · line 68 · raw

Data

type JMember source · line 76 · raw

Data

type Int source · line 81 · raw

Data

A whole number of either sign: IPos{n} is n, INeg{n} is -(n+1), so every integer has exactly one form (there is no -0).

type Raw source · line 85 · raw

Data

type Schema source · line 100 · raw

Data

type Step source · line 132 · raw

Data

A step of a path. Positions count what was passed on the way, so that following a path never compares two names: AtIndex{i} is element i of a list, AtField{skip, name} the field skip places after this one in the schema, BoundAt{i, key} the bound of element i of a bounded list. The names are for the host to print.

type Why source · line 138 · raw

Data

type Err source · line 166 · raw

Data

type JKind source · line 493 · raw

Data

type Both source · line 1424 · raw

@A:Data -> @B:Data -> Data

Definitions

def finite_number source · line 171 · raw

@bits:NumberBits -> Bool

JSON numbers use IEEE-754 binary64 bits. The exponent occupies the high word's bits 20 through 30; all ones denotes infinity or NaN.

def valid_json source · line 180 · raw

@value:Json -> Bool

A value is valid when every number in it is finite. Whether a name repeats in an object is not checked: RFC 8259 only says names should be unique, a host's object cannot hold a name twice, and checking it would cost every member a scan of the members after it.

def pick_raw source · line 201 · raw

@b:Bool -> @+x:Raw -> @+y:Raw -> Raw

def lookup source · line 209 · raw

@+name:String -> @r:Raw -> Raw

The value under name in an object, or RMissing.

def pick_bool source · line 216 · raw

@b:Bool -> @+x:Bool -> @+y:Bool -> Bool

def is_rnil source · line 223 · raw

@r:Raw -> Bool

def is_missing source · line 232 · raw

@v:Raw -> Bool

def in_names source · line 239 · raw

@+x:String -> @names:List<&2, String> -> Bool

def none_present source · line 247 · raw

@s:Schema -> @+r:Raw -> Bool

None of the keys of a variant chain is in the object r.

def key_names source · line 258 · raw

@s:Schema -> List<&2, String>

---- a strict object: its keys ----

The names a record or variant chain declares. SStrict refuses a key that is not one of them; wf asks that SStrict wrap such a chain.

def no_extra source · line 268 · raw

@+ns:List<&2, String> -> @r:Raw -> Bool

Every key of the object r is one of ns (anything but an object has none).

def drop_key source · line 283 · raw

@+k:String -> @r:Raw -> Raw

The object r without its first key k (what lookup(k, r) reads).

def is_str source · line 290 · raw

@+n:String -> @v:Raw -> Bool

def is_tag source · line 298 · raw

@+k:String -> @+n:String -> @r:Raw -> Bool

The tag under k is the string n.

def in_err source · line 315 · raw

@b:Bool -> @w:Why -> Maybe<&2, Err>

def str_len_ok source · line 322 · raw

@+lo:Nat -> @+hi:Nat -> @+x:String -> Bool

def num_ok source · line 325 · raw

@+lo:Nat -> @+hi:Nat -> @+n:Nat -> Bool

def int_le source · line 330 · raw

@a:Int -> @b:Int -> Bool

a <= b on integers. A negative is below every non-negative, and between two negatives the larger offset is the smaller number.

def int_ok source · line 341 · raw

@+lo:Int -> @+hi:Int -> @+i:Int -> Bool

def int_raw source · line 345 · raw

@i:Int -> Raw

How an integer is written: a non-negative as RNum, a negative as RNeg.

def len_ok source · line 354 · raw

@+lo:Nat -> @+hi:Nat -> @r:Raw -> Bool

r is a string whose length is in the bounds: what conforms reads at an SStrLen, and what len_err refuses exactly when it fails.

def str_len_in source · line 366 · raw

@+lo:Nat -> @+hi:Nat -> @r:Raw -> Maybe<&2, Err>

The same bounds as ready-made rules: a project's rule calls them by tag (case 0n: str_len_in(1n, 64n, r)), since a tag carries no numbers. Each refuses only the value it is about -- a string, a number -- and leaves any other kind to the schema's own check; the constructors below read the tests directly, because their bound is in the schema.

def nat_in source · line 373 · raw

@+lo:Nat -> @+hi:Nat -> @r:Raw -> Maybe<&2, Err>

def is_bool source · line 380 · raw

@r:Raw -> Bool

def raw_list source · line 396 · raw

@r:Raw -> Bool

def raw_len source · line 405 · raw

@r:Raw -> Nat

def count_ok source · line 412 · raw

@+lo:Nat -> @+hi:Nat -> @+r:Raw -> Bool

def list_len_ok source · line 415 · raw

@+lo:Nat -> @+hi:Nat -> @+r:Raw -> Bool

def has_key source · line 444 · raw

@+k:String -> @r:Raw -> Bool

k is a key of the object r.

def key_once_at source · line 459 · raw

@r:Raw -> @+b:Bool -> @+name:String -> Bool

name appears at most once among the keys of the object r. b is whether a key read before r was already name; the flag carries forward, and a key that is name while the flag is set is the second one -- refused here and nowhere else. The value is the first argument because a self-call must read its arguments left to right, unchanged until one shrinks, and only the value shrinks; the value is matched first because a match cannot be nested inside the branch of a match on another parameter. One pass, so a walk costs one key per key, the same as the lookup beside it.

def key_once source · line 468 · raw

@+name:String -> @r:Raw -> Bool

name appears at most once among the keys of the object r: nothing has been read yet.

def key_err source · line 476 · raw

@b:Bool -> @+name:String -> Maybe<&2, Err>

The repeated key as check reports it: at the key's own name, with no step after it (the second occurrence is not a place in the schema). The test is passed in rather than read here (a match cannot scrutinize a computed value): b is key_once(name, r), so nothing is reported exactly when key_once holds, which is what check_exact needs.

def kind_eq source · line 504 · raw

@a:JKind -> @b:JKind -> Bool

def kind_of source · line 527 · raw

@r:Raw -> JKind

def has_kind source · line 554 · raw

@s:Schema -> @+k:JKind -> Bool

def apart source · line 608 · raw

@+l:Schema -> @+r:Schema -> @+k:JKind -> Bool

No value of kind k is claimed by both.

def disjoint source · line 611 · raw

@+l:Schema -> @+r:Schema -> Bool

def alt_ok source · line 617 · raw

@+s:Schema -> Bool

An alternative is a value that is present and has a kind of its own: not an absent field (the host writes no key, so no kind is there to pick by), and not s.json(), which takes every kind.

def no_alt source · line 621 · raw

@+s:Schema -> Why

What an SEither would have taken, for the host to say.

def here source · line 693 · raw

@w:Why -> Maybe<&2, Err>

def under source · line 697 · raw

@+st:Step -> @m:Maybe<&2, Err> -> Maybe<&2, Err>

An error found under a step gets the step in front of its path.

def len_err source · line 707 · raw

@+lo:Nat -> @+hi:Nat -> @r:Raw -> Maybe<&2, Err>

At an SStrLen the shape comes first, as everywhere else: a value that is not a string is NotString, a string out of its bounds is LengthNotIn, and a node the codec refused to build or that is absent reads as it does at every kind.

def later_l_path source · line 721 · raw

@p:List<&2, Step> -> List<&2, Step>

An error found further along a list is one element further: its first step, if it counts elements, counts one more. A plain list's steps are AtIndex; a bounded list's are AtIndex and BoundAt.

def later_l source · line 728 · raw

@m:Maybe<&2, Err> -> Maybe<&2, Err>

def later_i_path source · line 735 · raw

@p:List<&2, Step> -> List<&2, Step>

def later_i source · line 744 · raw

@m:Maybe<&2, Err> -> Maybe<&2, Err>

def later_f_path source · line 752 · raw

@p:List<&2, Step> -> List<&2, Step>

An error found in a later field is one field further.

def later_f source · line 759 · raw

@m:Maybe<&2, Err> -> Maybe<&2, Err>

def first source · line 767 · raw

@m:Maybe<&2, Err> -> @n:Maybe<&2, Err> -> Maybe<&2, Err>

The first of two: the second counts only when the first found nothing.

def missing_or source · line 779 · raw

@r:Raw -> @w:Why -> Why

The reason a value that is not the shape is refused, at the place it was found: an absent slot is Missing, a node the host refused to build (RTooBig) is TooLarge, anything else is the schema kind's own reason. check and the defect helpers both answer through here, so the reason a path reports is the same one for both.

def pick_err source · line 788 · raw

@b:Bool -> @+x:Maybe<&2, Err> -> @+y:Maybe<&2, Err> -> Maybe<&2, Err>

def true_err source · line 795 · raw

@b:Bool -> Maybe<&2, Err>

def enum_err source · line 802 · raw

@b:Bool -> Maybe<&2, Err>

def dup_err source · line 810 · raw

@s:Schema -> @+r:Raw -> Maybe<&2, Err>

A second key of a variant chain in the object r: the first one found.

def extra_err source · line 818 · raw

@+ns:List<&2, String> -> @r:Raw -> Maybe<&2, Err>

The first key of r that is not one of ns, at its own name.

def tag_err source · line 827 · raw

@+k:String -> @r:Raw -> Maybe<&2, Err>

At the end of a tagged chain no case matched: the value is not an object, or its tag is missing, or it names no case.

def count_err source · line 841 · raw

@+lo:Nat -> @+hi:Nat -> @r:Raw -> Maybe<&2, Err>

The count, as an error: at an SListLen the count is read before any element, so a list out of bounds is CountNotIn at the list's own path. A value that is not a list has no count to object to, and is refused as a list schema refuses it: NotList, or Missing/TooLarge for the two nodes that are never a shape (the same reasons check's own arms give them).

def guard source · line 964 · raw

@b:Bool -> @m:Maybe<&2, Why> -> Maybe<&2, Why>

def nat_in_defect source · line 974 · raw

@+lo:Nat -> @+hi:Nat -> @r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

The same two reasons at an SNatIn and an SBool, replayed along a path: the value's own kind is the defect when the path ends here, and it is not a defect at all further along (the walk passed it).

def int_defect source · line 987 · raw

@r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

The same two reasons at an SInt and an SIntIn: a number of either sign is not the defect, and its bound is the defect only where the path ends.

def int_in_defect source · line 998 · raw

@+lo:Int -> @+hi:Int -> @r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def bool_defect source · line 1013 · raw

@r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def no_rule source · line 1030 · raw

@tag:Nat -> @r:Raw -> Maybe<&2, Err>

def first_why source · line 1033 · raw

@m:Maybe<&2, Why> -> @+n:Maybe<&2, Why> -> Maybe<&2, Why>

def step_eq source · line 1040 · raw

@x:Step -> @y:Step -> Bool

def path_eq source · line 1053 · raw

@x:List<&2, Step> -> @y:List<&2, Step> -> Bool

def rule_defect source · line 1062 · raw

@m:Maybe<&2, Err> -> @+p:List<&2, Step> -> Maybe<&2, Why>

def not_list source · line 1069 · raw

@x:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def not_object source · line 1076 · raw

@x:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def nat_defect source · line 1083 · raw

@r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def str_defect source · line 1092 · raw

@r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def pick_why source · line 1101 · raw

@b:Bool -> @+x:Maybe<&2, Why> -> @+y:Maybe<&2, Why> -> Maybe<&2, Why>

def true_defect source · line 1108 · raw

@r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def enum_defect source · line 1119 · raw

@names:List<&2, String> -> @r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def no_variant source · line 1130 · raw

@p:List<&2, Step> -> Maybe<&2, Why>

def dup_defect source · line 1139 · raw

@s:Schema -> @+r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

The claim about a second key: every key passed is absent, and the key the path names is there.

def at_end source · line 1148 · raw

@w:Why -> @p:List<&2, Step> -> Maybe<&2, Why>

def extra_defect source · line 1157 · raw

@ns:List<&2, String> -> @+r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

At a strict object the path is one key, and the replay checks it on its own: the key is there, and it is not one of the names.

def tag_defect source · line 1164 · raw

@+k:String -> @r:Raw -> @p:List<&2, Step> -> Maybe<&2, Why>

def tag_key_defect source · line 1185 · raw

@b:Bool -> @+k:String -> @p:List<&2, Step> -> Maybe<&2, Why>

The tag key of a tagged case, taken twice, as defect replays it. check reports it before the case's own schema is walked -- there is no place in the schema for the second occurrence -- so the path is one field step at the tag's own name and nothing after it. b is whether the key is not read once, so nothing is reported exactly when key_once holds; a path of any other shape is the case's own report, which the walk below answers.

def check0 source · line 1310 · raw

@s:Schema -> @r:Raw -> Maybe<&2, Err>

def conforms0 source · line 1313 · raw

@s:Schema -> @r:Raw -> Bool

def is_chain source · line 1324 · raw

@s:Schema -> Bool

def is_object source · line 1333 · raw

@r:Raw -> Bool

def one_if_there source · line 1342 · raw

@b:Bool -> Nat

def count_present source · line 1349 · raw

@s:Schema -> @+r:Raw -> Nat

def tuple_of source · line 1369 · raw

@ss:List<&2, Schema> -> Schema

def at_raw source · line 1379 · raw

@r:Raw -> @+i:Nat -> Raw

def count_unknown source · line 1400 · raw

@+ns:List<&2, String> -> @r:Raw -> Nat

---- the claim the strict law makes ----

How many keys of r are not in ns, counted rather than walked with a Bool.and: a second algorithm, so that a no_extra that lets a key through (or refuses a named one) disagrees with it.

def Meaning source · line 1427 · raw

@s:Schema -> Data

def enc source · line 1480 · raw

@s:Schema -> @x:Meaning(s) -> Raw

def lcons source · line 1545 · raw

@-A:Data -> @h:Maybe<&2, A> -> @t:Maybe<&2, List<&2, A>> -> Maybe<&2, List<&2, A>>

def both source · line 1552 · raw

@-A:Data -> @-B:Data -> @p:Maybe<&2, A> -> @q:Maybe<&2, B> -> Maybe<&2, Both<A, B>>

def opt_some source · line 1559 · raw

@-A:Data -> @m:Maybe<&2, A> -> Maybe<&2, Maybe<&2, A>>

def map_inl source · line 1566 · raw

@-A:Data -> @-B:Data -> @m:Maybe<&2, A> -> Maybe<&2, Either<&2, &2, A, B>>

def map_inr source · line 1573 · raw

@-A:Data -> @-B:Data -> @m:Maybe<&2, B> -> Maybe<&2, Either<&2, &2, A, B>>

def pick_m source · line 1580 · raw

@-A:Data -> @b:Bool -> @x:A -> @y:A -> A

def true_unit source · line 1587 · raw

@b:Bool -> Maybe<&2, Unit>

def nullish source · line 1594 · raw

@r:Raw -> Bool

def dec source · line 1603 · raw

@s:Schema -> @r:Raw -> Maybe<&2, Meaning(s)>

def opt_at source · line 1697 · raw

@s:Schema -> Bool

An absent value reaches the top of what enc writes for s: s is an SOptional, or it hands the value it was given straight to a schema that is (a rule, a strict object, a bound, or the tail of a variant or tagged chain -- and an SOpt, whose Some case passes the value through). Everything else wraps what it writes in a constructor of its own, so an absent value cannot surface.

def nullable source · line 1718 · raw

@s:Schema -> Bool

def no_key source · line 1755 · raw

@+k:String -> @s:Schema -> Bool

---- the object a schema's encoding lands in ----

enc writes an object as one flat chain of keys. An SField writes its name and its rest continues that object; an SVariant writes its name and the value under it is a chain of its own (REnd ends it, so it is a value and not a continuation). A wrapper -- a rule, a strict object, a bound, an optional -- writes no key of its own and hands its value straight to its inner schema, so a key reaches the top of the object from inside any of them. A tagged case's schema is the same object the tag key was written into, and so is the rest of its chain.

no_key is that, written down: k does not reach the top of the object enc builds for s, for any value of s. wf asks it of a tagged case's own schema, which is what keeps the tag key from being written twice.

def fresh_f source · line 1784 · raw

@+n:String -> @s:Schema -> Bool

n is not a key of the record chain s (a chain ends in SEnd). Both orders are asked, as fresh_v asks them: which of the two names a step puts first depends on which of them is the value's key there, and asking both spares a proof that String.eq is symmetric.

def fresh_v source · line 1796 · raw

@+n:String -> @s:Schema -> Bool

n is not a key of the variant chain s (a chain ends in SVEnd). Both orders are asked: reading a key compares the names one way, reading past it the other, and asking both spares a proof that String.eq is symmetric.

def is_keyed source · line 1809 · raw

@s:Schema -> Bool

A record chain (ending in SEnd) or a variant chain (ending in SVEnd): what SStrict may wrap, so every key its encoding writes is a declared name. A chain of keys (SField or SVariant links, ending in SEnd or SVEnd): what SStrict may wrap, so every key its encoding writes is a name it declares.

def fresh_t source · line 1824 · raw

@+k:String -> @+n:String -> @s:Schema -> Bool

n names no case of the tagged chain s, whose key is k, and the chain ends in STagEnd{k}: one key for the whole chain.

def wf source · line 1833 · raw

@s:Schema -> Bool

def names_ok source · line 1865 · raw

@s:Schema -> @x:Meaning(s) -> Bool

Every enum value in x is one of its names (encode_conforms' premise).

def bounds_ok source · line 1917 · raw

@s:Schema -> @x:Meaning(s) -> Bool

Every bound a constructor states holds of the value that will be written, at every place one sits in the schema (encode_conforms' second premise, beside names_ok). The encoder writes the meaning unchanged, so a value outside a bound is written out as it is and refused by check on the way back: the host that built it is at fault, and this is where it says so. An SStrLen's bound is read off the string its own schema writes, and an SListLen's count off the list it writes, which is why enc appears here.

Templates

template conforms source · line 624 · raw

@-rule:(@_:Nat -> @_:Raw -> Maybe<&2, Err>) -> @s:Schema -> @r:Raw -> @+prev:Maybe<&2, Nat> -> Bool

template check source · line 850 · raw

@-rule:(@_:Nat -> @_:Raw -> Maybe<&2, Err>) -> @s:Schema -> @r:Raw -> @+prev:Maybe<&2, Nat> -> Maybe<&2, Err>

template defect source · line 1194 · raw

@-rule:(@_:Nat -> @_:Raw -> Maybe<&2, Err>) -> @s:Schema -> @r:Raw -> @+prev:Maybe<&2, Nat> -> @p:List<&2, Step> -> Maybe<&2, Why>

template present_conform source · line 1356 · raw

@-rule:(@_:Nat -> @_:Raw -> Maybe<&2, Err>) -> @+s:Schema -> @+r:Raw -> Bool

template each_pos source · line 1388 · raw

@-rule:(@_:Nat -> @_:Raw -> Maybe<&2, Err>) -> @ss:List<&2, Schema> -> @+r:Raw -> @+i:Nat -> Bool