^ ket · Casts

^- ("kethep"), ^+ ("ketlus"), and ^= ("kettis") let us adjust types without violating type constraints.

The +nest algorithm which tests subtyping is conservative; it never allows invalid nests, it sometimes rejects valid nests.

^| "ketbar"

Convert a gold core to an iron core (contravariant).

Syntax

One argument, fixed.

Tall form
Wide form
Irregular form

^| p

^|(p)

None

AST

[%ktbr p=hoon]

Produces

.p as an iron core; crash if not a gold core.

Discussion

An iron core is an opaque function (gate or door).

Theorem: if type x nests within type a, and type y nests within type b, a core accepting b and producing x nests within a iron core accepting y and producing a.

Informally, a function fits an interface if the function has a more specific result and/or a less specific argument than the interface.

Examples

The prettyprinter shows the core metal (. gold, | iron):


^: "ketcol"

Switch parser into structure mode (mold definition) and produce a gate for type .p. (See , com which toggles modes.)

Syntax

One argument, fixed.

Tall form
Wide form
Irregular form

^: p

^:(p)

,p

AST

Produces

A gate that returns the sample value if it is of the correct type, but crashes otherwise.

Discussion

^: is used to produce a mold that crashes if its sample is of the wrong type.

In structure mode, [a=@ b=@] is a mold for a cell, whereas in value mode it's a pair of molds. Sometimes you need a structure in value mode, in which you can use ^: or ,.

Molds used to produced their bunt value if they couldn't mold their sample. This is no longer the case: molds now crash if molding fails, so this rune is redundant in certain cases.

One may expect that ^:(path /foo) would result in a syntax error since ^: only takes one child, but instead it will parse as =< ^ %:(path /foo). Since : is the irregular syntax for =< this is is parsed as "get ^ (i.e. the mold for cells) from a subject of (path /foo)", with : being the irregular syntax for =<.

Examples


^. "ketdot"

Typecast on value produced by passing .q to .p.

Syntax

Two arguments, fixed.

AST

Expands to

Discussion

.p produces a gate and q is any Hoon expression.

^. is particularly useful when .p is a gate that 'cleans up' the type information about some piece of data. For example, +limo is used to turn a raw noun of the appropriate shape into a genuine list. Hence we can use ^. to cast with +limo and similar gates, ensuring that the product has the desired type.

Examples


^- "kethep"

Typecast by explicit type label.

Syntax

Two arguments, fixed.

AST

Expands to

Discussion

It's a good practice to put a ^- ("kethep") at the top of every arm (including gates, loops, etc). This cast is strictly necessary only in the presence of head recursion (otherwise you'll get a rest-loop error, or if you really screw up spectacularly an infinite loop in the compiler).

Examples

^+ "ketlus"

Typecast by inferred type.

Syntax

Two arguments, fixed.

AST

Produces

The value of .q with the type of .p, if the type of .q nests within the type of .p. Otherwise, nest-fail.

Examples


^& "ketpam"

Convert a core to a zinc core (covariant).

Syntax

One argument, fixed.

Tall form
Wide form
Irregular form

^& p

^&(p)

None

AST

Produces

.p as a zinc core; crash if .p isn't a gold or zinc core.

Discussion

A zinc core has a read-only sample and an opaque context. See Advanced types.

Examples

The prettyprinter shows the core metal in the arm labels 1.xoz and 1&xoz below (. is gold, & is zinc):

You can read from the sample of a zinc core, but not change it:


^~ "ketsig"

Fold constant at compile time.

Syntax

One argument, fixed.

Tall form
Wide form
Irregular form

^~ p

^~(p)

None

AST

Produces

.p, folded as a constant if possible.

Examples


^* "kettar"

Produce example type value.

Syntax

One argument, fixed.

Tall form
Wide form
Irregular form

^* p

^*(p)

*p

.p is any structure expression.

AST

Produces

A default value (i.e., 'bunt value') of the type .p.

Examples

Regular:

Irregular:


^= "kettis"

Bind name to a value.

Syntax

Two arguments, fixed.

AST

Produces

If .p is a term, the product .q with type [%face p q]. .p may also be a tuple of terms, or a term-skin pair; the type of .q must divide evenly into cells to match it.

Examples


^? "ketwut"

Convert any core to a lead core (bivariant).

Syntax

One argument, fixed.

Tall form
Wide form
Irregular form

^? p

^?(p)

None

AST

Produces

.p as a lead core; crash if not a core.

Discussion

A lead core is an opaque generator; the payload can't be read or written.

Theorem: if type x nests within type a, a lead core producing x nests within a lead core producing a.

Informally, a more specific generator can be used as a less specific generator.

Examples

The prettyprinter shows the core metal (. gold, ? lead):

Last updated