lawspec 0.6.0 → 0.8.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.
package/index.d.ts CHANGED
@@ -1,19 +1,29 @@
1
1
  // Generated from LawSpec.Gen. Do not edit.
2
- export type Target = 'java' | 'python' | 'javascript' | 'typescript' | 'go' | 'haskell' | 'kotlin';
2
+ export type Target = 'java' | 'python' | 'javascript' | 'typescript' | 'go' | 'haskell' | 'kotlin' | 'rust';
3
3
  export interface Source { path: string; content: string }
4
4
  export interface Location { file: string; line: number; column: number }
5
+ export interface Span { start: Location; end: Location }
5
6
  export interface Diagnostic { code: string; message: string; at: Location | null }
6
- export interface Artifact { path: string; content: string; ownership: 'user' | 'generated' }
7
- export type Type = {tag: 'Named' | 'Variable'; contents: string} | {tag: 'Arrow'; contents: [Type, Type]};
8
- export type Expr = {tag: 'Var' | 'StringLit'; contents: string} | {tag: 'Number'; contents: number} | {tag: 'BoolLit'; contents: boolean} | {tag: 'Apply' | 'Compose'; contents: [Expr, Expr]};
9
- export type Definition = {tag: 'Forall'; contents: [[string, Type][], Definition]} | {tag: 'Equal'; contents: [Expr, Expr]} | {tag: 'Holds'; contents: Expr} | {tag: 'Implies'; contents: [Expr, Definition]} | {tag: 'And'; contents: [Definition, Definition]} | {tag: 'Invoke'; contents: [string, Expr[]]};
10
- export interface Expectation { actual: Expr; expected: number | string | boolean }
11
- export interface Example { exampleName: string; bindings: [string, number | string | boolean][]; expectations: Expectation[] }
12
- export interface Law { lawName: string; parameters: [string, Type][]; requirements: Type[]; definition: Definition; description: string; rationale: string; examples: Example[]; references: string[]; location: Location }
13
- export type Assertion = {tag: 'AssertEqual'; contents: [Expr, Expr]} | {tag: 'AssertImplies'; contents: [Expr, Assertion]} | {tag: 'AssertAll'; contents: Assertion[]};
14
- export interface Expanded { owner: string; name: string; inputs: {inputName: string; inputId: string; inputType: Type}[]; left: Expr; right: Expr; guards: Expr[]; assertion: Assertion; trace: string[]; original: Law }
15
- export interface CheckRequest { sources: Source[] }
16
- export interface GenerationRequest extends CheckRequest { target: Target; sourceDir?: string; testDir?: string }
17
- export interface Result { diagnostics: Diagnostic[]; laws?: Expanded[]; expansions?: string[]; files?: Artifact[] }
7
+ export interface Artifact { path: string; content: string; ownership: 'user' | 'generated'; placement: 'source' | 'test' }
8
+ export type Type = {kind:'constructor'; name:string; arguments:TypeArgument[]} | {kind:'variable'; id:string} | {kind:'function'; parameter:Type; result:Type};
9
+ export type TypeArgument = {kind:'type'; type:Type} | {kind:'natural'; value:string} | {kind:'indexVariable'; id:string};
10
+ export type Origin = {kind:'source'; span:Span} | {kind:'generated'; declaration:string};
11
+ export type Evidence = {kind:'numeric'|'structural'; type:Type};
12
+ export interface Expr {type:Type; origin:Origin; text:string; node:ExprNode}
13
+ export type ExprNode = {kind:'constant'; value:ScalarValue} | {kind:'local'; id:string} | {kind:'call'; declaration:string; arguments:Expr[]} | {kind:'binary'; operator:string; evidence:Evidence; left:Expr; right:Expr} | {kind:'unary'; operator:'-'|'!'; argument:Expr} | {kind:'shortCircuit'; operator:'&&'|'||'; left:Expr; right:Expr} | {kind:'convert'; conversion:'explicit'|'checked'; argument:Expr} | {kind:'helper'; name:string; arguments:Expr[]};
14
+ export type Assertion = {kind:'equal'; evidence:Evidence; left:Expr; right:Expr} | {kind:'implies'; guard:Expr; body:Assertion} | {kind:'all'; items:Assertion[]};
15
+ export interface Generation {cases:number; maxAttempts:number; maxShrinks:number; exhaustiveLimit:number}
16
+ export interface Binder {id:string; name:string; type:Type}
17
+ export interface Input extends Binder {predicates:Expr[]; bounds:{operator:string; value:Expr}[]}
18
+ export interface Example {name:string; bindings:(Binder & {value:ScalarValue})[]; expectations:Assertion[]}
19
+ export interface Law {id:string; owner:string; name:string; inputs:Input[]; assertion:Assertion; examples:Example[]; description:string; rationale:string; references:string[]; location:Location; trace:string[]; generation:Generation}
20
+ export interface Unit {id:string; declarations:(Binder & {origin:Origin})[]}
21
+ export interface Contract {id:string; name:string; arguments:Binder[]; result:Binder; preconditions:Expr[]; postconditions:Expr[]}
22
+ export interface Refinement {owner:string; name:string; parameters:{name:string; kind:'type'|'value'; type:string}[]; requirements:{capability:string; type:string}[]; definition:string}
23
+ export interface CheckRequest {sources:Source[]; schemaVersion?:3; machineBits?:32|64; generation?:Partial<Generation>}
24
+ export interface GenerationRequest extends CheckRequest {target:Target; sourceDir?:string; testDir?:string}
25
+ export interface Result {schemaVersion:3; machineBits?:32|64; generation?:Generation; units?:Unit[]; laws?:Law[]; contracts?:{owner:string; contract:Contract}[]; refinements?:Refinement[]; diagnostics:Diagnostic[]; expansions?:string[]; files?:Artifact[]}
26
+ export type IntegerType = 'Int8' | 'Int16' | 'Int32' | 'Int64' | 'UInt8' | 'UInt16' | 'UInt32' | 'UInt64' | 'IntSize' | 'UIntSize' | 'UIntPtr' | 'Integer' | 'BigInt' | 'BigUInt';
27
+ export type ScalarValue = {type: IntegerType; value: string} | {type: 'Bool'; value: boolean} | {type: 'Decimal'; coefficient: string; exponent: string} | {type: 'Rational'; numerator: string; denominator: string} | {type: 'Float32' | 'Float64'; bits: string} | {type: 'Complex64' | 'Complex128'; real: ScalarValue; imaginary: ScalarValue} | {type: 'Char' | 'CodePoint' | 'CodeUnit16'; value: number} | {type: 'Text' | 'CodePointText' | 'Utf16Text' | 'Bytes'; units: number[]} | {type: 'Symbol'; id: string; description: string} | {type: 'Unit' | 'Null' | 'Undefined'} | {type: 'Nullable' | 'Optional'; value: ScalarValue | null};
18
28
  export interface Compiler { check(input: CheckRequest): Promise<Result>; expand(input: CheckRequest): Promise<Result>; planGeneration(input: GenerationRequest): Promise<Result> }
