Files
AILang/examples/std_pair_demo.ailx
T
Brummel 689c445d25 Iter 15f: std_pair — 2-type-var product, no recursion
Fourth stdlib module. Pair<a, b> is the canonical product with two
type vars and a single constructor MkPair. Five combinators: fst,
snd, swap, map_first (Pair<a, b> -> Pair<c, b>), map_second
(Pair<a, b> -> Pair<a, c>).

Smallest dogfood for the parameterised-ADT path so far: no
recursion in the data def or any combinator, single-arm matches
throughout. Demo prints 7, 9, 9, 7, 8, 18 deterministically.

No compiler bug surfaced (fourth stdlib iter in a row to land
clean). The only fixable issue was a paren-balance typo in the
demo's seq chain.

Cumulative: 4 stdlib modules, 24 combinators. Type-system surface
exercised end-to-end now spans 1/2/3-type-var data, 1/2/3-type-var
fns, recursive ADTs, cross-module imports of all of the above,
flat and nested patterns, TCO via monomorphised musttail, GC.

Tests: 93/93 (e2e 33 → 34).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 19:12:23 +02:00

33 lines
1.3 KiB
Plaintext

; Iter 15f — fourth consumer demo. Drives every std_pair combinator
; once. fst/snd are stable at 7/9; swap reverses; map_first/map_second
; demonstrate the Pair<a, b> -> Pair<c, b> / Pair<a, c> reshape with
; b/a held rigid.
(module std_pair_demo
(import std_pair)
(fn inc
(doc "Add 1. Used as the (a -> c) arg.")
(type (fn-type (params (con Int)) (ret (con Int))))
(params x)
(body (app + x 1)))
(fn double
(doc "Multiply by 2. Used as the (b -> c) arg.")
(type (fn-type (params (con Int)) (ret (con Int))))
(params x)
(body (app * x 2)))
(fn main
(doc "Drive every combinator. Expected (per line): 7, 9, 9, 7, 8, 18.")
(type (fn-type (params) (ret (con Unit)) (effects IO)))
(params)
(body
(seq (do io/print_int (app std_pair.fst (term-ctor std_pair.Pair MkPair 7 9)))
(seq (do io/print_int (app std_pair.snd (term-ctor std_pair.Pair MkPair 7 9)))
(seq (do io/print_int (app std_pair.fst (app std_pair.swap (term-ctor std_pair.Pair MkPair 7 9))))
(seq (do io/print_int (app std_pair.snd (app std_pair.swap (term-ctor std_pair.Pair MkPair 7 9))))
(seq (do io/print_int (app std_pair.fst (app std_pair.map_first inc (term-ctor std_pair.Pair MkPair 7 9))))
(do io/print_int (app std_pair.snd (app std_pair.map_second double (term-ctor std_pair.Pair MkPair 7 9))))))))))))