<common_lisp_fundamentals> <concept name="s_expressions"> <description>Code and data share the same syntax (homoiconicity). Enables powerful macro systems for code transformation.</description> </concept>
<concept name="symbols"> <description>First-class named objects used for identifiers. Interned in packages, can have value, function, and property list.</description> </concept> <concept name="multiple_values"> <description>Functions can return multiple values using values, multiple-value-bind, multiple-value-list.</description> </concept> <concept name="dynamic_binding"> <description>Special variables with dynamic scope using defvar/defparameter. Convention: *earmuffs* for special variables.</description> </concept> </common_lisp_fundamentals> <clos> <description>Common Lisp Object System - Generic functions and multiple dispatch</description> <pattern name="defclass"> <description>Define a class with slots. Slot options: :initarg, :initform, :accessor, :reader, :writer, :type, :documentation.</description> <example> (defclass person () ((name :initarg :name :accessor person-name) (age :initarg :age :accessor person-age)) (:documentation "Represents a person.")) </example> </pattern> <pattern name="defgeneric_defmethod"> <description>Define generic functions with multiple method implementations.</description> <example> (defgeneric greet (entity) (:documentation "Greet an entity.")) (defmethod greet ((p person))
(format t "Hello, ~a!~%" (person-name p)))
</example>
<decision_tree name="when_to_use">
<question>Do you need polymorphic behavior based on multiple types?</question>
<if_yes>Use defgeneric and defmethod for multiple dispatch</if_yes>
<if_no>Use regular functions for single implementation</if_no>
</decision_tree>
(defmethod greet :around ((p person))
(format t "[Start]~%")
(call-next-method)
(format t "[End]~%"))
</example>
;; In my-project/main.lisp:
(defpackage #:my-project/main
(:use #:cl)
(:import-from #:my-project/utils #:helper))
</example>
<decision_tree name="when_to_use">
<question>Do you want automatic dependency inference from package definitions?</question>
<if_yes>Use package-inferred-system for modern projects</if_yes>
<if_no>Use traditional defsystem with explicit component dependencies</if_no>
</decision_tree>
(sb-ext:save-lisp-and-die "my-app"
:toplevel #'main
:executable t
:compression t)
</example>
(let ((thread (sb-thread:make-thread
(lambda ()
(setf _result_ (heavy-computation)))
:name "worker")))
(sb-thread:join-thread thread))
;; Mutex
(defvar _lock_ (sb-thread:make-mutex))
(sb-thread:with-mutex (_lock_)
(critical-section))
</example>
(strlen "hello") ; => 5 </example>
;; Execute external programs
(sb-ext:run-program "/bin/ls" '("-l"))
;; Trigger garbage collection
(sb-ext:gc)
;; POSIX interface: sb-posix
;; Network sockets: sb-bsd-sockets
</example>
(declare safe-div (Integer -> Integer -> (Maybe Integer)))
(define (safe-div x y)
(if (== y 0)
None
(Some (/ x y)))))
</example>
(define-instance (Printable Integer)
(define (print-it x)
(into x))))
</example>
<context7_libraries> <description>Available Context7 documentation libraries for Common Lisp ecosystem.</description>
<tool name="context7_common_lisp_docs"> <description>Common Lisp Docs - General Common Lisp documentation</description> <param name="library_id">/lisp-docs/lisp-docs.github.io</param> <param name="trust_score">4.7</param> <param name="snippets">580</param> </tool> <tool name="context7_asdf"> <description>ASDF - Another System Definition Facility documentation</description> <param name="library_id">/websites/asdf_common-lisp_dev</param> <param name="trust_score">7.5</param> <param name="snippets">190</param> </tool> <tool name="context7_sbcl"> <description>SBCL - Steel Bank Common Lisp documentation</description> <param name="library_id">/sbcl/sbcl</param> <param name="trust_score">8.0</param> <param name="snippets">86</param> </tool> <tool name="context7_cffi"> <description>CFFI - Common Foreign Function Interface documentation</description> <param name="library_id">/websites/cffi_common-lisp_dev</param> <param name="trust_score">7.5</param> <param name="snippets">198</param> </tool> <tool name="context7_fiveam"> <description>FiveAM - Testing framework documentation</description> <param name="library_id">/websites/fiveam_common-lisp_dev</param> <param name="trust_score">7.5</param> <param name="snippets">164</param> </tool> <tool name="context7_coalton"> <description>Coalton - Statically typed functional programming documentation</description> <param name="library_id">/coalton-lang/coalton</param> <param name="trust_score">6.6</param> <param name="snippets">568</param> </tool> <pattern name="retrieve_documentation"> <description>Use resolve-library-id then get-library-docs for latest documentation.</description> <example> ;; Get ASDF documentation mcp__context7__get-library-docs context7CompatibleLibraryID="/websites/asdf_common-lisp_dev" topic="defsystem" </example> </pattern> </context7_libraries><common_patterns> <pattern name="with_macro"> <description>Resource management with unwind-protect for cleanup.</description> <example> (defmacro with-open-socket ((var host port) &body body) `(let ((,var (make-socket ,host ,port))) (unwind-protect (progn ,@body) (close-socket ,var)))) </example> </pattern>
<pattern name="loop_macro"> <description>Loop macro for iteration with collection, filtering, and accumulation.</description> <example> (loop for item in list for i from 0 when (evenp i) collect item into evens finally (return evens)) </example> </pattern> <pattern name="format_directives"> <description>Common format directives: ~a (aesthetic), ~s (standard), ~d (decimal), ~f (float), ~% (newline), ~{~} (iteration), ~[~] (conditional).</description> <example> (format t "~a is ~d years old~%" name age) </example> </pattern> <pattern name="documentation"> <description>Document functions with docstrings explaining purpose and parameters.</description> <example> (defun my-function (arg) "Docstring describing the function. ARG is the argument description." (process arg)) </example> </pattern> </common_patterns><best_practices>
<practice priority="high">Use *earmuffs* for special variables</practice>
<practice priority="high">Use +plus-signs+ for constants</practice>
<practice priority="high">Prefer functional style, minimize mutation</practice>
<practice priority="high">Provide restarts for recoverable situations</practice>
<practice priority="high">Document exported symbols</practice>
<practice priority="medium">Use appropriate condition types, not just error</practice>
<practice priority="medium">Use check-type for argument validation</practice>
<practice priority="medium">Prefer ASDF package-inferred-system for new projects</practice>
<practice priority="medium">Consider Qlot for per-project dependency management</practice>
<practice priority="medium">Use Roswell for portable script execution</practice>
</best_practices>
<modern_tooling> <tool name="qlot"> <description>Per-project dependency manager (like bundler/npm)</description> <use_case>Install dependencies from qlfile</use_case> <use_case>Run commands with project dependencies</use_case> <example> qlot install qlot exec ros run </example> </tool>
<tool name="roswell"> <description>Lisp implementation manager and script runner</description> <use_case>Install Lisp implementations or libraries</use_case> <use_case>Start REPL with specified implementation</use_case> <use_case>Build standalone executable</use_case> <example> ros install sbcl ros run ros build myapp.ros </example> </tool> </modern_tooling><anti_patterns> <avoid name="global_state"> <description>Global mutable state makes code harder to test and reason about.</description> <instead>Pass state explicitly or use closures to encapsulate mutable state.</instead> </avoid>
<avoid name="bare_use"> <description>Using :use for packages other than :cl creates namespace pollution.</description> <instead>Use :import-from or package-local-nicknames for clearer dependencies.</instead> </avoid> <avoid name="ignore_conditions"> <description>Ignoring conditions loses error context and recovery opportunities.</description> <instead>Handle conditions with handler-case or handler-bind, and provide appropriate restarts.</instead> </avoid> <avoid name="deep_nesting"> <description>Deeply nested code reduces readability and maintainability.</description> <instead>Extract helper functions and use early returns to reduce nesting depth.</instead> </avoid> <avoid name="eval_usage"> <description>Using eval in application code is slow and defeats compile-time optimization.</description> <instead>Use macros for compile-time code generation or first-class functions for runtime dispatch.</instead> </avoid> <avoid name="read_macros_overuse"> <description>Custom reader macros make code harder to read for others.</description> <instead>Use reader macros sparingly and document them clearly when necessary.</instead> </avoid> </anti_patterns> <workflow> <phase name="analyze"> <objective>Understand Lisp code requirements</objective> <step>1. Check ASDF system definition</step> <step>2. Review existing macros and patterns</step> <step>3. Identify CLOS class hierarchies</step> </phase> <phase name="implement"> <objective>Write idiomatic Common Lisp code</objective> <step>1. Use appropriate abstraction level</step> <step>2. Follow condition system for errors</step> <step>3. Design reusable macros carefully</step> </phase> <phase name="validate"> <objective>Verify Lisp code correctness</objective> <step>1. Load system with ASDF</step> <step>2. Run tests with appropriate framework</step> <step>3. Check for compilation warnings</step> </phase> </workflow><error_escalation> <level severity="low"> <example>Style inconsistency</example> <action>Fix formatting, follow project conventions</action> </level> <level severity="medium"> <example>Compilation warning or type error</example> <action>Fix issue, add type declarations if needed</action> </level> <level severity="high"> <example>Macro expansion error</example> <action>Debug with macroexpand, present options to user</action> </level> <level severity="critical"> <example>Reader macro conflict</example> <action>Block operation, require careful namespace management</action> </level> </error_escalation>
<constraints> <must>Use ASDF for system definition</must> <must>Follow condition system for error handling</must> <must>Document macros with clear examples</must> <avoid>Overly complex macros without documentation</avoid> <avoid>Global state without clear lifecycle</avoid> <avoid>Reader macros without namespace isolation</avoid> </constraints><related_skills> <skill name="serena-usage">Navigate CLOS hierarchies, generic functions, and symbol definitions</skill> <skill name="context7-usage">Access ASDF, SBCL, and Common Lisp library documentation</skill> <skill name="investigation-patterns">Debug condition handling, macro expansion, and SBCL-specific issues</skill> </related_skills>