19
29
  export function createCompiler(): Promise<Compiler>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lawspec",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "State the law once. Check it everywhere.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -8,6 +8,6 @@
8
8
  "bin": { "lawspec": "bin/lawspec.mjs" },
9
9
  "exports": { ".": { "types": "./index.d.ts", "import": "./api.mjs" } },
10
10
  "types": "index.d.ts",
11
- "files": ["*.mjs", "*.json", "index.d.ts", "bin", "core.wasm", "core_jsffi.js", "README.md", "LICENSE", "starter.lawspec", "examples"],
11
+ "files": ["*.mjs", "*.json", "index.d.ts", "bin", "core.wasm", "core_jsffi.js", "README.md", "PRIMITIVES.md", "REFINEMENTS.md", "RUST.md", "LANGUAGE.md", "API-MIGRATION.md", "LICENSE", "starter.lawspec", "examples"],
12
12
  "scripts": { "test": "node --test test/*.test.mjs" }
13
13
  }
package/scalars.mjs ADDED
@@ -0,0 +1,13 @@
1
+ // Human-readable, lossless API v2 scalar display.
2
+ export function showScalar(v) {
3
+ if (!v || typeof v !== 'object') throw new TypeError('Expected an API v2 scalar');
4
+ if ('coefficient' in v) return `${v.coefficient}e${v.exponent}`;
5
+ if ('numerator' in v) return `rational(${v.numerator}, ${v.denominator})`;
6
+ if ('bits' in v) return `${v.type === 'Float32' ? 'float32Bits' : 'float64Bits'}(${JSON.stringify(v.bits)})`;
7
+ if ('real' in v) return `${v.type}(${showScalar(v.real)}, ${showScalar(v.imaginary)})`;
8
+ if ('units' in v) return v.type === 'Text' ? JSON.stringify(v.units.map(c => String.fromCodePoint(c)).join('')) : `${v.type}([${v.units.join(', ')}])`;
9
+ if (v.type === 'Symbol') return `symbol(${JSON.stringify(v.id)}, ${JSON.stringify(v.description)})`;
10
+ if (v.type === 'Optional' || v.type === 'Nullable') return v.value === null ? (v.type === 'Optional' ? 'undefined' : 'null') : `${v.type.toLowerCase()}(${showScalar(v.value)})`;
11
+ if ('value' in v) return String(v.value);
12
+ return {Unit:'unitValue', Null:'null', Undefined:'undefined'}[v.type] ?? v.type;
13
+ }
package/templates.mjs CHANGED
@@ -6,8 +6,10 @@ export const targets = [
6
6
  "go",
7
7
  "haskell",
8
8
  "kotlin",
9
+ "rust",
9
10
  ];
