SCARP v0.0.6-alpha.13 · SOURCE-VERIFIED WORKSHOP

Typed lists, maps and checked results

Eight actual Scarp programs, copied from the published compiler and paired with exact expected output. Read each program, predict its result and try it locally.

Run Scarp on your computer

Use the published compiler build at immutable source commit e755fda37cdaeed29434e848cb5dd41f2f2e7eb2. The website does not execute code in your browser.

python scripts/verify_alpha13_compat.py --compiler /path/to/scarp --compiler-source /path/to/pinned/compiler

Lists and maps are immutable, bounded snapshots. Option / Result and the restricted ? binding propagation were introduced in alpha.12 and are verified against alpha.13 here.

Eight compiler-backed examples

REAL SCARP ALPHA.13 PROGRAM

Typed dynamic lists

Lists of typed records and nested values

typed_dynamic_lists.srp

// Source-level typed dynamic lists: immutable values with a length-independent type.
// Every binding retains a snapshot and list indexing is checked at runtime.
record Task {
    title: string,
    done: boolean,
}

pure funcra first_title(tasks: list<record Task>) -> string {
    return tasks[0]->title;
}

bind one: list<record Task> = [list:
    new Task { title: "Draft", done: false },
    new Task { title: "Review", done: true },
];
bind unchanged = one;

var tasks: list<record Task> = one;
print("First", first_title(tasks));
tasks = [list:
    new Task { title: "Ready", done: true },
    new Task { title: "Review", done: true },
    new Task { title: "Publish", done: false },
];
assert(unchanged[0]->title == "Draft");
print("Updated", first_title(tasks), tasks[2]->title);
bind empty: list<integer> = [list: <integer>];
print("Empty", empty);
bind nested: list<list<integer>> = [list: [list: 1, 2], [list: 3]];
print("Nested", nested[0][1], nested[1][0]);

typed_dynamic_lists.stdout

Exact compiler fixture
First Draft
Updated Ready Publish
Empty [list: <integer>]
Nested 2 3

Run locally: scarp verify tutorials/modern-collections/typed_dynamic_lists.srp then scarp exec tutorials/modern-collections/typed_dynamic_lists.srp. All eight examples ↑

REAL SCARP ALPHA.13 PROGRAM

Persistent list updates

Push, set and remove return new snapshots

persistent_list_updates.srp

// Pure persistent collection operations: no mutation of published snapshots.
record Task { title: string, done: boolean }

pure funcra add_task(tasks: list<record Task>, title: string) -> list<record Task> {
    return list_push(tasks, new Task { title: title, done: false });
}

bind empty: list<record Task> = [list: <record Task>];
bind first = add_task(empty, "Draft");
bind second = add_task(first, "Review");
print("Counts", list_len(empty), list_len(first), list_len(second));
print("First", second[0]->title, second[1]->title);

// Replacing and removing return new lists without affecting their input.
bind reviewed = list_set(second, 1, new Task { title: "Approved", done: true });
bind only_one = list_remove(reviewed, 0);
print("Snapshots", second[1]->title, reviewed[1]->title);
print("Remaining", list_len(only_one), only_one[0]->title);
assert(list_len(empty) == 0);
assert(list_len(first) == 1);
assert(list_len(second) == 2);
assert(only_one[0]->done);

// Length-independent list<T> permits assigning a new complete snapshot.
var active: list<record Task> = first;
active = only_one;
print("Active", active[0]->title);

persistent_list_updates.stdout

Exact compiler fixture
Counts 0 1 2
First Draft Review
Snapshots Review Approved
Remaining 1 Approved
Active Approved

Run locally: scarp verify tutorials/modern-collections/persistent_list_updates.srp then scarp exec tutorials/modern-collections/persistent_list_updates.srp. All eight examples ↑

REAL SCARP ALPHA.13 PROGRAM

Canonical typed maps

Typed keys, nested values and deterministic ordering

typed_maps.srp

// Typed, immutable maps have canonical ordering irrespective of source order.
pure funcra labels() -> map<string, integer> {
    return [map: "z": 9, "a": 1];
}

