@panproto/core 0.11.0 → 0.13.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,63 @@
1
+ import { WasmModule, Expr, Literal } from './types.js';
2
+ /**
3
+ * Parse expression source text into an AST node.
4
+ *
5
+ * Accepts Haskell-style expression syntax (lambdas, let bindings, pattern
6
+ * matches, record literals, etc.) and returns the corresponding {@link Expr}
7
+ * AST node.
8
+ *
9
+ * @param source - The expression source text
10
+ * @param wasm - The WASM module
11
+ * @returns The parsed expression AST
12
+ * @throws {@link WasmError} if the source text contains a syntax error
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const expr = parseExpr('\\x -> x + 1', panproto._wasm);
17
+ * // => { type: 'lam', param: 'x', body: { type: 'builtin', op: 'Add', ... } }
18
+ * ```
19
+ */
20
+ export declare function parseExpr(source: string, wasm: WasmModule): Expr;
21
+ /**
22
+ * Evaluate an expression with an optional environment.
23
+ *
24
+ * Reduces the expression to a literal value. Free variables in the
25
+ * expression are resolved from the provided environment map.
26
+ *
27
+ * @param expr - The expression to evaluate
28
+ * @param env - Optional mapping of variable names to literal values
29
+ * @param wasm - The WASM module
30
+ * @returns The evaluated literal value
31
+ * @throws {@link WasmError} if evaluation fails (e.g., unbound variable, type error)
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const expr = ExprBuilder.add(
36
+ * ExprBuilder.var_('x'),
37
+ * ExprBuilder.lit({ type: 'int', value: 1 }),
38
+ * );
39
+ * const result = evalExpr(expr, { x: { type: 'int', value: 41 } }, panproto._wasm);
40
+ * // => { type: 'int', value: 42 }
41
+ * ```
42
+ */
43
+ export declare function evalExpr(expr: Expr, env: Record<string, Literal> | undefined, wasm: WasmModule): Literal;
44
+ /**
45
+ * Parse and pretty-print an expression (round-trip formatting).
46
+ *
47
+ * Parses the source text and converts the resulting AST back to a
48
+ * canonical string representation. Useful for normalizing expression
49
+ * formatting.
50
+ *
51
+ * @param source - The expression source text
52
+ * @param wasm - The WASM module
53
+ * @returns The canonically formatted expression string
54
+ * @throws {@link WasmError} if the source text contains a syntax error
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const formatted = formatExpr('\\x->x + 1', panproto._wasm);
59
+ * // => '\\x -> x + 1'
60
+ * ```
61
+ */
62
+ export declare function formatExpr(source: string, wasm: WasmModule): string;
63
+ //# sourceMappingURL=expr-parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expr-parser.d.ts","sourceRoot":"","sources":["../src/expr-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAW,MAAM,YAAY,CAAC;AAIrE;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI,CAUhE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,QAAQ,CACtB,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EACxC,IAAI,EAAE,UAAU,GACf,OAAO,CAYT;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CAInE"}
@@ -0,0 +1,43 @@
1
+ import { WasmModule } from './types.js';
2
+ /** Node IDs in a fiber (preimage of migration at a target anchor). */
3
+ export type Fiber = number[];
4
+ /** Fiber decomposition: all fibers keyed by target anchor. */
5
+ export type FiberDecomposition = Record<string, number[]>;
6
+ /**
7
+ * Compute the fiber of a migration at a specific target anchor.
8
+ *
9
+ * Returns all source node IDs whose remapped anchor equals the target.
10
+ *
11
+ * @param instance - MessagePack-encoded instance bytes
12
+ * @param migration - MessagePack-encoded migration bytes
13
+ * @param targetAnchor - The target anchor to compute the fiber at
14
+ * @param wasm - The WASM module
15
+ * @returns Array of source node IDs in the fiber
16
+ * @throws {@link WasmError} if the fiber computation fails
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const fiber = fiberAt(instanceBytes, migrationBytes, 'post', panproto._wasm);
21
+ * // => [0, 3, 7]
22
+ * ```
23
+ */
24
+ export declare function fiberAt(instance: Uint8Array, migration: Uint8Array, targetAnchor: string, wasm: WasmModule): Fiber;
25
+ /**
26
+ * Compute fibers for ALL target anchors simultaneously.
27
+ *
28
+ * Returns a map from target anchor to source node IDs.
29
+ *
30
+ * @param instance - MessagePack-encoded instance bytes
31
+ * @param migration - MessagePack-encoded migration bytes
32
+ * @param wasm - The WASM module
33
+ * @returns A record mapping each target anchor to its fiber node IDs
34
+ * @throws {@link WasmError} if the decomposition fails
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const decomposition = fiberDecomposition(instanceBytes, migrationBytes, panproto._wasm);
39
+ * // => { post: [0, 3], comment: [1, 2, 5] }
40
+ * ```
41
+ */
42
+ export declare function fiberDecomposition(instance: Uint8Array, migration: Uint8Array, wasm: WasmModule): FiberDecomposition;
43
+ //# sourceMappingURL=fiber.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fiber.d.ts","sourceRoot":"","sources":["../src/fiber.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAI7C,sEAAsE;AACtE,MAAM,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC;AAE7B,8DAA8D;AAC9D,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;AAE1D;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CACrB,QAAQ,EAAE,UAAU,EACpB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,UAAU,GACf,KAAK,CAUP;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,UAAU,EACpB,SAAS,EAAE,UAAU,EACrB,IAAI,EAAE,UAAU,GACf,kBAAkB,CAUpB"}
@@ -0,0 +1,53 @@
1
+ import { WasmModule } from './types.js';
2
+ /** An edge in the lens graph. */
3
+ export interface GraphEdge {
4
+ readonly source: string;
5
+ readonly target: string;
6
+ readonly chain: Uint8Array;
7
+ }
8
+ /** Result of a preferred path query. */
9
+ export interface PreferredPath {
10
+ readonly cost: number;
11
+ readonly steps: string[];
12
+ }
13
+ /**
14
+ * Find the minimum-cost conversion path between two schemas.
15
+ *
16
+ * Returns null if no path exists.
17
+ *
18
+ * @param edges - The edges in the lens graph
19
+ * @param sourceSchema - The source schema identifier
20
+ * @param targetSchema - The target schema identifier
21
+ * @param wasm - The WASM module
22
+ * @returns The preferred path, or null if unreachable
23
+ * @throws {@link WasmError} if the computation fails for reasons other than no path
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const path = preferredPath(edges, 'schema_v1', 'schema_v3', panproto._wasm);
28
+ * if (path) {
29
+ * console.log(`Cost: ${path.cost}, Steps: ${path.steps}`);
30
+ * }
31
+ * ```
32
+ */
33
+ export declare function preferredPath(edges: GraphEdge[], sourceSchema: string, targetSchema: string, wasm: WasmModule): PreferredPath | null;
34
+ /**
35
+ * Get the distance (minimum conversion cost) between two schemas.
36
+ *
37
+ * Returns Infinity if no path exists.
38
+ *
39
+ * @param edges - The edges in the lens graph
40
+ * @param sourceSchema - The source schema identifier
41
+ * @param targetSchema - The target schema identifier
42
+ * @param wasm - The WASM module
43
+ * @returns The minimum conversion cost, or Infinity if unreachable
44
+ * @throws {@link WasmError} if the computation fails
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const d = distance(edges, 'schema_v1', 'schema_v3', panproto._wasm);
49
+ * // => 2.0
50
+ * ```
51
+ */
52
+ export declare function distance(edges: GraphEdge[], sourceSchema: string, targetSchema: string, wasm: WasmModule): number;
53
+ //# sourceMappingURL=graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../src/graph.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAI7C,iCAAiC;AACjC,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC5B;AAED,wCAAwC;AACxC,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,SAAS,EAAE,EAClB,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,UAAU,GACf,aAAa,GAAG,IAAI,CAYtB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CACtB,KAAK,EAAE,SAAS,EAAE,EAClB,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,UAAU,GACf,MAAM,CAUR"}
package/dist/hom.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { WasmModule } from './types.js';
2
+ /**
3
+ * Construct the internal hom schema [S, T].
4
+ *
5
+ * Returns a schema whose instances represent lenses from S to T.
6
+ *
7
+ * @param sourceSchema - MessagePack-encoded source schema bytes
8
+ * @param targetSchema - MessagePack-encoded target schema bytes
9
+ * @param wasm - The WASM module
10
+ * @returns MessagePack-encoded hom schema bytes
11
+ * @throws {@link WasmError} if construction fails
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const homSchema = polyHom(sourceBytes, targetBytes, panproto._wasm);
16
+ * ```
17
+ */
18
+ export declare function polyHom(sourceSchema: Uint8Array, targetSchema: Uint8Array, wasm: WasmModule): Uint8Array;
19
+ //# sourceMappingURL=hom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hom.d.ts","sourceRoot":"","sources":["../src/hom.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG7C;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,OAAO,CACrB,YAAY,EAAE,UAAU,EACxB,YAAY,EAAE,UAAU,EACxB,IAAI,EAAE,UAAU,GACf,UAAU,CASZ"}
package/dist/index.cjs CHANGED
@@ -122,7 +122,16 @@ async function loadWasm(input) {
122
122
  check_dataset_staleness: glue.check_dataset_staleness,
123
123
  store_protocol_definition: glue.store_protocol_definition,
124
124
  get_protocol_definition: glue.get_protocol_definition,
125
- get_migration_complement: glue.get_migration_complement
125
+ get_migration_complement: glue.get_migration_complement,
126
+ parse_expr: glue.parse_expr,
127
+ eval_func_expr: glue.eval_func_expr,
128
+ execute_query: glue.execute_query,
129
+ // Phase 12: Fiber, hom, and graph
130
+ fiber_at: glue.fiber_at,
131
+ fiber_decomposition_wasm: glue.fiber_decomposition_wasm,
132
+ poly_hom: glue.poly_hom,
133
+ preferred_conversion_path: glue.preferred_conversion_path,
134
+ conversion_distance: glue.conversion_distance
126
135
  };
