Skip to main content

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:
OperationSyntaxType SignatureDescription
Addition+Number Number -> NumberAdd two numbers
Subtraction-Number Number -> NumberSubtract second from first
Multiplication*Number Number -> NumberMultiply two numbers
Division/Number Number -> NumberDivide second by first
ModulomodNumber Number -> NumberRemainder 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:
OperationSyntaxType SignatureDescription
Equals=? or equals?T T -> BinaryTest equality
Not equals!=? or nequals?T T -> BinaryTest inequality
Greater than>? or greater?Number Number -> BinaryFirst > second
Greater or equal>=?Number Number -> BinaryFirst >= second
Less than<?Number Number -> BinaryFirst < second
Less or equal=<?Number Number -> BinaryFirst <= 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:
OperationSyntaxType SignatureDescription
ANDboth?Binary Binary -> BinaryLogical AND
OReither?Binary Binary -> BinaryLogical OR
NOTnotBinary -> BinaryLogical NOT

Examples

true true both?      # true
true false both?     # false
true false either?   # true
false false either?  # false
true not             # false

Bitwise Operations

OperationSyntaxType SignatureDescription
XORxorNumber Number -> NumberBitwise exclusive OR

Example

3 1 xor  # 2

Stack Manipulation

Direct stack operations for advanced control:
OperationSyntaxType SignatureDescription
Copycopy or ^T -> T TDuplicate top value
Dropdrop or vT ->Remove top value
SwapswapT1 T2 -> T2 T1Swap 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.

std/io - Input/Output

The I/O module provides basic printing functionality.

Including

include: std/io

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

include: std/io

act: println
    printi "\n" prints ;

act: main
    42 println
:act
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

  • std/io: prints, printi

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.

Build docs developers (and LLMs) love