Bend/builtins.md

167 lines
3.3 KiB
Markdown
Raw Normal View History

2024-05-24 18:41:18 +03:00
>this is a WIP
2024-05-24 17:09:25 +03:00
# Built-in Types and Functions
2024-05-24 18:41:18 +03:00
Bend built-in types and functions, this document serves as a reference guide to these built-in features.
2024-05-24 17:09:25 +03:00
## Types
### String
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
data String = (Cons head ~tail) | (Nil)
```
2024-05-24 17:09:25 +03:00
- **Nil**: Represents an empty string.
- **Cons head ~tail**: Represents a string with a `head` character and a `tail` string.
### List
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
data List = (Cons head ~tail) | (Nil)
```
2024-05-24 17:09:25 +03:00
- **Nil**: Represents an empty list.
- **Cons head ~tail**: Represents a list with a `head` element and a `tail` list.
### Nat
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
data Nat = (Succ ~pred) | (Zero)
```
2024-05-24 17:09:25 +03:00
- **Succ ~pred**: Represents a natural number successor.
- **Zero**: Represents the natural number zero.
### Result
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
data Result = (Ok val) | (Err val)
```
2024-05-24 17:09:25 +03:00
- **Ok val**: Represents a successful result with value `val`.
- **Err val**: Represents an error with value `val`.
### Map
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
data Map = (Node value ~left ~right) | (Leaf)
```
2024-05-24 17:09:25 +03:00
- **Node value ~left ~right**: Represents a map node with a `value` and `left` and `right` subtrees.
- **Leaf**: Represents an empty map.
## Functions
### Map
#### Map/empty
Initializes an empty map.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
Map/empty = Map/Leaf
```
2024-05-24 17:09:25 +03:00
#### Map/get
Retrieves a value from the map based on the key.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
Map/get map key = ...
```
2024-05-24 17:09:25 +03:00
#### Map/set
Sets a value in the map at the specified key.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
Map/set map key value = ...
```
2024-05-24 17:09:25 +03:00
## IO
### IO/Done
Represents a completed IO operation.
### IO/Call
Represents a pending IO operation.
### IO/MAGIC
Returns a magic number used internally.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
def IO/MAGIC: return (0xD0CA11, 0xFF1FF1)
```
2024-05-24 17:09:25 +03:00
### IO/wrap
Wraps a value in an IO/Done.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
def IO/wrap(x): return IO/Done(IO/MAGIC, x)
```
2024-05-24 17:09:25 +03:00
### IO/bind
Chains IO operations.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
def IO/bind(a, b): match a ...
```
2024-05-24 17:09:25 +03:00
### call
Calls an IO function with an argument.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
def call(func, argm): return IO/Call(IO/MAGIC, func, argm, lambda x: IO/Done(IO/MAGIC, x))
```
2024-05-24 17:09:25 +03:00
### print
Prints text to the console.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
print text = (IO/Call IO/MAGIC "PUT_TEXT" text @x (IO/Done IO/MAGIC x))
```
2024-05-24 17:33:39 +03:00
# Usage Examples
## Map
2024-05-24 18:19:33 +03:00
### Map/empty
Initializes an empty map.
2024-05-24 18:23:32 +03:00
```bend
2024-05-24 18:41:18 +03:00
Map/empty = Map/Leaf
```
2024-05-24 18:19:33 +03:00
2024-05-24 17:33:39 +03:00
### Map/get
2024-05-24 18:19:33 +03:00
Retrieves a `value` from the `map` based on the `key`.
2024-05-24 17:33:39 +03:00
2024-05-24 18:23:32 +03:00
```bend
Map/get map key =
2024-05-24 17:33:39 +03:00
match map {
Map/Leaf: (*, map)
Map/Node:
switch _ = (== 0 key) {
0: switch _ = (% key 2) {
0:
let (got, rest) = (Map/get map.left (/ key 2))
(got, (Map/Node map.value rest map.right))
_:
let (got, rest) = (Map/get map.right (/ key 2))
(got, (Map/Node map.value map.left rest))
}
_: (map.value, map)
}
2024-05-24 18:19:33 +03:00
}
```
2024-05-24 17:33:39 +03:00
### Map/set
2024-05-24 18:19:33 +03:00
Sets a `value` in the `map` at the specified `key`.
2024-05-24 17:33:39 +03:00
2024-05-24 18:23:32 +03:00
```bend
Map/set map key value =
2024-05-24 17:33:39 +03:00
match map {
Map/Node:
switch _ = (== 0 key) {
0: switch _ = (% key 2) {
0: (Map/Node map.value (Map/set map.left (/ key 2) value) map.right)
_: (Map/Node map.value map.left (Map/set map.right (/ key 2) value))
}
_: (Map/Node value map.left map.right)
}
Map/Leaf:
switch _ = (== 0 key) {
0: switch _ = (% key 2) {
0: (Map/Node * (Map/set Map/Leaf (/ key 2) value) Map/Leaf)
_: (Map/Node * Map/Leaf (Map/set Map/Leaf (/ key 2) value))
}
_: (Map/Node value Map/Leaf Map/Leaf)
}
2024-05-24 18:19:33 +03:00
}
```
2024-05-24 17:33:39 +03:00