import std/array.{type Array} import std/io import std/process.{type Pid, type Subject} import std/string import std/wire // A `Wire` dictionary is the type's descriptor (ADR 0072, // docs/wire-format.md): a literal for a monomorphic type, built from // literal pieces and the dictionaries a generic function is given. pub type Msg { Ping(reply: Subject), Stop } pub type Tree { Leaf, Node(Tree, a, Tree) } // No field stores a value of `unit`: descriptors erase it. pub type Tagged { Tagged(Int) } pub type Keyed { Keyed(key: k, values: List) } fn of :: x: a -> String where Wire { wire.describe x } // Generic: `Option<` ... `>` around the given dictionary. fn some :: x: a -> String where Wire { wire.describe (Some(x)) } // Two dictionaries, and one inside another type's instance. fn keyed :: k: k v: v -> String where Wire, Wire { wire.describe (Keyed(key: k, values: [v]), [Some(v)]) } // Generic over generic: `some`'s dictionary is built from this one's. fn nested :: x: a -> String where Wire { some [(x, x)] } pub fn main :: -> () / { io.println (of [1, 2]) let reply: Option<(String, Subject)> = None io.println (of reply) io.println (of (Node(Leaf, 1.5, Leaf))) let t: Tagged = Tagged(1) let u: Tagged ()> = Tagged(2) io.println (of t) io.println (of u) let bytes: Option = None io.println (of ((), True, bytes, (1, "two"))) io.println (some 1) io.println (some "s") io.println (some (Node(Leaf, [Stop], Leaf))) io.println (keyed "k" 1) let b: Tagged = Tagged(3) io.println (keyed (Some(1)) b) io.println (nested 1) io.println (nested [True]) let pids: Array = array.empty () io.println (of pids) // No value at hand: the type comes from `None`'s. let msg: Option = None io.println (wire.describe_none msg) // The table, which every descriptor above resolves against, and its // hash: literals, only in a program that asks for them. let table = wire.definitions () let has = fn entry { io.println (show (string.contains table entry)) } has "Dwire_descriptors.Msg;0CPing;Freply;SiCStop;" has "Dwire_descriptors.Tagged;0CTagged;F;i" has "Dwire_descriptors.Keyed;2CKeyed;Fkey;$0Fvalues;Nstd/prelude.List;1$1" has "Dstd/prelude.Bool;0CFalse;CTrue;" io.println (show (string.length (wire.schema_hash ()))) } //> Nstd/prelude.List;1i //> Nstd/prelude.Option;1T2sSNwire_descriptors.Msg;0 //> Nwire_descriptors.Tree;1f //> Nwire_descriptors.Tagged;0 //> Nwire_descriptors.Tagged;0 //> T4uNstd/prelude.Bool;0Nstd/prelude.Option;1bT2is //> Nstd/prelude.Option;1i //> Nstd/prelude.Option;1s //> Nstd/prelude.Option;1Nwire_descriptors.Tree;1Nstd/prelude.List;1Nwire_descriptors.Msg;0 //> T2Nwire_descriptors.Keyed;2siNstd/prelude.List;1Nstd/prelude.Option;1i //> T2Nwire_descriptors.Keyed;2Nstd/prelude.Option;1iNwire_descriptors.Tagged;0Nstd/prelude.List;1Nstd/prelude.Option;1Nwire_descriptors.Tagged;0 //> Nstd/prelude.Option;1Nstd/prelude.List;1T2ii //> Nstd/prelude.Option;1Nstd/prelude.List;1T2Nstd/prelude.List;1Nstd/prelude.Bool;0Nstd/prelude.List;1Nstd/prelude.Bool;0 //> Ap //> Nwire_descriptors.Msg;0 //> True //> True //> True //> True //> 16