bind original = labels();
bind reversed = [map: "a": 1, "z": 9];
print(original);
print(original == reversed);
bind empty: map<integer, string> = [map: <integer, string>];
print(empty);
bind features: map<boolean, string> = [map: true: "enabled", false: "disabled"];
print(features);
bind nested: map<integer, list<integer>> = [map:
    2: [list: 1, 2],
    1: [list: 3],
];
print(nested);

typed_maps.stdout

Exact compiler fixture
[map: "a": 1, "z": 9]
true
[map: <integer, string>]
[map: false: "disabled", true: "enabled"]
[map: 1: [list: 3], 2: [list: 1, 2]]

Run locally: scarp verify tutorials/modern-collections/typed_maps.srp then scarp exec tutorials/modern-collections/typed_maps.srp. All eight examples ↑

REAL SCARP ALPHA.13 PROGRAM

Persistent map updates

Set, remove, lookup and preserved snapshots

persistent_maps.srp

// Persistent sorted maps: all updates return new immutable snapshots.
pure funcra replace_alpha(values: map<string, integer>) -> map<string, integer> {
    return map_set(values, "alpha", 12);
}

bind base = [map: "beta": 2, "alpha": 1];
bind changed = map_set(base, "alpha", 10);
bind added = map_set(changed, "gamma", 3);
bind removed = map_remove(added, "beta");
print("Base", base);
print("Updated", added);
print("Removed", removed);
print("Lookup", map_get_or(removed, "alpha", 0), map_get_or(removed, "absent", -1));
print("Contains", map_contains(removed, "alpha"), map_contains(removed, "beta"));
print("Lengths", map_len(base), map_len(removed));
bind same = map_remove(removed, "unknown");
print(same == removed);
print("Pure", replace_alpha(base));

persistent_maps.stdout

Exact compiler fixture
Base [map: "alpha": 1, "beta": 2]
Updated [map: "alpha": 10, "beta": 2, "gamma": 3]
Removed [map: "alpha": 10, "gamma": 3]
Lookup 10 -1
Contains true false
Lengths 2 2
true
Pure [map: "alpha": 12, "beta": 2]

Run locally: scarp verify tutorials/modern-collections/persistent_maps.srp then scarp exec tutorials/modern-collections/persistent_maps.srp. All eight examples ↑

REAL SCARP ALPHA.13 PROGRAM

Ordered map projections

Map keys and values become aligned typed lists

map_projections.srp

// Maps expose their canonical ordering as immutable typed lists.
pure funcra ordered_labels(items: map<string, integer>) -> list<string> {
    return map_keys(items);
}

bind mixed = [map: "z": 9, "a": 1, "m": 5];
bind keys: list<string> = ordered_labels(mixed);
bind values: list<integer> = map_values(mixed);
print(keys);
print(values);
print(keys[0], values[0]);
print(map_len(mixed));

bind none: map<integer, string> = [map: <integer, string>];
print(map_keys(none));
print(map_values(none));

bind boolean_keys = [map: true: "enabled", false: "disabled"];
print(map_keys(boolean_keys));
print(map_values(boolean_keys));

map_projections.stdout

Exact compiler fixture
[list: "a", "m", "z"]
[list: 1, 5, 9]
a 1
3
[list: <integer>]
[list: <string>]
[list: false, true]
[list: "disabled", "enabled"]

Run locally: scarp verify tutorials/modern-collections/map_projections.srp then scarp exec tutorials/modern-collections/map_projections.srp. All eight examples ↑

REAL SCARP ALPHA.13 PROGRAM

Checked Option and Result

Explicit constructors and exhaustive matches

typed_submission.srp

// Concrete option/result specializations use checked, exhaustive variant matches.
// Explicit propagation is a follow-up; every early exit here is a normal return.
record Task { title: string, done: boolean }

pure funcra submit(title: option<string>) -> result<record Task, string> {
    match on title {
        case None { return new result<record Task, string>::Err("title is missing"); }
        case Some(value) {
            if value == "" {
                return new result<record Task, string>::Err("title is required");
            }
            return new result<record Task, string>::Ok(new Task {
                title: value, done: false
            });
        }
    }
}

