Affe

1 program Added 2026-03-17T05:41:02Z Agent: claude-codeModel: claude-sonnet-4-6WebSearch: enabled Evidence Report issue View issues
Aliases: —
Provenance: commit 7b69b84ffe · authored 2026-03-17T06:41:28+01:00 · agent claude-code · model claude-sonnet-4-6

Sources mentioning this language

1 source · not in taxonomy (canonical name didn't match any upstream)
LLM (this repo) · 1

Related languages

Aime (0.20)Ante (0.20)Acute (0.18)AFNIX (0.18)Algae (0.18)

LLM-contributed programs

Affine Channels Example

Provenance: commit 7b69b84ffe · authored 2026-03-17T06:41:28+01:00 · agent claude-code · model claude-sonnet-4-6 · WebSearch enabled
code.affe · added: 2026-03-17T05:41:02Z
# Channels
#
# We use channels as a basic example of the various concepts available in Affe

# Prelude
# Let's assume the existence of strings
type string : un

# Channels are affine.
# This means that a channel can be used at most once.
# It can't be aliased and used by two different functions,
# but it can be "dropped", which means it can stay unused
#
# Affe supports three kinds:
# - `un` : Unrestricted, can be used arbitrarely
# - `aff` : Affine, can be used at most once
# - `lin` : Must be used exactly once
type channel : aff

# We can open a channel at a particular addresse/file/..
val open : string -> channel

# We can close a channel explicitely
val close : channel -> unit

# We can send and receive data from a channel
# Since the channel can not be aliased, this takes the handle
# on the channel, and then return it again
val send : int -> channel -> channel
val receive : channel -> int * channel

# Basic programs on channels

# Open a channel, wait for a message, send it back
let echo_once s =
  let ch = open s in
  let (msg, ch) = receive ch in
  let ch = send msg ch in
  let () = close ch in # This `close` is optional, since channels are affines.
  # let x = send msg ch in # This would fail
  ()

# A common function on channels
let with_channel s f =
  let ch = open s in
  let ch = f ch in
  close ch
  
# The type of `with_channel` is
#    ∀^a. string -> (channel -{^a}> channel) -> unit
# The annotation on the arrow indicate the linearity.
# It's time to study arrows more closely.


# Interlude on arrows
# 
# Let us consider a function that applies a closure on a range of integer

let iter_integers =
  let rec aux f i j =
  match i = j with {
  | True -> ()
  | False ->
    let () = f i in
    aux f (i+1) j
  }
  in
  aux

# This function takes a closure of type `int -{^k}> unit`
# Where `^a` is a *kind* variable that must respect the constraints `^k < unₙ`
#
# Indeed, the closure is called multiple time. if it captured anything linear,
# it could use it multiple time!

# We can try to send multiple integers by uncommenting the following code:

# let send_integers s =
#   let ch = open s in
#   let k msg = let _unused = send msg ch in () in
#   iter_integers k 0 10

# We get an error indicating than the function `k` is `affine`, as it captures
# the channel `ch` which is affine, and thus can not be passed to `iter_integers`

Contribute — propose a file extension

Tell us where to find evidence about Affe (mapped to pl/affe). A reference URL is required; at least one of extension or program code must be provided too. A maintainer reviews each submission via a draft PR before anything lands.
Optional: attach a program from that URL
If the reference URL points at a single source file you'd like to add as an example program, paste it below. The workflow will write it under languages/Affe/programs/<sha>/. Keep under ~200 lines.
(or open the pre-filled issue directly)
← AF Affine Mess →