sigildocs

(sigil diagnostic)

(sigil diagnostic) - Error Diagnostics and Suggestions

Provides a diagnostic layer on top of (sigil error) that adds contextual suggestions to errors like "did you mean?" for unbound variables, type guard hints for type errors, and arity summaries.

All functions accept both old-style exceptions and error structs — they call exception->error internally to normalize.

(import (sigil diagnostic))

(guard (exn
         (else (display (format-error-with-suggestions exn))))
  (car 42))

Exports

Compute the Levenshtein edit distance between two strings.

Uses a single-row dynamic programming approach for O(min(m,n)) space.

(levenshtein-distance "kitten" "sitting")  ; => 3
(levenshtein-distance "foo" "foo")          ; => 0

Find symbols similar to NAME in the current module.

Returns a list of (symbol-name . distance) pairs sorted by distance, filtered to symbols within edit distance 3.

(find-similar-symbols "mapc")  ; => (("map" . 1) ...)

Generate suggestion strings for an error.

Accepts both old-style exceptions and error structs. Returns a list of suggestion strings based on the error type:

  • Type errors: guard hint based on expected type
  • Arity errors: expected argument count summary
  • Unbound errors: "did you mean?" suggestions via Levenshtein
  • Others: empty list
(error-suggestions (type-error message: "car: expected pair"
                               expected-type: "pair"))
; => ("Use `pair?` to check the value first")

Format an error with appended suggestions.

Combines format-exception output with contextual suggestions. Accepts both old-style exceptions and error structs.

(guard (exn
         (else (display (format-error-with-suggestions exn))))
  (car 42))
; Error (type-error): car: expected pair
;   In: car
;   Expected: pair
;
; Suggestions:
;   - Use `pair?` to check the value first
inspect-errorprocedure

Convert an error to a dict with structured fields and suggestions.

Suitable for nREPL/MCP structured error responses. Accepts both old-style exceptions and error structs.

(inspect-error (exception->error exn))
; => #{ type: 'type-error message: "..." procedure-name: "car" ... }