• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
        • Crypto-hdwallet
        • Apt
        • Error-checking
        • Fty-extensions
        • Isar
        • Kestrel-utilities
        • Set
        • Soft
        • C
          • Syntax-for-tools
            • Disambiguator
            • Abstract-syntax
            • Parser
            • Validator
              • Valid-exprs/decls/stmts
              • Valid-stmt
              • Valid-expr
              • Valid-dirdeclor
              • Valid-type-spec
              • Valid-transunit
              • Valid-stor-spec-list
              • Valid-binary
              • Valid-unary
              • Valid-initdeclor
              • Valid-fundef
              • Valid-type-spec-list-residual
              • Valid-cond
                • Valid-lookup-ord
                • Valid-transunit-ensemble
                • Valid-declor
                • Valid-iconst
                • Valid-initer
                • Valid-c-char
                • Valid-stringlit-list
                • Valid-funcall
                • Valid-add-ord
                • Valid-arrsub
                • Valid-univ-char-name
                • Valid-extdecl
                • Valid-extdecl-list
                • Valid-cast
                • Valid-add-ord-file-scope
                • Valid-spec/qual
                • Valid-sizeof/alignof
                • Valid-memberp
                • Valid-decl-spec
                • Valid-var
                • Valid-param-declon
                • Valid-oct-escape
                • Valid-structdeclor
                • Valid-structdecl
                • Valid-designor
                • Valid-escape
                • Valid-enum-const
                • Valid-cconst
                • Valid-s-char
                • Valid-dec/oct/hex-const
                • Valid-const
                • Valid-gensel
                • Valid-decl-spec-list
                • Valid-lookup-ord-file-scope
                • Valid-member
                • Valid-param-declor
                • Valid-spec/qual-list
                • Valid-fconst
                • Valid-stringlit
                • Valid-s-char-list
                • Valid-c-char-list
                • Valid-block-item
                • Valid-structdeclor-list
                • Valid-simple-escape
                • Valid-align-spec
                • Valid-enumer
                • Valid-dirabsdeclor
                • Valid-decl
                • Valid-pop-scope
                • Valid-enumspec
                • Valid-declor-option
                • Valid-push-scope
                • Valid-initer-option
                • Valid-block-item-list
                • Valid-absdeclor
                • Valid-label
                • Valid-genassoc
                • Valid-tyname
                • Valid-strunispec
                • Valid-structdecl-list
                • Valid-genassoc-list
                • Valid-dirabsdeclor-option
                • Valid-designor-list
                • Valid-const-expr
                • Valid-initdeclor-list
                • Valid-desiniter-list
                • Valid-absdeclor-option
                • Valid-table-num-scopes
                • Valid-expr-list
                • Valid-param-declon-list
                • Valid-desiniter
                • Valid-const-expr-option
                • Valid-expr-option
                • Valid-statassert
                • Valid-enumer-list
                • Valid-init-table
                • Valid-empty-scope
                • Valid-member-designor
                • Valid-decl-list
              • Printer
              • Formalized-subset
              • Mapping-to-language-definition
              • Input-files
              • Defpred
              • Output-files
              • Abstract-syntax-operations
              • Validation-information
              • Implementation-environments
              • Concrete-syntax
              • Unambiguity
              • Ascii-identifiers
              • Preprocessing
              • Abstraction-mapping
            • Atc
            • Language
            • Representation
            • Transformation-tools
            • Insertion-sort
            • Pack
          • Bv
          • Imp-language
          • Event-macros
          • Java
          • Bitcoin
          • Ethereum
          • Yul
          • Zcash
          • ACL2-programming-language
          • Prime-fields
          • Json
          • Syntheto
          • File-io-light
          • Cryptography
          • Number-theory
          • Lists-light
          • Axe
          • Builtins
          • Solidity
          • Helpers
          • Htclient
          • Typed-lists-light
          • Arithmetic-light
        • X86isa
        • Axe
        • Execloader
      • Math
      • Testing-utilities
    • Validator

    Valid-cond

    Validate a conditional expression, given types for its operands.

    Signature
    (valid-cond expr type-test type-then type-else ienv) 
      → 
    (mv erp type)
    Arguments
    expr — Guard (exprp expr).
    type-test — Guard (typep type-test).
    type-then — Guard (typep type-then).
    type-else — Guard (typep type-else).
    ienv — Guard (ienvp ienv).
    Returns
    type — Type (typep type).

    The first operand must have scalar type [C17:6.5.15/2]. In our currently approximate type system, the other two operands must have both arithmetic type, or both the same structure type, or both the union type, or both the void type, or both the pointer type, or one pointer type and the other operand a null pointer constant [C17:6.5.15/3]. Currently, null pointer constants [C17:6.3.2.3/3] are approximated as any expression with an integer type. The type of the result is the one from the usual arithmetic converions in the first case, and the common type in the other cases [C17:6.5.15/5]. Since pointers may be involved, we need to perform array-to-pointer and function-to-pointer conversions.

    Definitions and Theorems

    Function: valid-cond

    (defun valid-cond (expr type-test type-then type-else ienv)
     (declare (xargs :guard (and (exprp expr)
                                 (typep type-test)
                                 (typep type-then)
                                 (typep type-else)
                                 (ienvp ienv))))
     (declare (xargs :guard (expr-case expr :cond)))
     (let ((__function__ 'valid-cond))
      (declare (ignorable __function__))
      (b*
       (((reterr) (irr-type))
        ((when (or (type-case type-test :unknown)
                   (type-case type-then :unknown)
                   (type-case type-else :unknown)))
         (retok (type-unknown)))
        (type1 (type-fpconvert (type-apconvert type-test)))
        (type2 (type-fpconvert (type-apconvert type-then)))
        (type3 (type-fpconvert (type-apconvert type-else)))
        ((unless (type-scalarp type1))
         (reterr
          (msg
           "In the conditional expression ~x0, ~
                          the first operand has type ~x1."
           (expr-fix expr)
           (type-fix type-test))))
        ((when (and (type-arithmeticp type2)
                    (type-arithmeticp type3)))
         (retok (type-uaconvert type2 type3 ienv)))
        ((when (and (type-case type2 :struct)
                    (type-case type3 :struct)))
         (if (type-equiv type2 type3)
             (retok type2)
           (reterr (msg "Struct types ~x0 and ~x1 are incompatible."
                        type2 type3))))
        ((when (and (type-case type2 :union)
                    (type-case type3 :union)))
         (retok (type-union)))
        ((when
             (if (type-case type2 :pointer)
                 (or (type-case type3 :pointer)
                     (expr-null-pointer-constp (expr-cond->else expr)
                                               type3))
               (and (if (expr-cond->then expr)
                        (expr-null-pointer-constp (expr-cond->then expr)
                                                  type2)
                      (expr-null-pointer-constp (expr-cond->test expr)
                                                type2))
                    (type-case type3 :pointer))))
         (retok (type-pointer))))
       (reterr
        (msg
         "In the conditional expression ~x0, ~
                      the second operand has type ~x1 ~
                      and the third operand has type ~x2."
         (expr-fix expr)
         (type-fix type-then)
         (type-fix type-else))))))

    Theorem: typep-of-valid-cond.type

    (defthm typep-of-valid-cond.type
      (b* (((mv acl2::?erp ?type)
            (valid-cond expr
                        type-test type-then type-else ienv)))
        (typep type))
      :rule-classes :rewrite)

    Theorem: valid-cond-of-expr-fix-expr

    (defthm valid-cond-of-expr-fix-expr
      (equal (valid-cond (expr-fix expr)
                         type-test type-then type-else ienv)
             (valid-cond expr
                         type-test type-then type-else ienv)))

    Theorem: valid-cond-expr-equiv-congruence-on-expr

    (defthm valid-cond-expr-equiv-congruence-on-expr
      (implies
           (expr-equiv expr expr-equiv)
           (equal (valid-cond expr type-test type-then type-else ienv)
                  (valid-cond expr-equiv
                              type-test type-then type-else ienv)))
      :rule-classes :congruence)

    Theorem: valid-cond-of-type-fix-type-test

    (defthm valid-cond-of-type-fix-type-test
      (equal (valid-cond expr (type-fix type-test)
                         type-then type-else ienv)
             (valid-cond expr
                         type-test type-then type-else ienv)))

    Theorem: valid-cond-type-equiv-congruence-on-type-test

    (defthm valid-cond-type-equiv-congruence-on-type-test
      (implies
           (type-equiv type-test type-test-equiv)
           (equal (valid-cond expr type-test type-then type-else ienv)
                  (valid-cond expr type-test-equiv
                              type-then type-else ienv)))
      :rule-classes :congruence)

    Theorem: valid-cond-of-type-fix-type-then

    (defthm valid-cond-of-type-fix-type-then
      (equal (valid-cond expr type-test (type-fix type-then)
                         type-else ienv)
             (valid-cond expr
                         type-test type-then type-else ienv)))

    Theorem: valid-cond-type-equiv-congruence-on-type-then

    (defthm valid-cond-type-equiv-congruence-on-type-then
      (implies
           (type-equiv type-then type-then-equiv)
           (equal (valid-cond expr type-test type-then type-else ienv)
                  (valid-cond expr type-test
                              type-then-equiv type-else ienv)))
      :rule-classes :congruence)

    Theorem: valid-cond-of-type-fix-type-else

    (defthm valid-cond-of-type-fix-type-else
      (equal (valid-cond expr
                         type-test type-then (type-fix type-else)
                         ienv)
             (valid-cond expr
                         type-test type-then type-else ienv)))

    Theorem: valid-cond-type-equiv-congruence-on-type-else

    (defthm valid-cond-type-equiv-congruence-on-type-else
      (implies
           (type-equiv type-else type-else-equiv)
           (equal (valid-cond expr type-test type-then type-else ienv)
                  (valid-cond expr type-test
                              type-then type-else-equiv ienv)))
      :rule-classes :congruence)

    Theorem: valid-cond-of-ienv-fix-ienv

    (defthm valid-cond-of-ienv-fix-ienv
      (equal (valid-cond expr type-test
                         type-then type-else (ienv-fix ienv))
             (valid-cond expr
                         type-test type-then type-else ienv)))

    Theorem: valid-cond-ienv-equiv-congruence-on-ienv

    (defthm valid-cond-ienv-equiv-congruence-on-ienv
      (implies
           (ienv-equiv ienv ienv-equiv)
           (equal (valid-cond expr type-test type-then type-else ienv)
                  (valid-cond expr type-test
                              type-then type-else ienv-equiv)))
      :rule-classes :congruence)