10
11
  export const commands = {
12
+ rust: "cargo test",
11
13
  java: "mvn test",
12
14
  python: "python -m pytest",
13
15
  javascript: "node --test test/*.test.mjs",
@@ -18,6 +20,7 @@ export const commands = {
18
20
  kotlin: "gradle test",
19
21
  };
20
22
  export const setup = {
23
+ rust: "Use Rust 1.85+ with edition 2024, Proptest 1.11.0, num-bigint 0.4.8, num-rational 0.4.2, num-complex 0.4.6, and num-traits 0.2.19. Run cargo test.",
21
24
  java: "Use JDK 25+, Maven, JetCheck 0.3.0, JUnit Jupiter 5.14.x, compiler plugin 3.14.1+, Surefire 3.5.x, and maven.compiler.release=25. Run mvn test-compile.",
22
25
  python:
23
26
  'Use Python 3.13+. Install pytest 8.4.x and Hypothesis 6.x into the selected environment: python -m pip install -e ".[test]". Configure pytest pythonpath=["src"] and testpaths=["tests"]. Set the target python field to the interpreter path if needed.',
@@ -41,6 +44,27 @@ export function templates(target) {
41
44
  type: "module",
42
45
  };
43
46
  switch (target) {
47
+ case "rust":
48
+ return {
49
+ "src/lib.rs": '// Application library. LawSpec maintains the included module declarations.\ninclude!("lawspec_modules.rs");\n',
50
+ "Cargo.toml": `[package]
51
+ name = "lawspec-example"
52
+ version = "0.1.0"
53
+ edition = "2024"
54
+ rust-version = "1.85"
55
+ publish = false
56
+
57
+ [dependencies]
58
+ num-bigint = "=0.4.8"
59
+ num-rational = "=0.4.2"
60
+ num-complex = "=0.4.6"
61
+ num-traits = "=0.2.19"
62
+
63
+ [dev-dependencies]
64
+ proptest = "=1.11.0"
65
+ `,
66
+ };
67
+
44
68
  case "javascript":
45
69
  return {
46
70
  "package.json": json({
@@ -100,7 +124,7 @@ export function templates(target) {
100
124
  case "haskell":
101
125
  return {
102
126
  "stack.yaml": "snapshot: lts-24.58\npackages: [.]\n",
103
- "package.yaml": `name: lawspec-example\nversion: 0.1.0\ndependencies: [base, text]\nlibrary:\n source-dirs: src\ntests:\n laws:\n main: Spec.hs\n source-dirs: test\n dependencies: [lawspec-example, hspec, hedgehog, hspec-hedgehog]\n build-tools: [hspec-discover]\n`,
127
+ "package.yaml": `name: lawspec-example\nversion: 0.1.0\ndependencies: [base, text, bytestring]\nlibrary:\n source-dirs: src\ntests:\n laws:\n main: Spec.hs\n source-dirs: test\n dependencies: [lawspec-example, hspec, hedgehog, hspec-hedgehog]\n build-tools: [hspec-discover]\n`,
104
128
  "test/Spec.hs": "{-# OPTIONS_GHC -F -pgmF hspec-discover #-}\n",
105
129
  };
106
130
  case "kotlin":