Standard Library
Dryft provides built-in operations for arithmetic, logic, stack manipulation, and a growing standard library for common tasks.
Built-in Operations
These operations are available in all Dryft programs without imports.
Arithmetic Operations
All arithmetic operations work on Number types:
| Operation | Syntax | Type Signature | Description |
|---|
| Addition | + | Number Number -> Number | Add two numbers |
| Subtraction | - | Number Number -> Number | Subtract second from first |
| Multiplication | * | Number Number -> Number | Multiply two numbers |
| Division | / | Number Number -> Number | Divide second by first |
| Modulo | mod | Number Number -> Number | Remainder after division |
Examples
5 3 + # 8
10 4 - # 6
7 6 * # 42
20 4 / # 5
10 3 mod # 1
Expression Clarity
No precedence rules needed:
# (6/2)(1+2) = 9
6 2 / 1 2 + *
# 6/(2(1+2)) = 1
6 2 1 + 2 * /
Comparison Operations
Comparisons produce Binary (boolean) results:
| Operation | Syntax | Type Signature | Description |
|---|
| Equals | =? or equals? | T T -> Binary | Test equality |
| Not equals | !=? or nequals? | T T -> Binary | Test inequality |
| Greater than | >? or greater? | Number Number -> Binary | First > second |
| Greater or equal | >=? | Number Number -> Binary | First >= second |
| Less than | <? | Number Number -> Binary | First < second |
| Less or equal | =<? | Number Number -> Binary | First <= second |
Examples
5 5 =? # true
4 7 =? # false
10 3 >? # true
5 5 >=? # true
3 8 <? # true
Logical Operations
Logical operations work on Binary types:
| Operation | Syntax | Type Signature | Description |
|---|
| AND | both? | Binary Binary -> Binary | Logical AND |
| OR | either? | Binary Binary -> Binary | Logical OR |
| NOT | not | Binary -> Binary | Logical NOT |
Examples
true true both? # true
true false both? # false
true false either? # true
false false either? # false
true not # false
Bitwise Operations
| Operation | Syntax | Type Signature | Description |
|---|
| XOR | xor | Number Number -> Number | Bitwise exclusive OR |
Example
Stack Manipulation
Direct stack operations for advanced control:
| Operation | Syntax | Type Signature | Description |
|---|
| Copy | copy or ^ | T -> T T | Duplicate top value |
| Drop | drop or v | T -> | Remove top value |
| Swap | swap | T1 T2 -> T2 T1 | Swap top two values |
Examples
5 copy # Stack: [5, 5]
5 copy + # 10 (duplicate and add to itself)
42 drop # Discard value
1 0 swap # Stack: [0, 1]
Standard Library Modules
The standard library is organized into modules that can be included as needed.
The I/O module provides basic printing functionality.
Including
Functions
prints - Print a string
act: prints # Text ->
"Hello, World!\n" prints
printi - Print an integer
act: printi # Number ->
42 printi
"\n" prints
Implementation
The std/io module uses FFI (Foreign Function Interface) to link external functions:
linkin act _x_print_string
linkin act _x_print_int
act prints _x_print_string ;
act printi _x_print_int ;
Complete Example
include: std/io
act: main
"The answer is: " prints
42 printi
"\n" prints
:act
Foreign Function Interface
Dryft can link to external C functions using the linkin keyword.
The FFI is potentially unsafe. Use with caution and ensure external functions match expected signatures.
Syntax
linkin fun external_function_name
linkin act external_action_name
Example
# Link external C function
linkin act _x_print_string
# Create Dryft wrapper
act: prints _x_print_string ;
# Use it
act: main
"Hello from FFI\n" prints
:act
Class Specification
You must specify whether the linked function is a fun (pure) or act (impure):
linkin fun pure_math_function # Pure function
linkin act file_read_operation # Impure action
String Operations
Escape Sequences
Strings support escape sequences:
"\n" # Newline
"\t" # Tab
"\\" # Backslash
"\"" # Double quote
Common Patterns
Print Number with Newline
include: std/io
act: println
printi "\n" prints ;
act: main
42 println
:act
Print Space-Separated Numbers
act: printspace
printi " " prints ;
10 printspace 20 printspace 30 printspace
Divisibility Check
(Int Int -> Bool)
fun: divby
mod 0 =? ;
15 3 divby # true (15 is divisible by 3)
Range Iteration
include: std/io
act: count var: max
0 var: i
cycle:
$i $max =? then: break ;
$i printi "\n" prints
$i 1 + i!
:cycle
:act
act: main 10 count ;
Future Standard Library
The Dryft standard library is under active development. Planned modules include:
- std/math - Advanced mathematical operations
- std/string - String manipulation
- std/file - File I/O operations
- std/collections - Data structures (lists, maps)
- std/memory - Memory management utilities
Building Your Own Library
Create reusable Dryft modules:
mylib/math.dry:
fun: square copy * ;
fun: cube copy copy * * ;
(Int -> Int)
fun: abs var: n
when: $n 0 <? then: $n -1 * ;
$n ;
:fun
main.dry:
include: mylib/math
include: std/io
act: main
-5 abs printi "\n" prints
4 square printi "\n" prints
:act
Summary
Built-in Operations
- Arithmetic:
+, -, *, /, mod
- Comparison:
=?, !=?, >?, >=?, <?, =<?
- Logical:
both?, either?, not
- Stack:
copy, drop, swap
- Bitwise:
xor
Standard Modules
FFI
linkin fun name - Link external pure function
linkin act name - Link external action
Best Practices
- Always
include: std/io for I/O operations
- Use
linkin carefully - it’s potentially unsafe
- Build reusable modules for common functionality
- Document your library functions with type annotations
The standard library provides the foundation for Dryft programs, with more functionality being added as the language evolves.