funcra present(outcome: result<record Task, string>) -> unit {
    match on outcome {
        case Err(message) { print("Rejected", message); }
        case Ok(task) { print("Saved", task->title); }
    }
}

present(submit(new option<string>::Some("Write tests")));
present(submit(new option<string>::Some("")));
present(submit(new option<string>::None()));

typed_submission.stdout

Exact compiler fixture
Saved Write tests
Rejected title is required
Rejected title is missing

Run locally: scarp verify tutorials/modern-collections/typed_submission.srp then scarp exec tutorials/modern-collections/typed_submission.srp. All eight examples ↑

REAL SCARP ALPHA.13 PROGRAM

Explicit failure propagation

Scoped immutable binding initializers with ?

typed_propagation.srp

// Development-only: explicit propagation from immutable bindings.
record Task { title: string }

pure funcra validate_title(value: option<string>) -> result<string, string> {
    match on value {
        case None { return new result<string, string>::Err("title missing"); }
        case Some(title) {
            if title == "" {
                return new result<string, string>::Err("title empty");
            }
            return new result<string, string>::Ok(title);
        }
    }
}

// Failure skips the remaining body and returns Err from this function.
// The successful payload is bound to title, with no unchecked extraction.
pure funcra submit(value: option<string>) -> result<record Task, string> {
    bind title: string = validate_title(value)?;
    return new result<record Task, string>::Ok(new Task { title: title });
}

// Absence also propagates between distinct Option payload types.
pure funcra has_title(value: option<string>) -> option<boolean> {
    bind title = value?;
    return new option<boolean>::Some(title != "");
}

funcra present(outcome: result<record Task, string>) -> unit {
    match on outcome {
        case Ok(task) { print("Saved", task->title); }
        case Err(message) { print("Rejected", message); }
    }
}

present(submit(new option<string>::Some("Review PR")));
present(submit(new option<string>::Some("")));
present(submit(new option<string>::None()));
match on has_title(new option<string>::None()) {
    case None { print("No title"); }
    case Some(value) { print(value); }
}
match on has_title(new option<string>::Some("Ready")) {
    case None { print("No title"); }
    case Some(value) { print(value); }
}

typed_propagation.stdout

Exact compiler fixture
Saved Review PR
Rejected title empty
Rejected title missing
No title
true

Run locally: scarp verify tutorials/modern-collections/typed_propagation.srp then scarp exec tutorials/modern-collections/typed_propagation.srp. All eight examples ↑

REAL SCARP ALPHA.13 PROGRAM

Aggregate snapshots

Independent map projections and boolean key order

aggregate_resource_boundaries.srp

// Persistent map projections and deterministic boolean key order.
bind flags = [map: true: "enabled", false: "disabled"];
bind original_keys = map_keys(flags);
bind original_values = map_values(flags);
bind changed = map_set(flags, false, "off");
print(original_keys);
print(original_values);
print(map_values(changed));
print(map_len(flags), map_len(changed));

aggregate_resource_boundaries.stdout

Exact compiler fixture
[list: false, true]
[list: "disabled", "enabled"]
[list: "off", "enabled"]
2 2

Run locally: scarp verify tutorials/modern-collections/aggregate_resource_boundaries.srp then scarp exec tutorials/modern-collections/aggregate_resource_boundaries.srp. All eight examples ↑

हिन्दी में अभ्यास

हर उदाहरण में असली Scarp कोड और उसका प्रमाणित आउटपुट दिया गया है। पहले आउटपुट का अनुमान लगाएँ, फिर अपने कंप्यूटर पर verify और exec से जाँचें। List और map को बदलने वाले फ़ंक्शन नया snapshot लौटाते हैं; पुराना मान नहीं बदलता।

पूरी हिन्दी और अंग्रेज़ी गाइड ↗

Source copies, stdout fixtures, version and SHA are checked by GitHub Actions. Native C11 code generation, browser execution, networking and asynchronous runtimes are not implied.