The Native Library Alloy

The Native Library Alloy

Alloy is the native standard library for Clef compilation. It provides BCL-sympathetic APIs that compile to native code with deterministic memory management.

Note: This chapter specifies the Alloy library interface. For implementation details, see the Alloy repository.

Overview

Where .NET F# programs reference FSharp.Core.dll and mscorlib.dll, Clef programs reference Alloy. Alloy provides:

  • Familiar API surface (Console, String, Array, etc.)
  • Native type implementations (fat pointers, stack allocation)
  • BCL-free implementation using CCS intrinsics
  • No garbage collector dependency

Architectural Position

Alloy is Layer 3 in the binding architecture - it is pure F# code that uses CCS intrinsics:

Layer 1: CCS Intrinsics (Sys.write, NativePtr.set, etc.)
    ↑
Layer 2: Binding Libraries (Farscape-generated)
    ↑
Layer 3: User Code (Alloy, applications)  ← Alloy is here

Alloy does NOT declare platform bindings. It uses Sys.* intrinsics directly.

See: Platform Bindings for the three-layer architecture.

Automatically Opened Namespaces

The following namespaces are automatically opened for all Clef code:

open Alloy
open Alloy.Core
open Alloy.Operators
open Alloy.Collections

Basic Types

Type Abbreviations

Type NameNative RepresentationNotes
unitZero-sizedIdentical to standard F#
booli80 = false, 1 = true
intPlatform wordnativeint semantics
int32i32Fixed 32-bit
int64i64Fixed 64-bit
floatf64IEEE 754 double
float32f32IEEE 754 single
chari32UTF-32 codepoint
string{ptr, len}UTF-8 fat pointer

Types with Unit of Measure Support

Type NameDescription
int<'u>Platform integer with unit of measure
int32<'u>32-bit integer with unit of measure
int64<'u>64-bit integer with unit of measure
float<'u>Double with unit of measure
float32<'u>Single with unit of measure

Core Modules

Alloy.Console

Console I/O operations using CCS intrinsics.

module Console =
    /// Write a string to stdout
    val Write : string -> unit

    /// Write a string followed by newline to stdout
    val WriteLine : string -> unit

    /// Read a line from stdin
    val ReadLine : unit -> string

Implementation uses Sys.write and Sys.read intrinsics directly.

Alloy.String

String operations on UTF-8 fat pointers.

module String =
    val length : string -> int
    val isEmpty : string -> bool
    val concat : string -> string -> string
    val slice : int -> int -> string -> voption<string>

Alloy.Array

Array operations on fat pointers.

module Array =
    val length : array<'T> -> int
    val isEmpty : array<'T> -> bool
    val tryItem : int -> array<'T> -> voption<'T>
    val map : ('T -> 'U) -> array<'T> -> array<'U>

Alloy.Option

Option operations with voption semantics.

module Option =
    val isSome : voption<'T> -> bool
    val isNone : voption<'T> -> bool
    val defaultValue : 'T -> voption<'T> -> 'T
    val map : ('T -> 'U) -> voption<'T> -> voption<'U>

Using CCS Intrinsics

Alloy implements I/O using CCS intrinsics directly:

// Alloy/Primitives.fs
module Primitives =
    let inline writeStr (fd: int) (s: string) : int =
        Sys.write fd s.Pointer s.Length

    let inline writeStrOut s = writeStr 1 s
    let inline writeStrErr s = writeStr 2 s

This pattern:

  • Uses Sys.write (Layer 1 intrinsic)
  • No stub declarations with BCL dependencies
  • Pure F# implementation

Operators

Arithmetic Operators

Standard arithmetic operators are defined in Alloy.Operators:

OperatorDescription
+, -, *, /Arithmetic
%Modulo
**Power

Comparison Operators

OperatorDescription
=, <>Equality
<, >, <=, >=Ordering

Pipe Operators

OperatorDescription
|>Forward pipe
<|Backward pipe
>>Forward composition
<<Backward composition

Differences from FSharp.Core

AspectFSharp.CoreAlloy
String encodingUTF-16UTF-8
Option typeReference, nullablevoption, non-nullable
Array headerObject headerFat pointer
Memory managementGCDeterministic
Platform operationsP/InvokeCCS intrinsics

SRTP Resolution

Alloy provides witnesses for statically resolved type parameters. The compiler resolves SRTP constraints against Alloy’s type definitions.

// SRTP constraint resolved against Alloy.Operators
let inline double x = x + x

See: SRTP Resolution for resolution rules.