127
136
  const memory = initOutput.memory;
128
137
  if (!memory) {
@@ -2838,6 +2847,178 @@ class ExprBuilder {
2838
2847
  return ExprBuilder.builtin("Concat", a, b);
2839
2848
  }
2840
2849
  }
2850
+ function parseExpr(source, wasm) {
2851
+ try {
2852
+ const resultBytes = wasm.exports.parse_expr(source);
2853
+ return unpackFromWasm(resultBytes);
2854
+ } catch (error) {
2855
+ throw new WasmError(
2856
+ `Failed to parse expression: ${error instanceof Error ? error.message : String(error)}`,
2857
+ { cause: error }
2858
+ );
2859
+ }
2860
+ }
2861
+ function evalExpr(expr, env, wasm) {
2862
+ try {
2863
+ const exprBytes = packToWasm(expr);
2864
+ const envBytes = packToWasm(env ?? {});
2865
+ const resultBytes = wasm.exports.eval_func_expr(exprBytes, envBytes);
2866
+ return unpackFromWasm(resultBytes);
2867
+ } catch (error) {
2868
+ throw new WasmError(
2869
+ `Failed to evaluate expression: ${error instanceof Error ? error.message : String(error)}`,
2870
+ { cause: error }
2871
+ );
2872
+ }
2873
+ }
2874
+ function formatExpr(source, wasm) {
2875
+ const expr = parseExpr(source, wasm);
2876
+ return exprToString(expr);
2877
+ }
2878
+ function exprToString(expr) {
2879
+ switch (expr.type) {
2880
+ case "var":
2881
+ return expr.name;
2882
+ case "lit":
2883
+ return literalToString(expr.value);
2884
+ case "lam":
2885
+ return `\\${expr.param} -> ${exprToString(expr.body)}`;
2886
+ case "app":
2887
+ return `(${exprToString(expr.func)} ${exprToString(expr.arg)})`;
2888
+ case "let":
2889
+ return `let ${expr.name} = ${exprToString(expr.value)} in ${exprToString(expr.body)}`;
2890
+ case "field":
2891
+ return `${exprToString(expr.expr)}.${expr.name}`;
2892
+ case "record": {
2893
+ const fields = expr.fields.map(([k, v]) => `${k} = ${exprToString(v)}`).join(", ");
2894
+ return `{ ${fields} }`;
2895
+ }
2896
+ case "list": {
2897
+ const items = expr.items.map(exprToString).join(", ");
2898
+ return `[${items}]`;
2899
+ }
2900
+ case "index":
2901
+ return `${exprToString(expr.expr)}[${exprToString(expr.index)}]`;
2902
+ case "match": {
2903
+ const arms = expr.arms.map(([pat, body]) => `${patternToString(pat)} -> ${exprToString(body)}`).join("; ");
2904
+ return `match ${exprToString(expr.scrutinee)} { ${arms} }`;
2905
+ }
2906
+ case "builtin": {
2907
+ const args = expr.args.map(exprToString).join(", ");
2908
+ return `${expr.op}(${args})`;
2909
+ }
2910
+ }
2911
+ }
2912
+ function literalToString(lit) {
2913
+ switch (lit.type) {
2914
+ case "bool":
2915
+ return String(lit.value);
2916
+ case "int":
2917
+ return String(lit.value);
2918
+ case "float":
2919
+ return String(lit.value);
2920
+ case "str":
2921
+ return JSON.stringify(lit.value);
2922
+ case "bytes":
2923
+ return `<bytes:${lit.value.length}>`;
2924
+ case "null":
2925
+ return "null";
2926
+ case "record": {
2927
+ const fields = lit.fields.map(([k, v]) => `${k} = ${literalToString(v)}`).join(", ");
2928
+ return `{ ${fields} }`;
2929
+ }
2930
+ case "list": {
2931
+ const items = lit.items.map(literalToString).join(", ");
2932
+ return `[${items}]`;
2933
+ }
2934
+ }
2935
+ }
2936
+ function patternToString(pat) {
2937
+ switch (pat.type) {
2938
+ case "wildcard":
2939
+ return "_";
2940
+ case "var":
2941
+ return pat.name;
2942
+ case "lit":
2943
+ return literalToString(pat.value);
2944
+ case "record": {
2945
+ const fields = pat.fields.map(([k, v]) => `${k} = ${patternToString(v)}`).join(", ");
2946
+ return `{ ${fields} }`;
2947
+ }
2948
+ case "list": {
2949
+ const items = pat.items.map(patternToString).join(", ");
2950
+ return `[${items}]`;
2951
+ }
2952
+ }
2953
+ }
2954
+ function executeQuery(query, instance, wasm) {
2955
+ try {
2956
+ const queryBytes = packToWasm(query);
2957
+ const resultBytes = wasm.exports.execute_query(queryBytes, instance._bytes);
2958
+ return unpackFromWasm(resultBytes);
2959
+ } catch (error) {
2960
+ throw new WasmError(
2961
+ `Failed to execute query: ${error instanceof Error ? error.message : String(error)}`,
2962
+ { cause: error }
2963
+ );
2964
+ }
2965
+ }
2966
+ function fiberAt(instance, migration, targetAnchor, wasm) {
2967
+ try {
2968
+ const resultBytes = wasm.exports.fiber_at(instance, migration, targetAnchor);
2969
+ return unpackFromWasm(resultBytes);
2970
+ } catch (error) {
2971
+ throw new WasmError(
2972
+ `Failed to compute fiber at "${targetAnchor}": ${error instanceof Error ? error.message : String(error)}`,
2973
+ { cause: error }
2974
+ );
2975
+ }
2976
+ }
2977
+ function fiberDecomposition(instance, migration, wasm) {
2978
+ try {
2979
+ const resultBytes = wasm.exports.fiber_decomposition_wasm(instance, migration);
2980
+ return unpackFromWasm(resultBytes);
2981
+ } catch (error) {
2982
+ throw new WasmError(
2983
+ `Failed to compute fiber decomposition: ${error instanceof Error ? error.message : String(error)}`,
2984
+ { cause: error }
2985
+ );
2986
+ }
2987
+ }
2988
+ function polyHom(sourceSchema, targetSchema, wasm) {
2989
+ try {
2990
+ return wasm.exports.poly_hom(sourceSchema, targetSchema);
2991
+ } catch (error) {
2992
+ throw new WasmError(
2993
+ `Failed to construct internal hom: ${error instanceof Error ? error.message : String(error)}`,
2994
+ { cause: error }
2995
+ );
2996
+ }
2997
+ }
2998
+ function preferredPath(edges, sourceSchema, targetSchema, wasm) {
2999
+ try {
3000
+ const graphBytes = packToWasm(edges);
3001
+ const resultBytes = wasm.exports.preferred_conversion_path(
3002
+ graphBytes,
3003
+ sourceSchema,
3004
+ targetSchema
3005
+ );
3006
+ return unpackFromWasm(resultBytes);
3007
+ } catch {
3008
+ return null;
3009
+ }
3010
+ }
3011
+ function distance(edges, sourceSchema, targetSchema, wasm) {
3012
+ try {
3013
+ const graphBytes = packToWasm(edges);
3014
+ return wasm.exports.conversion_distance(graphBytes, sourceSchema, targetSchema);
3015
+ } catch (error) {
3016
+ throw new WasmError(
3017
+ `Failed to compute conversion distance: ${error instanceof Error ? error.message : String(error)}`,
3018
+ { cause: error }
3019
+ );
3020
+ }
3021
+ }
2841
3022
  function classifyOpticKind(chain, schema, _wasm) {
2842
3023
  const spec = chain.requirements(schema);
2843
3024
  const hasDefaults = spec.forwardDefaults.length > 0;
@@ -3122,8 +3303,17 @@ exports.WasmError = WasmError;
3122
3303
  exports.checkMorphism = checkMorphism;
3123
3304
  exports.colimit = colimit;
3124
3305
  exports.createTheory = createTheory;
3306
+ exports.distance = distance;
3307
+ exports.evalExpr = evalExpr;
3308
+ exports.executeQuery = executeQuery;
3125
3309
  exports.factorizeMorphism = factorizeMorphism;
3310
+ exports.fiberAt = fiberAt;
3311
+ exports.fiberDecomposition = fiberDecomposition;
3312
+ exports.formatExpr = formatExpr;
3126
3313
  exports.getBuiltinProtocol = getBuiltinProtocol;
3127
3314
  exports.getProtocolNames = getProtocolNames;
3128
3315
  exports.migrateModel = migrateModel;
3316
+ exports.parseExpr = parseExpr;
3317
+ exports.polyHom = polyHom;
3318
+ exports.preferredPath = preferredPath;
3129
3319
  //# sourceMappingURL=index.cjs.map