~/bend-docscommunity

core.bend checks

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

1 import
import Base

Types

type Raw source · line 35 · raw

Data

type Schema source · line 48 · raw

Data

type Step source · line 76 · 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 82 · raw

Data

type Err source · line 106 · raw

Data

type Both source · line 1103 · raw

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

Definitions

def pick_raw source · line 111 · raw

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

def lookup source · line 119 · raw

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

The value under name in an object, or RMissing.

def pick_bool source · line 126 · raw

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

def is_rnil source · line 133 · raw

@r:Raw -> Bool

def is_missing source · line 142 · raw

@v:Raw -> Bool

def in_names source · line 149 · raw

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

def none_present source · line 157 · raw

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

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

def key_names source · line 168 · 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 178 · 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 193 · raw

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

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

def is_str source · line 200 · raw

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

def is_tag source · line 208 · raw

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

The tag under k is the string n.

def in_err source · line 225 · raw

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

def str_len_ok source · line 232 · raw

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

def num_ok source · line 235 · raw

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

def len_ok source · line 240 · 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 252 · 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 259 · raw

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

def is_bool source · line 266 · raw

@r:Raw -> Bool

def raw_list source · line 282 · raw

@r:Raw -> Bool

def raw_len source · line 291 · raw

@r:Raw -> Nat

def count_ok source · line 298 · raw

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

def list_len_ok source · line 301 · raw

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

def has_key source · line 330 · raw

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

k is a key of the object r.

def key_once_at source · line 345 · 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 354 · 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 362 · 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 here source · line 428 · raw

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

def under source · line 432 · 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 442 · 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 456 · 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 463 · raw

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

def later_i_path source · line 470 · raw

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

def later_i source · line 479 · raw

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

def later_f_path source · line 487 · raw

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

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

def later_f source · line 494 · raw

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

def first source · line 502 · 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 514 · 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 523 · raw

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

def true_err source · line 530 · raw

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

def enum_err source · line 537 · raw

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

def dup_err source · line 545 · 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 553 · 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 562 · 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 576 · 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 681 · raw

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

def nat_in_defect source · line 691 · 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 bool_defect source · line 702 · raw

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

def no_rule source · line 719 · raw

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

def first_why source · line 722 · raw

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

def step_eq source · line 729 · raw

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

def path_eq source · line 742 · raw

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

def rule_defect source · line 751 · raw

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

def not_list source · line 758 · raw

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

def not_object source · line 765 · raw

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

def nat_defect source · line 772 · raw

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

def str_defect source · line 781 · raw

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

def pick_why source · line 790 · raw

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

def true_defect source · line 797 · raw

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

def enum_defect source · line 808 · raw

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

def no_variant source · line 819 · raw

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

def dup_defect source · line 828 · 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 837 · raw

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

def extra_defect source · line 846 · 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 853 · raw

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

def tag_key_defect source · line 874 · 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 989 · raw

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

def conforms0 source · line 992 · raw

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

def is_chain source · line 1003 · raw

@s:Schema -> Bool

def is_object source · line 1012 · raw

@r:Raw -> Bool

def one_if_there source · line 1021 · raw

@b:Bool -> Nat

def count_present source · line 1028 · raw

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

def tuple_of source · line 1048 · raw

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

def at_raw source · line 1058 · raw

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

def count_unknown source · line 1079 · 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 1106 · raw

@s:Schema -> Data

def enc source · line 1151 · raw

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

def lcons source · line 1206 · raw

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

def both source · line 1213 · raw

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

def opt_some source · line 1220 · raw

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

def map_inl source · line 1227 · raw

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

def map_inr source · line 1234 · raw

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

def pick_m source · line 1241 · raw

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

def true_unit source · line 1248 · raw

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

def nullish source · line 1255 · raw

@r:Raw -> Bool

def dec source · line 1264 · raw

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

def opt_at source · line 1342 · 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 1363 · raw

@s:Schema -> Bool

def no_key source · line 1398 · 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 1425 · 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 1437 · 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 1450 · 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 1465 · 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 1474 · raw

@s:Schema -> Bool

def names_ok source · line 1502 · 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 1548 · 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 371 · raw

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

template check source · line 585 · raw

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

template defect source · line 883 · 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 1035 · raw

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

template each_pos source · line 1067 · raw

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