leanfran 0.0.0-stage → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,131 @@
1
+ import { S as resolve, o as checkValue } from "./marshal-DhKsxhRw.js";
2
+ // src/schema/arbitrary.ts
3
+ const MAX_DEPTH = 3;
4
+ function bound(f, key) {
5
+ const v = f.bounds[key];
6
+ return v === void 0 ? void 0 : BigInt(v);
7
+ }
8
+ function intRange(fc, lo, hi) {
9
+ if (lo !== void 0 && hi !== void 0) return fc.bigInt({
10
+ min: lo,
11
+ max: hi
12
+ });
13
+ if (lo !== void 0) return fc.bigInt({
14
+ min: lo,
15
+ max: lo + 2n ** 64n
16
+ });
17
+ if (hi !== void 0) return fc.bigInt({
18
+ min: hi - 2n ** 64n,
19
+ max: hi
20
+ });
21
+ return fc.bigInt();
22
+ }
23
+ function fieldArb(fc, f, depth) {
24
+ const d = resolve(f.desc);
25
+ if (d.kind === "nat" || d.kind === "int") {
26
+ const exMin = bound(f, "exclusiveMinimum");
27
+ const exMax = bound(f, "exclusiveMaximum");
28
+ const lo = bound(f, "minimum") ?? (exMin === void 0 ? void 0 : exMin + 1n);
29
+ const hi = bound(f, "maximum") ?? (exMax === void 0 ? void 0 : exMax - 1n);
30
+ return intRange(fc, d.kind === "nat" && (lo === void 0 || lo < 0n) ? 0n : lo, hi);
31
+ }
32
+ return gen(fc, f.desc, depth);
33
+ }
34
+ function variantArb(fc, v, depth, head) {
35
+ const parts = v.fields.map((f) => fieldArb(fc, f, depth).map((x) => [f, x]));
36
+ return fc.tuple(...parts).map((xs) => {
37
+ const out = { ...head };
38
+ for (const [f, x] of xs) if (f.flat && typeof x === "object" && x !== null) Object.assign(out, x);
39
+ else if (!(x === void 0 && f.omitNone)) out[f.key] = x;
40
+ return out;
41
+ });
42
+ }
43
+ function weight(v) {
44
+ return v.fields.filter((f) => {
45
+ const k = resolve(f.desc).kind;
46
+ return k === "struct" || k === "inductive" || k === "list" || k === "array";
47
+ }).length;
48
+ }
49
+ function gen(fc, desc, depth) {
50
+ const d = resolve(desc);
51
+ switch (d.kind) {
52
+ case "nat": return fc.bigInt({
53
+ min: 0n,
54
+ max: 2n ** 64n
55
+ });
56
+ case "int": return fc.bigInt({
57
+ min: -(2n ** 64n),
58
+ max: 2n ** 64n
59
+ });
60
+ case "bool": return fc.boolean();
61
+ case "char": return fc.integer({
62
+ min: 32,
63
+ max: 1114111
64
+ }).filter((c) => c < 55296 || c > 57343).map((c) => String.fromCodePoint(c));
65
+ case "string": return fc.string({
66
+ unit: "grapheme",
67
+ maxLength: 12
68
+ });
69
+ case "float":
70
+ case "float32": return fc.double({ noNaN: true });
71
+ case "unit": return fc.constant(void 0);
72
+ case "uint": return d.bits === 64 ? fc.bigInt({
73
+ min: 0n,
74
+ max: 2n ** 64n - 1n
75
+ }) : fc.integer({
76
+ min: 0,
77
+ max: 2 ** d.bits - 1
78
+ });
79
+ case "sint": return d.bits === 64 ? fc.bigInt({
80
+ min: -(2n ** 63n),
81
+ max: 2n ** 63n - 1n
82
+ }) : fc.integer({
83
+ min: -(2 ** (d.bits - 1)),
84
+ max: 2 ** (d.bits - 1) - 1
85
+ });
86
+ case "usize": return fc.bigInt({
87
+ min: 0n,
88
+ max: 2n ** 64n - 1n
89
+ });
90
+ case "isize": return fc.bigInt({
91
+ min: -(2n ** 63n),
92
+ max: 2n ** 63n - 1n
93
+ });
94
+ case "list":
95
+ case "array": return fc.array(gen(fc, d.item, depth - 1), { maxLength: depth > 0 ? 8 : 0 });
96
+ case "option": {
97
+ const nil = d.absent === "null" ? null : void 0;
98
+ return fc.option(gen(fc, d.item, depth), { nil });
99
+ }
100
+ case "prod": return fc.tuple(gen(fc, d.fst, depth), gen(fc, d.snd, depth));
101
+ case "except": return fc.oneof(gen(fc, d.error, depth).map((error) => ({
102
+ ok: false,
103
+ error
104
+ })), gen(fc, d.value, depth).map((value) => ({
105
+ ok: true,
106
+ value
107
+ })));
108
+ case "fin": {
109
+ const big = fc.bigInt({
110
+ min: 0n,
111
+ max: d.bound - 1n
112
+ });
113
+ return d.bound <= 2n ** 32n ? big.map(Number) : big;
114
+ }
115
+ case "struct": return variantArb(fc, d.variant, depth - 1, {});
116
+ case "enum": return fc.constantFrom(...d.ctors);
117
+ case "inductive": {
118
+ const vs = depth > 0 ? d.variants : d.variants.filter((v) => weight(v) === 0);
119
+ const pool = vs.length === 0 ? d.variants : vs;
120
+ return fc.oneof(...pool.map((v) => variantArb(fc, v, depth - 1, { [d.tagKey]: v.name })));
121
+ }
122
+ case "subtype": return gen(fc, d.base, depth);
123
+ case "model": throw new TypeError(`leanfran: no arbitrary for the user schema ${d.name}`);
124
+ default: return gen(fc, d.get(), depth);
125
+ }
126
+ }
127
+ function arbitrary(schema, fc) {
128
+ const desc = "desc" in schema ? schema.desc : schema;
129
+ return gen(fc, desc, MAX_DEPTH).map((v) => checkValue(desc, v)).filter((r) => "value" in r).map((r) => r.value);
130
+ }
131
+ export { arbitrary as t };
@@ -0,0 +1,37 @@
1
+ // src/runtime/block.ts
2
+ const KEY = Symbol.for("leanfran.blocks");
3
+ function isRegistry(x) {
4
+ return typeof x === "object" && x !== null && Reflect.get(x, "brand") === "leanfran.blocks";
5
+ }
6
+ function blockRegistry() {
7
+ const existing = Reflect.get(globalThis, KEY);
8
+ if (isRegistry(existing)) return existing;
9
+ const created = {
10
+ brand: "leanfran.blocks",
11
+ blocks: /* @__PURE__ */ new Map(),
12
+ models: /* @__PURE__ */ new Map(),
13
+ recording: null
14
+ };
15
+ Reflect.set(globalThis, KEY, created);
16
+ return created;
17
+ }
18
+ function registerBlock(entry) {
19
+ const blocks = blockRegistry().blocks;
20
+ const list = blocks.get(entry.name);
21
+ if (list === void 0) blocks.set(entry.name, [entry]);
22
+ else list.push(entry);
23
+ }
24
+ function leanName(fn, name) {
25
+ Object.defineProperty(fn, "__leanName", { value: name });
26
+ return fn;
27
+ }
28
+ function leanNameOf(fn) {
29
+ const n = typeof fn === "function" ? Reflect.get(fn, "__leanName") : void 0;
30
+ return typeof n === "string" ? n : void 0;
31
+ }
32
+ function tsNumbers(v) {
33
+ if (typeof v === "bigint") return Number(v);
34
+ if (Array.isArray(v)) return v.map((x) => tsNumbers(x));
35
+ return v;
36
+ }
37
+ export { tsNumbers as a, registerBlock as i, leanName as n, leanNameOf as r, blockRegistry as t };