11 Il Libro di Ridgun
La lib list — Liste, Mergesort $O(N \log N)$ e Mappe Chiave-Valore
11.1. Perché esiste la lib list? (Il problema reale e la filosofia Ridgun)
«Perché ho bisogno di una lib
listscritta da zero in Ridgun puro invece di collegare una libreria C tradizionale?»
Questa è la prima domanda che si pone qualsiasi ingegnere di sistema arrivando a list. E la risposta riassume l'intera filosofia di ridgun-libs:
Siamo sinceri: accedere a un indice fuori intervallo (IndexError / panic!) o mutare per errore una lista condivisa sono due degli errori più frequenti nell'elaborazione delle collezioni.
La lib list fornisce indicizzazione con valore predefinito, slicing (take, drop, slice), intervalli con passo, deduplicazione stabile, ordinamento mergesort $O(N \log N)$ per [Int] e [String] e dizionari associativi immutabili [String String] (kv get, kv put, kv merge).
In un ecosistema tradizionale, risolvere i compiti del dominio LIST comporta trascinare dipendenze esterne in C, gestire puntatori grezzi, conversioni opache di tipo, eccezioni non verificate o pause imprevedibili del garbage collector. In Ridgun, la lib list (src/main/ridgun/lib/list.rg) affronta il problema con quattro principi non negoziabili:
- Dichiara
import list(oimport list as alias) all'inizio del file: ogni lib definisce il proprio spazio dei nomimodule :: liste in modalità--strictle funzioni multi-parola si invocano sempre con spazi naturali. - Concatena le tue pipeline da sinistra a destra con
->: scriviinput passo uno -> passo due -> risultato, usa le maschere(+ _ _ +)per destrutturare le sequenze e ometti_ =nelle istruzioni sequenziali (o scarta il valore con-> ()). - Ricorsione di coda con TCO (Tail-Call Optimization): tutte le scansioni interne di
listvengono compilate in cicli nativi in spazio di stack $O(1)$. - Memoria deterministica (
RMM1/RMM2): ogni struttura intermedia viene liberata nell'istante esatto in cui termina la sua ultima lettura, senza Garbage Collector.
11.2. Artefatti nativi, importazione e catalogo completo dell'API di list
In ridgun-libs, list è una lib di prima classe situata in src/main/ridgun/lib/list.rg (dominio LIST). Compilando con stable bundle, viene impacchettata in tre artefatti nativi:
| Artefatto | Percorso | Scopo |
|---|---|---|
| Bundle Ridgun | target/bundles/list.rglib | Distribuzione e importazione diretta con import list |
| File compilato | target/list.rgc | Artefatto intermedio verificato da ridgunc |
| Oggetto condiviso ELF64 | target/liblist.so | Libreria dinamica nativa senza dipendenze C né libc |
Ogni lib dichiara il proprio spazio dei nomi con
module :: list: basta scrivereimport list(oimport list as alias) nell'intestazione e invocare le sue funzioni con spazi naturali (mia funzione x) e pipeline->(x mia funzione -> risultato).
11.2.1. Riepilogo delle famiglie funzionali di list
| Firma / Funzione / Tipo | Cosa fa e quando usarla |
|---|---|
list nth or / list str nth or | Lettura sicura per indice che restituisce default_val se fuori intervallo |
list take / list drop / list slice / list reverse | Prefisso, suffisso, sottointervallo [start, end) e inversione ricorsiva di coda |
list range / list range step | Genera sequenze aritmetiche (step == 0 restituisce [] in modo sicuro) |
list sort / list str sort / list dedup / list str dedup | Ordinamento mergesort $O(N \log N)$ stabile e deduplicazione per prima occorrenza |
kv empty / kv put / kv get / kv get or / kv remove / kv merge | Operazioni su dizionari associativi ordinati [String String] |
11.2.2. Catalogo esaustivo dei tipi e delle firme in src/main/ridgun/lib/list.rg
Di seguito trovi l'inventario completo dei tipi algebrici (type), degli operatori (op ::) e delle firme di funzione (fn ::) definiti in src/main/ridgun/lib/list.rg (0 tipi, 0 operatori e 59 funzioni):
| # | Funzione / Firma (fn ::) | Tipo di Ritorno |
|---|---|---|
1 | fn :: list len [Int] | Int |
2 | fn :: list str len [String] | Int |
3 | fn :: list is empty [Int] | Bool |
4 | fn :: list str is empty [String] | Bool |
5 | fn :: list nth or [Int] Int Int | Int |
6 | fn :: list nth [Int] Int | Int |
7 | fn :: list str nth or [String] Int String | String |
8 | fn :: list str nth [String] Int | String |
9 | fn :: list head [Int] Int | Int |
10 | fn :: list last [Int] Int | Int |
11 | fn :: list take [Int] Int | [Int] |
12 | fn :: list str take [String] Int | [String] |
13 | fn :: list drop [Int] Int | [Int] |
14 | fn :: list str drop [String] Int | [String] |
15 | fn :: l clamp nonneg Int | Int |
16 | fn :: l slice ints from [Int] Int Int | [Int] |
17 | fn :: list slice [Int] Int Int | [Int] |
18 | fn :: l slice strs from [String] Int Int | [String] |
19 | fn :: list str slice [String] Int Int | [String] |
20 | fn :: l rev ints [Int] [Int] | [Int] |
21 | fn :: list reverse [Int] | [Int] |
22 | fn :: l rev strs [String] [String] | [String] |
23 | fn :: list str reverse [String] | [String] |
24 | fn :: list range step Int Int Int | [Int] |
25 | fn :: list range Int Int | [Int] |
26 | fn :: list replicate Int Int | [Int] |
27 | fn :: l idx int [Int] Int Int | Int |
28 | fn :: list index of [Int] Int | Int |
29 | fn :: list contains [Int] Int | Bool |
30 | fn :: l idx str [String] String Int | Int |
31 | fn :: list str index of [String] String | Int |
32 | fn :: list str contains [String] String | Bool |
33 | fn :: list count [Int] Int | Int |
34 | fn :: list str count [String] String | Int |
35 | fn :: l dedup ints [Int] [Int] | [Int] |
36 | fn :: list dedup [Int] | [Int] |
37 | fn :: l dedup strs [String] [String] | [String] |
38 | fn :: list str dedup [String] | [String] |
39 | fn :: l merge ints [Int] [Int] | [Int] |
40 | fn :: l sort ints len [Int] Int | [Int] |
41 | fn :: list sort [Int] | [Int] |
42 | fn :: list is sorted [Int] | Bool |
43 | fn :: l str le loop String String Int Int Int | Bool |
44 | fn :: l str le String String | Bool |
45 | fn :: l merge strs [String] [String] | [String] |
46 | fn :: l sort strs len [String] Int | [String] |
47 | fn :: list str sort [String] | [String] |
48 | fn :: l kv key (String String) | String |
49 | fn :: l kv val (String String) | String |
50 | fn :: list kv empty | [String String] |
51 | fn :: list kv len [String String] | Int |
52 | fn :: list kv has [String String] String | Bool |
53 | fn :: list kv get or [String String] String String | String |
54 | fn :: list kv get [String String] String | String |
55 | fn :: list kv put [String String] String String | [String String] |
56 | fn :: list kv remove [String String] String | [String String] |
57 | fn :: list kv keys [String String] | [String] |
58 | fn :: list kv values [String String] | [String] |
59 | fn :: list kv merge [String String] [String String] | [String String] |
11.3. Esplorare list a fondo: Esempi passo dopo passo di ogni funzionalità
Non basta consultare la tabella delle firme: esploreremo ciascuna delle funzionalità della lib list tramite esempi progressivi ed eseguibili in Ridgun. Inizieremo con due ricette tabulari di uso quotidiano e poi percorreremo nel dettaglio tutti gli scenari funzionali, le trasformazioni e i casi limite verificati in src/test/ridgun/test list.rg.
11.3.1. Esempio 1: Flusso principale in stile tabulare con list
Osserva come in Ridgun i segni = e i passaggi di trasformazione con list risultino allineati come una tabella pulita, senza variabili mutabili superflue né effetti collaterali nascosti:
import list
fn :: top tres unicos [Int] => [Int]
top tres unicos puntos =
puntos list dedup -> list sort -> list reverse -> ordenados
list take ordenados 3
11.3.2. Esempio 2: Composizione sicura e gestione dei casi limite
Ogni funzione della lib list è progettata per comporsi con le guardie booleane | e con il resto delle 34 lib di ridgun-libs:
import list
fn :: configurar entorno [String String] => String [String]
configurar entorno base =
kv put base "host" "0.0.0.0" -> cfg1
kv put cfg1 "port" "8080" -> cfg2
kv get or cfg2 "host" "127.0.0.1" -> host
cfg2 kv keys -> claves
(host, claves)
11.3.3. Esempio 3: Indicizzazione sicura (nth or, head or, last or), slicing (take, drop, slice), intervalli (range, iota, replicate) e deduplicazione (dedup, count eq)
In questo esempio pratico esploriamo in modo esaustivo come si comporta list in questo blocco funzionale, inclusi input standard, combinazioni di parametri e valori di frontiera:
fn :: test list indexing slicing and ranges => ()
test list indexing slicing and ranges =
xs = [10, 20, 30, 40, 50]
ss = ["aa", "bb", "cc", "dd"]
assert int equals (list len xs) 5 "AC1: list_len 5"
assert int equals (list str len ss) 4 "AC1: list_str_len 4"
assert true (list is empty []) "AC1: list_is_empty []"
assert false (list is empty xs) "AC1: list_is_empty non-empty"
assert true (list str is empty []) "AC1: list_str_is_empty []"
assert false (list str is empty ss) "AC1: list_str_is_empty non-empty"
assert int equals (list nth xs 0) 10 "AC1: list_nth 0"
assert int equals (list nth xs 4) 50 "AC1: list_nth 4"
assert int equals (list nth or xs 99 (0 - 1)) (0 - 1) "AC1: list_nth_or out of bounds"
assert int equals (list nth or xs (0 - 1) (0 - 1)) (0 - 1) "AC1: list_nth_or negative"
assert str eq (list str nth ss 2) "cc" "AC1: list_str_nth 2"
assert str eq (list str nth or ss 99 "oob") "oob" "AC1: list_str_nth_or out of bounds"
assert int equals (list head xs 0) 10 "AC1: list_head"
assert int equals (list head [] 99) 99 "AC1: list_head empty fallback"
assert int equals (list last xs 0) 50 "AC1: list_last"
assert int equals (list last [] 99) 99 "AC1: list_last empty fallback"
assert eq (list take xs 3) [10, 20, 30]
assert eq (list take xs 0) []
assert eq (list take xs (0 - 2)) []
assert eq (list take xs 99) [10, 20, 30, 40, 50]
assert eq (list drop xs 3) [40, 50]
assert eq (list drop xs 0) xs
assert eq (list drop xs 99) []
assert eq (list slice xs 1 4) [20, 30, 40]
assert eq (list slice xs (0 - 3) 2) [10, 20]
assert eq (list slice xs 4 2) []
assert eq (list str slice ss 1 3) ["bb", "cc"]
assert eq (list reverse xs) [50, 40, 30, 20, 10]
assert eq (list str reverse ss) ["dd", "cc", "bb", "aa"]
assert eq (list range 1 5) [1, 2, 3, 4, 5]
assert eq (list range 3 3) [3]
assert eq (list range 5 1) []
assert eq (list range step 0 6 2) [0, 2, 4, 6]
assert eq (list range step 6 0 (0 - 2)) [6, 4, 2, 0]
assert eq (list range step 1 5 0) []
assert eq (list replicate 3 7) [7, 7, 7]
assert eq (list replicate 0 7) []
assert true (list contains xs 30) "AC1: list_contains 30"
assert false (list contains xs 99) "AC1: list_contains 99"
assert true (list str contains ss "cc") "AC1: list_str_contains cc"
assert false (list str contains ss "zz") "AC1: list_str_contains zz"
assert int equals (list index of xs 30) 2 "AC1: list_index_of 30"
assert int equals (list index of xs 99) (0 - 1) "AC1: list_index_of missing"
assert int equals (list str index of ss "dd") 3 "AC1: list_str_index_of dd"
assert int equals (list count [1, 2, 1, 3, 1] 1) 3 "AC1: list_count"
assert int equals (list str count ["a", "b", "a"] "a") 2 "AC1: list_str_count"
assert eq (list dedup [3, 1, 3, 2, 1, 3]) [3, 1, 2]
assert eq (list str dedup ["b", "a", "b", "c", "a"]) ["b", "a", "c"]
()
11.3.4. Esempio 4: Ordinamento deterministico $O(N \log N)$ (list sort int, list sort desc, list sort str, list is sorted)
In questo esempio pratico esploriamo in modo esaustivo come si comporta list in questo blocco funzionale, inclusi input standard, combinazioni di parametri e valori di frontiera:
fn :: test list mergesort => ()
test list mergesort =
unsorted = [42, 7, 19, 7, 0, (0 - 5), 100, 3, (0 - 50)]
sorted = list sort unsorted
assert eq sorted [(0 - 50), (0 - 5), 0, 3, 7, 7, 19, 42, 100]
assert true (list is sorted sorted) "AC2: list_is_sorted after sort"
assert false (list is sorted unsorted) "AC2: list_is_sorted false on unsorted"
assert true (list is sorted []) "AC2: empty list is sorted"
assert true (list is sorted [1]) "AC2: singleton list is sorted"
assert eq (list sort []) []
assert eq (list sort [1]) [1]
assert eq (list sort [5, 4, 3, 2, 1]) [1, 2, 3, 4, 5]
s_strs = list str sort ["delta", "alpha", "charlie", "bravo", "alpha", "al", ""]
assert eq s_strs ["", "al", "alpha", "alpha", "bravo", "charlie", "delta"]
()
11.3.5. Esempio 5: Mappe associative chiave-valore immutabili (kv put, kv get or, kv has, kv remove, kv keys, kv values, kv len)
In questo esempio pratico esploriamo in modo esaustivo come si comporta list in questo blocco funzionale, inclusi input standard, combinazioni di parametri e valori di frontiera:
fn :: test kv associative map => ()
test kv associative map =
m0 = kv empty
assert int equals (kv len m0) 0 "AC3: kv_empty len 0"
m1 = kv put m0 "host" "127.0.0.1"
m2 = kv put m1 "port" "8080"
m3 = kv put m2 "port" "9090"
assert int equals (kv len m3) 2 "AC3: kv_put updates in-place without duplicating key"
assert true (kv has m3 "host") "AC3: kv_has host"
assert false (kv has m3 "missing") "AC3: kv_has missing"
assert str eq (kv get m3 "port") "9090" "AC3: kv_get updated port"
assert str eq (kv get m3 "missing") "" "AC3: kv_get missing returns empty"
assert str eq (kv get or m3 "mode" "prod") "prod" "AC3: kv_get_or default"
assert eq (kv keys m3) ["host", "port"]
assert eq (kv values m3) ["127.0.0.1", "9090"]
m4 = kv merge m3 [("port", "8443"), ("tls", "true")]
assert int equals (kv len m4) 3 "AC3: kv_merge len 3"
assert str eq (kv get m4 "port") "8443" "AC3: kv_merge overrides port"
assert str eq (kv get m4 "tls") "true" "AC3: kv_merge adds tls"
m5 = kv remove m4 "host"
assert false (kv has m5 "host") "AC3: kv_remove removes host"
assert int equals (kv len m5) 2 "AC3: kv_remove len 2"
()
11.4. Sotto il cofano: come è costruita list in Ridgun puro (src/main/ridgun/lib/list.rg)
Uno dei maggiori vantaggi di ridgun-libs è che non ci sono scatole nere in C. Puoi aprire src/main/ridgun/lib/list.rg e leggere esattamente come è implementata ogni operazione usando pattern matching, alberi di guardie | / |. e ricorsione di coda (TCO). Vediamo i pezzi chiave della sua implementazione reale:
11.4.1. Implementazione interna: List Length & Emptiness
Nota come list.rg strutturi prima le firme fn :: ... => ... e risolva ogni caso tramite equazioni allineate e chiamate di coda:
module :: list
fn :: list len [Int] => Int
list len [] = 0
list len (_:rest) = 1 + (list len rest)
fn :: list str len [String] => Int
list str len [] = 0
list str len (_:rest) = 1 + (list str len rest)
fn :: list is empty [Int] => Bool
list is empty [] = True
list is empty _ = False
fn :: list str is empty [String] => Bool
list str is empty [] = True
list str is empty _ = False
11.4.2. Implementazione interna: Slicing (take, drop, slice) & Reversal
Nota come list.rg strutturi prima le firme fn :: ... => ... e risolva ogni caso tramite equazioni allineate e chiamate di coda:
fn :: list take [Int] Int => [Int]
list take [] _ = []
list take (x:rest) n =
| n <= 0 = []
| otherwise = [x] :: (list take rest (n - 1))
fn :: list str take [String] Int => [String]
list str take [] _ = []
list str take (x:rest) n =
| n <= 0 = []
| otherwise = [x] :: (list str take rest (n - 1))
fn :: list drop [Int] Int => [Int]
list drop [] _ = []
list drop (x:rest) n =
| n <= 0 = [x] :: rest
| otherwise = list drop rest (n - 1)
fn :: list str drop [String] Int => [String]
list str drop [] _ = []
list str drop (x:rest) n =
| n <= 0 = [x] :: rest
| otherwise = list str drop rest (n - 1)
fn :: l clamp nonneg Int => Int
l clamp nonneg x =
| x < 0 = 0
| otherwise = x
fn :: l slice ints from [Int] Int Int => [Int]
l slice ints from xs s0 end_idx =
| end_idx <= s0 = []
| otherwise = list take (list drop xs s0) (end_idx - s0)
fn :: list slice [Int] Int Int => [Int]
list slice xs start end_idx =
start l clamp nonneg -> s0
l slice ints from xs s0 end_idx
fn :: l slice strs from [String] Int Int => [String]
l slice strs from xs s0 end_idx =
| end_idx <= s0 = []
| otherwise = list str take (list str drop xs s0) (end_idx - s0)
fn :: list str slice [String] Int Int => [String]
list str slice xs start end_idx =
start l clamp nonneg -> s0
l slice strs from xs s0 end_idx
fn :: l rev ints [Int] [Int] => [Int]
l rev ints [] acc = acc
l rev ints (x:rest) acc = l rev ints rest ([x] :: acc)
fn :: list reverse [Int] => [Int]
list reverse xs = l rev ints xs []
fn :: l rev strs [String] [String] => [String]
l rev strs [] acc = acc
l rev strs (x:rest) acc = l rev strs rest ([x] :: acc)
fn :: list str reverse [String] => [String]
list str reverse xs = l rev strs xs []
11.4.3. Implementazione interna: Search, Count & Deduplication
Nota come list.rg strutturi prima le firme fn :: ... => ... e risolva ogni caso tramite equazioni allineate e chiamate di coda:
fn :: l idx int [Int] Int Int => Int
l idx int [] _ _ = 0 - 1
l idx int (x:rest) tgt idx =
| x == tgt = idx
| otherwise = l idx int rest tgt (idx + 1)
fn :: list index of [Int] Int => Int
list index of xs tgt = l idx int xs tgt 0
fn :: list contains [Int] Int => Bool
list contains xs tgt = (list index of xs tgt) >= 0
fn :: l idx str [String] String Int => Int
l idx str [] _ _ = 0 - 1
l idx str (x:rest) tgt idx =
| x == tgt = idx
| otherwise = l idx str rest tgt (idx + 1)
fn :: list str index of [String] String => Int
list str index of xs tgt = l idx str xs tgt 0
fn :: list str contains [String] String => Bool
list str contains xs tgt = (list str index of xs tgt) >= 0
fn :: list count [Int] Int => Int
list count [] _ = 0
list count (x:rest) tgt =
| x == tgt = 1 + (list count rest tgt)
| otherwise = list count rest tgt
fn :: list str count [String] String => Int
list str count [] _ = 0
list str count (x:rest) tgt =
| x == tgt = 1 + (list str count rest tgt)
| otherwise = list str count rest tgt
fn :: l dedup ints [Int] [Int] => [Int]
l dedup ints [] seen = list reverse seen
l dedup ints (x:rest) seen =
| list contains seen x = l dedup ints rest seen
| otherwise = l dedup ints rest ([x] :: seen)
fn :: list dedup [Int] => [Int]
list dedup xs = l dedup ints xs []
fn :: l dedup strs [String] [String] => [String]
l dedup strs [] seen = list str reverse seen
l dedup strs (x:rest) seen =
| list str contains seen x = l dedup strs rest seen
| otherwise = l dedup strs rest ([x] :: seen)
fn :: list str dedup [String] => [String]
list str dedup xs = l dedup strs xs []
11.4.4. Implementazione interna: Associative Key-Value Map ([(String, String)])
Nota come list.rg strutturi prima le firme fn :: ... => ... e risolva ogni caso tramite equazioni allineate e chiamate di coda:
fn :: l kv key (String String) => String
l kv key (k, _) = k
fn :: l kv val (String String) => String
l kv val (_, v) = v
fn :: kv empty => [String String]
kv empty = []
fn :: kv len [String String] => Int
kv len [] = 0
kv len (_:rest) = 1 + (kv len rest)
fn :: kv has [String String] String => Bool
kv has [] _ = False
kv has (pair:rest) key =
| (l kv key pair) == key = True
| otherwise = kv has rest key
fn :: kv get or [String String] String String => String
kv get or [] _ fallback = fallback
kv get or (pair:rest) key fallback =
| (l kv key pair) == key = l kv val pair
| otherwise = kv get or rest key fallback
fn :: kv get [String String] String => String
kv get pairs key = kv get or pairs key ""
fn :: kv put [String String] String String => [String String]
kv put [] key val = [(key, val)]
kv put (pair:rest) key val =
| (l kv key pair) == key = [(key, val)] :: rest
| otherwise = [pair] :: (kv put rest key val)
fn :: kv remove [String String] String => [String String]
kv remove [] _ = []
kv remove (pair:rest) key =
| (l kv key pair) == key = kv remove rest key
| otherwise = [pair] :: (kv remove rest key)
fn :: kv keys [String String] => [String]
kv keys [] = []
kv keys (pair:rest) = [(l kv key pair)] :: (kv keys rest)
fn :: kv values [String String] => [String]
kv values [] = []
kv values (pair:rest) = [(l kv val pair)] :: (kv values rest)
11.5. Garanzie algoritmiche, memoria RMM1/RMM2 e le 5 Regole d'Oro di list
- Garanzia algoritmica e di sicurezza: Ordinamento deterministico $O(N \log N)$ nel caso peggiore (senza l'$O(N^2)$ patologico di Quicksort) e conservazione dell'ordine di inserimento nelle mappe
kv. - Complessità dello stack $O(1)$ con TCO: Tutti i cicli ricorsivi in
src/main/ridgun/lib/list.rgsono in posizione di coda, perciòridguncli compila in salti nativi senza crescita dello stack. - Memoria deterministica (
RMM1/RMM2): Zero pause del garbage collector; tutte le strutture intermedie vengono liberate in modo deterministico all'uscita dallo scope. - Verifica continua: Testata in modo esaustivo con
stable checke la suite automatizzatasrc/test/ridgun/test list.rg.
Le 5 Regole d'Oro quando programmi con list
- Dichiara
import listall'inizio del file e usa le funzioni multi-parola con spazi naturali affinché il tuo codice si legga come prosa. - Allinea le tue pipeline di dati con
=e->: concatena le chiamate alistin passaggi con nomi chiari e lascia chestable formatallinei le colonne. - Sfrutta le funzioni totali di
listnelle tue guardie|: controlla i valori di ritorno o destruttura gli ADT con pattern matching invece di dare per scontato che l'input esterno sia sempre valido. - Evita di copiare grandi strutture se devi solo consultare un campo: ricorda che in
RMM2gli argomenti di input sono prestati e non incrementano né decrementano i contatori (+0/-0). - Combina
listcontest(import test): scrivi sempre asserzioni conassert equalsoassert trueper fissare gli invarianti del tuo dominio.
Nel Capitolo 12 passeremo a esplorare in profondità la lib btree (La lib btree — Alberi AVL Persistenti $O(\log N)$, Insiemi e Heap).