Learn: Operators
Every min program needs operators to:
- Manipulate elements on the stack
- Perform operations on data
- Provide side effects (read/print to standard input/output/files, etc.)
There are two types of operators: symbols and sigils.
Symbols
Symbols are the most common type of operator. A min symbol is a single word that is either provided by one of the predefined min modules like stack.dup or sys.pwd or defined by the user. User-defined symbols must:
- Start with a letter or an underscore (_).
- Contain zero or more letters, numbers and/or any of the following characters: / ! ? + * _ -
If a symbol contains a dot (.) then it is namespaced by its containing module or dictionary. For example, fs.dirname identifies the {fs.dirname}} operator defined in the {{#link-module symbol. The following min program defines a new symbol called square that duplicates the first element on the stack and multiplies the two elements:
The operator symbol provides way to:
- Specify the name of the symbol operator (square in this case)
- Specify a signature to identify the type of the input and output values (in this case, the operator takes a numeric input value and produces a numeric output value). Also, note how inputs and outputs are captured into the n and result symbols in the signature quotation and then referenced in the body quotation.
- Specify a quotation containing the code that the operator will execute.
Also, symbol operator definitions can be annotated with documentation comments (starting with or wrapped in #|| ... ||#)) so that a help text can be displayed using the help symbol.
Using the lambda operator
Sometimes you just want to bind a piece of code to a symbol to reuse it later, typically something simple and easy-to-read. In these cases, you can use the lambda operator (or the ^ sigil). For example, the previous square operator definition could be rewritten simply as the following.
Note that this feels like using define, but the main difference between lambda and define is that lambda only works on quotations doesn’t auto-quote them, so that they are immediately evaluated when the corresponding symbol is pushed on the stack.
Also note that unlike with operator, symbols defined with lambda:
- have no built-in validation of input and output values.
- do not support the return symbol to immediately end their execution.
- have no built-in stack pollution checks.
Tip
You can use lambda-bind to re-set a previously set lambda.
Sigils
Besides symbols, you can also define sigils. min provides a set of predefined sigils as abbreviations for commonly used symbols.
A sigil can be prepended to a double-quoted string or a single word (with no spaces) which will be treated as a string instead of using the corresponding symbol.
Currently min provides the following sigils:
- '
- Alias for quote.
- :
- Alias for define.
- ?
- Alias for help.
- ~
- Alias for lambda-bind.
- $
- Alias for |get-env.
- @
- Alias for bind.
- ^
- Alias for lambda.
Besides system sigils, you can also create your own sigils. Unlike system sigils however, user defined sigils:
- have the same character restrictions as symbols
- can only be prepended to double-quoted strings
- can be unsealed, deleted, redefined, and sealed.
Sigils can be a very powerful construct and a way to reduce boilerplate code: you can define a sigil to use as you would use any symbol which requires a single string or quoted symbol on the stack.
Like symbols, sigils can be defined with the operator operator, like this:
This definition will add a j sigil that will process the following string as JSON code, so for example:
…will push the following dictionary on the stack:
Also, sigil definitions can be annotated with documentation comments (starting with or wrapped in #|| ... ||#) so that a help text can be displayed using the help symbol.
Auto-popping
Typically, but not always, operators push one or more value to the stack. While this is typically the desired behavior, in some cases you may want to keep the stack clear so in these cases you can append a ! character to any symbol to cause the symbol pop to be pushed on the stack immediately afterwards.
Operator signatures
When defining symbols and sigils with the operator operator, you must specify a signature that will be used to validate and capture input and output values:
In this case for example the square symbol expects a number on the stack, which will be captured to the symbol n and it will place a number on the stack which needs to be bound in the operator body to the symbol result.
In a signature, a type expression must precede the capturing symbol. Such type expression can be:
- One of the following shorthand symbols identifying a well-known min base type (see the reference section for more information): a, bool, null, str, int, num, flt, 'sym, quot, or dict.
- A typed dictionary like dict:module or dict:datastore.
- A type class (see below).
- a type expression like str|int.
Note
If the operator you are defining doesn’t require any input value or doesn’t leave ang output value on the stack, simply don’t put anything before or after the ==> separator, respectively. For example, the signature of the puts! operator could be written like (a ==>).
Type classes
Besides standard base types, you can define your own type classes to express custom constraints/validations for operator input and output values.
Consider the following type class definition validating a quotation containing strings:
The operator operator can be used to define a symbol prefixed with typeclass: (typeclass:strquot in this case) corresponding to a type class that can be used in operator signatures in place of a type, like this:
This operator will raise an error if anything other than a quotation of strings is found on the stack.
Tip
typeclass:-prefixed symbols are just like ordinary symbols: they are lexically scoped, they can be sealed, unsealed and deleted.
Capturing lambdas
You can also specify a lambda to be captured to an output value, like this:
Essentially, this allows you to push a lambda on the stack from an operator.
Note that:
- Lambdas must be captured using the ^ sigil in signatures and bound using lambda-bind in the operator body.
- Lambdas cannot be captured in input values (they have already been pushed on the stack).
- Requiring a lambda as an output value effectively bypasses stack pollution checks. While this can be useful at times, use with caution!
Type expressions
When specifying types in operator signatures or through the expect operator, you can specify a logical expression containing types and type classes joined with one of the following operators:
- | (or)
- & (and)
- ! (not)
Suppose for example you defined the following type classes:
You can combine them in a type expression as following:
Type aliases
As you can see, type expressions can quickly become quite long and complex. To avoid this, you can define type aliases using the typealias operator.
For example, you can create an alias of part of the type expression used in the previous example, like this:
Note that:
- Type aliases be used to create an alias for any type expression.
- Aliased type expressions can contain standard min types, dictionary types, type classes, and even other type aliases.
- The typealias operator actually creates lexically-scoped, typealias:-prefixed symbols that can be sealed, unsealed, and deleted exactly like other symbols.
Generics
min supports generics in operator signatures. in other words, you can define a custom type alias on-the-fly directly in an operator signature, like this:
In this case, t is set to the type union stribg|num|quot, and the add method above can be used to sum two numbers or join two strings or quotations.
Note that the value of t is evaluated to the type of the first value that is processed. In other words, the following programs will work as expected:
while the following will raise an error, because the value of t from num to quot within the same operator use:
Constructors
The operator operator can also be used to create constructor symbols. A constructor is a particular type of operator that is used to create a new typed dictionary.
Consider the following example:
The operator above creates a point constructor symbol that can be used to create a new dict:point typed dictionary by popping two numbers from the stack:
Tip
Except for some native symbols, constructors represent the only way to create new typed dictionaries. The more validations you perform in a constructor, the most effective checking for a specific type using the type? operator will be, as type? only checks if a specific type annotation is present on a typed dictionary, nothing else.
→ Continue to Quotations