• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Debugging
    • Projects
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Hons-and-memoization
      • Events
      • History
      • Parallelism
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
        • Memoize
        • Mbe
        • Io
        • Defpkg
        • Apply$
        • Mutual-recursion
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
        • Time$
        • Loop$-primer
        • Fast-alists
        • Defmacro
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • System-attachments
        • Developers-guide
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Irrelevant-formals
        • Efficiency
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
        • Program-wrapper
        • Get-internal-time
        • Basics
          • Let
          • Return-last
          • Mv-let
          • Or
          • Flet
          • Mv
          • And
          • Booleanp
          • If
          • Not
          • Equal
          • Implies
          • Iff
          • Quote
          • Macrolet
          • Let*
          • Case-match
          • ACL2-count
          • Good-bye
          • Case
            • Safe-case
          • Cond
          • Null
          • Progn$
          • Identity
          • Xor
        • Packages
        • Defmacro-untouchable
        • Primitive
        • <<
        • Revert-world
        • Set-duplicate-keys-action
        • Unmemoize
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Defopen
        • Sleep
      • Start-here
      • Real
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Testing-utilities
    • Math
  • Basics
  • ACL2-built-ins

Case

Conditional based on if-then-else using eql

Example Form:
(case typ
  ((:character foo)
   (open file-name :direction :output))
  (bar (open-for-bar file-name))
  (otherwise
   (my-error "Illegal.")))

is the same as

(cond ((member typ '(:character foo))
       (open file-name :direction :output))
      ((eql typ 'bar)
       (open-for-bar file-name))
      (t (my-error "Illegal.")))

which in turn is the same as

(if (member typ '(:character foo))
    (open file-name :direction :output)
    (if (eql typ 'bar)
        (open-for-bar file-name)
        (my-error "Illegal.")))

Notice the quotations that appear in the example above: '(:character foo) and 'bar. Indeed, a case expression expands to a cond expression in which each tested form is quoted, and eql is used to test equality, as described below..

General Forms:
(case expr
  (x1 val-1)
  ...
  (xk val-k)
  (otherwise val-k+1))

(case expr
  (x1 val-1)
  ...
  (xk val-k)
  (t val-k+1))

(case expr
  (x1 val-1)
  ...
  (xk val-k))

where each xi is either eqlablep or a true list of eqlablep objects. The final otherwise or t case is optional; if neither is present, then an equivalent expression results from adding the final case (t nil)..

As suggested above, each case (xi val-i) generates an if-then-else expression as follows. If xi is a non-nil atom (i.e., xi is not nil or a cons pair), then that case generates the expression (if (eql expr (quote xi)) vali ...) where `...' denotes the expression generated by the rest of the cases. If however xi is a list, then instead the generated expression is (if (member expr (quote xi)) vali ...). The final case (t val-k) or (otherwise val-k) generates nil. Note that t and otherwise here must be in the "ACL2" package. Also note that to compare expr with nil, you should write the case as ((nil) val-i) rather than (nil val-i).

Case is defined in Common Lisp. See any Common Lisp documentation for more information.

Subtopics

Safe-case
Error-checking alternative to case.