• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Hons-and-memoization
      • Events
        • Defun
        • Verify-guards
        • Table
        • Memoize
        • Make-event
        • Include-book
        • Encapsulate
        • Defun-sk
        • Defttag
        • Defpkg
        • Mutual-recursion
        • Defattach
        • Defstobj
        • Defchoose
        • Progn
        • Defabsstobj
        • Verify-termination
        • Redundant-events
        • Defmacro
        • In-theory
        • Embedded-event-form
        • Defconst
        • Skip-proofs
        • Value-triple
        • Comp
        • Local
        • Defthm
        • Progn!
        • Defevaluator
        • Theory-invariant
        • Assert-event
        • Defun-inline
        • Project-dir-alist
        • Define-trusted-clause-processor
        • Partial-encapsulate
        • Defproxy
        • Defexec
        • Defun-nx
        • Defthmg
        • Defpun
        • Defabbrev
        • Defrec
          • Change
          • Make
          • Access
        • Add-custom-keyword-hint
        • Name
        • Regenerate-tau-database
        • Deftheory
        • Deftheory-static
        • Defcong
        • Defaxiom
        • Defund
        • Evisc-table
        • Verify-guards+
        • Logical-name
        • Profile
        • Defequiv
        • Defmacro-untouchable
        • Defthmr
        • Defstub
        • Deflabel
        • Defrefinement
        • In-arithmetic-theory
        • Defabsstobj-missing-events
        • Defthmd
        • Set-body
        • Unmemoize
        • Defun-notinline
        • Dump-events
        • Defund-nx
        • Defun$
        • Remove-custom-keyword-hint
        • Dft
        • Defthy
        • Defund-notinline
        • Defnd
        • Defn
        • Defund-inline
        • Defmacro-last
      • History
      • Parallelism
      • Programming
      • Start-here
      • Real
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Testing-utilities
    • Math
  • Events

Defrec

Introduce a record structure, like a struct in C.

The defrec macro is built into ACL2 and is frequently used in the ACL2 sources to define basic kinds of structures.

Better Alternatives to Defrec

While defrec may be a reasonable choice for writing program-mode code, most users would likely be better served by using one of the many, richer alternatives to defrec that are available in various books. See for instance macros such as:

  • The std::defaggregate macro from std/util,
  • The defdata macro,
  • The fty::defprod macro from the fty library, and
  • The data-structures/structures book.

A major reason to favor these macros over defrec is that they introduce the constructors and accessors for the structure as proper functions, rather than mere macros. This is very helpful when you are trying to use ACL2 to reason about code that works with structures. For instance, it means that your proof goals will involve terms like (employee->position x) instead of (cdddr x).

Another reason is that defrec does not support putting constraints on the fields of your structure. For instance, you might want to have an employee structure where the name field is always a string. You can't do this with defrec, but any of the above macros support it.

Some of the above macros also have other useful features, e.g., integration with b*, support for xdoc documentation, and so forth.

Usage

A typical use of defrec might look like this:

(defrec employee             ;; name of the structure
  (name salary . position)   ;; fields of the structure
  nil)                       ;; "cheap" flag

This will result in the introduction of:

  • A "weak" recognizer function, weak-employee-p, which recognizes cons structures that have the right shape. We call the recognizer weak because it doesn't impose any constraints on the fields, e.g., there is no requirement that the name of an employee must be a string or that the salary must be a number.
  • A make macro that can be used like this:
    (make employee :name "Jimmy"
                   :salary 0
                   :position "Unpaid Intern")
  • A change macro that can be used like this:
    (let ((jimmy (make-employee :name "Jimmy" ...)))
      (change employee jimmy :salary 300000
                             :position "Vice President"))
  • Suitable access macros that can be used like this:
    (let ((jimmy (make-employee :name "Jimmy" ...)))
      (access employee jimmy :name))

Subtopics

Change
Mutator macro for defrec structures.
Make
Constructor macro for defrec structures.
Access
Accessor macro for defrec structures.