Some Standard Library
Document Type: Reference
Our goal at this point is to get you familiar with enough of the Sire standard library that you can write a basic cog. As such, this is not an exhaustive reference but an overview of common functions.
Feel free to skim this section for now and come back to it as needed while you work through the rest of the docs.
trk
trk
This is a printf, a console.log
.
trk
prints its first argument. Its second argument is typically the rest of the function we're currently defining. If we were to give trk
a Haskelly type signature, it would be trk : a -> b -> b
and we would say that it prints a
and returns b
. That's why in this example we have to supply it with an (arbitrary) additional argument, the inc
function. trk
feels like it only takes a single argument when it is used in the "middle" of other lines of code, but since it does technically take a second argument, you can't just call it on its own or have it as the final line of a function. Most of the time you will use trk
within other lines and you won't notice this caveat, but you should be aware of it.
=?=
=?=
Assertion. It takes two arguments and exists source file loading if they are not equal, otherwise it evaluates without issue.
Nats
Basic arithmetic
We won't cover all the basic arithmetic functions because they're quite straightforward. Here is a list of the common operations: inc
dec
add
sub
mod
mul
div
pow
.
showNat
showNat
Prints a nat as a string. Mostly used in the REPL.
For more information on Nat representations, read this.
natBar
natBar
Given a nat, returns its bar representation.
Bars
barNat
barNat
Given a bar, returns it as a nat.
barLen
barLen
Given a bar, returns the length of the byte array.
barCat
barCat
Given a row of bars, return their concatenation.
barCatList
barCatList
Similar to barCat
, but operates on a list, rather than a row (note the ~
in ~[]
below).
SOME, "Maybe"
Useful when you're not sure if you're going to have a value.
"Maybes" are represented as a pair of (0 <your-thing>)
. This is called SOME
in sire. For convenience, we also have NONE
, which is really just the nat 0
.
Now we can do some operations on these:
isNone
, isSome
isNone
, isSome
fromSome
, unpackSome
fromSome
, unpackSome
fromSome
returns the value of the SOME
, otherwise returns the first parameter.
unpackSome
also returns the value of the SOME
, but doesn't have a guard - it just crashes if given a NONE
maybeCase
maybeCase
maybeCase
takes a maybe, a guard for NONE
and a function to call on the value in the case of a SOME
.
fmapMaybe
fmapMaybe
fmapMaybe
takes a maybe and a function to call on it, but preserves the maybe form for the return value.
Tabs
A tab is a map from noun to noun. A noun is a nat or a two-tuple. Like a dict in Haskell or Python.
Create a tab: #[]
/ tabFromPairs
#[]
/ tabFromPairs
To create a tab, you can use the tabFromPairs
function or the #[]
syntax.
tabFromPairs
accepts a row of pairs of nats for the keys and values, while #[]
works more like a row of bindings.
In the above example, one
, two
and three
are string keys; 1
2
and 3
are nat values.
❗Caution: While any nat can be used for keys and values, you might run into syntax issues when using the #[]
structure:
This comes down to syntax intricacies that we'll leave as an exercise to you to inspect, if you like. Use the tabFromPairs
approach and you'll be okay.
tabGet
tabGet
Get the value at a key in a tab, return 0
if the key doesn't exist.
tabLookup
tabLookup
Similar to above, but returns maybes when hunting for keys in tabs
A few Row functions
gen
gen
Return a row of n length, generated by function passed. gen
passes the item's indexes in the row to the function.
map
map
Apply a function to all values in a row
weld
weld
Concatenate two rows
Finding additional standard library functions
The entire "standard library" is defined in the consecutively-numbered sire/sire_<n>_<name>.sire
files. If you're trying to complete some task and the functions described above don't help, there's a decent chance there's already a function defined in the standard library that will help.
Do a text-search on the *.sire
files for a term that seems relevant (slice
, sum
, etc.) and look around nearby. Check out the list of imports at the top of the file to see what this file depends on. Consult the list of sire files below and skim the files that might seem useful. When first encountering a file we suggest you skim the list of exports at the bottom (after reading any initial comment at the very top) to get a sense of the functions this file exports. Often, the =?=
test cases can be very helpful.
Rough explanation of the standard library files
Below is a summary of each of the standard library files. Particularly helpful ones for a beginner are annotated with a 👍. Files that require a more advanced understanding and can be skipped for now are annotated with a ❗. Most of these files primarily define lower-level dependencies that other higher-order (and easier to understand) subsequent functions rely on.
sire_01_fan.sire
- Defines named wrappers around PLAN operationssire_02_bit.sire
- Booleanssire_03_nat.sire
- Natural numbers and operating on themsire_04_cmp.sire
- Comparison, ordering and equalitysire_05_row.sire
- Rows and basic operations on them❗
sire_06_rex.sire
- Representation for rex trees - mostly needed for macros.👍
sire_07_dat.sire
- Data structures; rows, lists, maybe, either, etc.👍
sire_10_str.sire
- ASCII characters and stringssire_11_set.sire
- Sets👍
sire_12_tab.sire
- Tabs❗
sire_13_exp.sire
- More rex and macro utilities❗
sire_14_hax.sire
- Explains how the#
rune is used for macros❗
sire_15_pad.sire
- Bit-strings encoded as nats👍
sire_16_bar.sire
- Byte-arrays and operationssire_17_sug.sire
- Syntactic sugar and convenience macros❗
sire_18_pat.sire
- Pattern matching❗
sire_19_bst.sire
- Binary search trees❗
sire_20_prp.sire
- Sire propertiessire_21_switch.sire
- Atomic switch❗
sire_22_seed.sire
- Seed; serialization framework❗
sire_23_repl.sire
- REPL utilities❗
sire_24_rex.sire
- Rexsire_25_datatype.sire
- Datacase/Record⁉️
sire_26_compile.sire
- Backend of the sire compiler⁉️
sire_27_sire.sire
- Sire-in-sire; can be used to bootstrap itself
Taking a look at this list above, you can also get a sense of how the Sire source files start at the basics of wrapping PLAN and incrementally build on each other until the full system is realized. By starting with PLAN and going through all the files above, after a (relatively) small investment of time, you could understand the entirety of this computational model. Pretty cool.
Next we'll introduce some more advanced topics that you'll come across:
Last updated