• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Community
    • 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
        • Bv
        • Imp-language
        • Event-macros
        • Java
        • Bitcoin
        • Ethereum
        • Yul
        • Zcash
        • ACL2-programming-language
        • Prime-fields
        • Json
        • Syntheto
          • Process-syntheto-toplevel-fn
          • Translation
          • Language
            • Static-semantics
              • Check-expression-fns
              • Subtypep
              • Match-type
              • Check-product-update-expression
              • Get-builtin-function-in/out/pre-post
              • Check-sum-update-expression
              • Check-sum-field-expression
                • Check-strict-binary-expression
                • Check-lt/le/gt/ge-expression
                • Check-eq/ne-expression
                • Check-div/rem-expression
                • Check-add/sub/mul-expression
                • Align-let-vars-values
                • Check-iff-expression
                • Check-function-definition-top/nontop
                • Check-sum-construct-expression
                • Check-rem-expression
                • Check-mul-expression
                • Check-sub-expression
                • Check-div-expression
                • Check-add-expression
                • Check-ne-expression
                • Check-lt-expression
                • Check-le-expression
                • Check-gt-expression
                • Check-ge-expression
                • Check-eq-expression
                • Check-function-specifier
                • Type-result
                • Check-product-construct-expression
                • Supremum-type
                • Check-call-expression
                • Check-product-field-expression
                • Check-function-definer
                • Make-subproof-obligations
                • Get-function-in/out/pre/post
                • Check-sum-test-expression
                • Match-field
                • Decompose-expression
                • Match-to-target
                • Check-unary-expression
                • Max-supertype
                • Match-type-list
                • Check-minus-expression
                • Check-type-definition
                • Check-not-expression
                • Check-type-product
                • Match-field-list
                • Check-type-subset
                • Check-type-definition-in-recursion
                • Align-let-vars-values-aux
                • Non-trivial-proof-obligation
                • Check-type-recursion
                • Check-function-specification
                • Check-toplevel
                • Supremum-type-list
                • Check-component-expression
                • Check-branch-list
                • Check-function-recursion
                • Check-function-definition
                • Binding
                • Check-function-header
                • Check-function-definition-list
                • Check-type-definition-list-in-recursion
                • Check-theorem
                • Check-nonstrict-binary-expression
                • Context-add-variables
                • Decompose-expression-aux
                • Check-alternative
                • Check-multi-expression
                • Check-type-sum
                • Check-type
                • Check-alternative-list
                • Context-add-condition
                • Check-type-definer
                • Check-transform
                • Check-variable
                • Check-transform-args
                • Check-toplevel-list
                • Context-add-condition-list
                • Check-if/when/unless-expression
                • Initializers-to-variable-substitution
                • Context-add-binding
                • Check-function-header-list
                • Context-add-toplevel
                • Ensure-single-type
                • Max-supertypes
                • Check-bind-expression
                • Check-type-list
                • Check-literal
                • Literal-type
                • Check-expression-list
                • Variable-context
                • Check-cond-expression
                • Check-branch
                • Args-without-defaults
                • Check-expression
                • *builtin-function-names*
                • Function-called-in
              • Abstract-syntax
              • Outcome
              • Abstract-syntax-operations
              • Outcome-list
              • Outcomes
            • Process-syntheto-toplevel
            • Shallow-embedding
          • File-io-light
          • Cryptography
          • Number-theory
          • Lists-light
          • Axe
          • Builtins
          • Solidity
          • Helpers
          • Htclient
          • Typed-lists-light
          • Arithmetic-light
        • X86isa
        • Axe
        • Execloader
      • Math
      • Testing-utilities
    • Static-semantics

    Check-sum-field-expression

    Check if a sum field expression is statically well-formed.

    Signature
    (check-sum-field-expression tname target target-result 
                                alternative field expr ctxt) 
     
      → 
    result
    Arguments
    tname — Guard (identifierp tname).
    target — Guard (expressionp target).
    target-result — Guard (type-resultp target-result).
    alternative — Guard (identifierp alternative).
    field — Guard (identifierp field).
    expr — Guard (expressionp expr).
    ctxt — Guard (contextp ctxt).
    Returns
    result — Type (type-resultp result).

    The type of the expression must be a defined sum type, or a subtype of a defined sum type: we calculate the maximal supertype of the expression's type, and ensure that it is a defined sum type; since sum types are not subtypes, calculating the maximal supertype cannot ``skip'' a sum type.

    We generate a proof obligation saying that the target expression has the specified alternative.

    We find the type of the field in the alternative of the sum type: this is the type of the whole expression.

    Definitions and Theorems

    Function: check-sum-field-expression

    (defun check-sum-field-expression
           (tname target target-result
                  alternative field expr ctxt)
     (declare (xargs :guard (and (identifierp tname)
                                 (expressionp target)
                                 (type-resultp target-result)
                                 (identifierp alternative)
                                 (identifierp field)
                                 (expressionp expr)
                                 (contextp ctxt))))
     (let ((__function__ 'check-sum-field-expression))
      (declare (ignorable __function__))
      (type-result-case
       target-result :err
       (type-result-err target-result.info)
       :ok
       (b*
         ((type (ensure-single-type target-result.types))
          ((when (not type))
           (type-result-err (list :multi-valued-field-target
                                  (expression-fix target)
                                  target-result.types)))
          (type (max-supertype type (context->tops ctxt)))
          ((when (not type))
           (type-result-err (list :no-max-supertype type)))
          (tsum (get-type-sum type (context->tops ctxt)))
          ((when (not tsum))
           (type-result-err (list :sum-field-type-mismatch type)))
          (oblig?
               (non-trivial-proof-obligation
                    (context->obligation-vars ctxt)
                    (context->obligation-hyps ctxt)
                    (make-expression-sum-test :type tname
                                              :target target
                                              :alternative alternative)
                    expr))
          (product
               (get-alternative-product alternative
                                        (type-sum->alternatives tsum)))
          ((when (not product))
           (type-result-err (list :sum-field-no-alternative
                                  tsum (identifier-fix alternative))))
          (ftype (get-field-type field (type-product->fields product)))
          ((when (not ftype))
           (type-result-err (list :no-field-in-product
                                  (identifier-fix field)
                                  product))))
        (make-type-result-ok
             :types (list ftype)
             :obligations (append target-result.obligations oblig?))))))

    Theorem: type-resultp-of-check-sum-field-expression

    (defthm type-resultp-of-check-sum-field-expression
      (b*
        ((result
              (check-sum-field-expression tname target target-result
                                          alternative field expr ctxt)))
        (type-resultp result))
      :rule-classes :rewrite)

    Theorem: check-sum-field-expression-of-identifier-fix-tname

    (defthm check-sum-field-expression-of-identifier-fix-tname
      (equal (check-sum-field-expression (identifier-fix tname)
                                         target target-result
                                         alternative field expr ctxt)
             (check-sum-field-expression tname target target-result
                                         alternative field expr ctxt)))

    Theorem: check-sum-field-expression-identifier-equiv-congruence-on-tname

    (defthm
        check-sum-field-expression-identifier-equiv-congruence-on-tname
     (implies
       (identifier-equiv tname tname-equiv)
       (equal
            (check-sum-field-expression tname target target-result
                                        alternative field expr ctxt)
            (check-sum-field-expression tname-equiv target target-result
                                        alternative field expr ctxt)))
     :rule-classes :congruence)

    Theorem: check-sum-field-expression-of-expression-fix-target

    (defthm check-sum-field-expression-of-expression-fix-target
      (equal (check-sum-field-expression tname (expression-fix target)
                                         target-result
                                         alternative field expr ctxt)
             (check-sum-field-expression tname target target-result
                                         alternative field expr ctxt)))

    Theorem: check-sum-field-expression-expression-equiv-congruence-on-target

    (defthm
       check-sum-field-expression-expression-equiv-congruence-on-target
     (implies
       (expression-equiv target target-equiv)
       (equal
            (check-sum-field-expression tname target target-result
                                        alternative field expr ctxt)
            (check-sum-field-expression tname target-equiv target-result
                                        alternative field expr ctxt)))
     :rule-classes :congruence)

    Theorem: check-sum-field-expression-of-type-result-fix-target-result

    (defthm check-sum-field-expression-of-type-result-fix-target-result
     (equal
      (check-sum-field-expression tname
                                  target (type-result-fix target-result)
                                  alternative field expr ctxt)
      (check-sum-field-expression tname target target-result
                                  alternative field expr ctxt)))

    Theorem: check-sum-field-expression-type-result-equiv-congruence-on-target-result

    (defthm
     check-sum-field-expression-type-result-equiv-congruence-on-target-result
     (implies
       (type-result-equiv target-result target-result-equiv)
       (equal
            (check-sum-field-expression tname target target-result
                                        alternative field expr ctxt)
            (check-sum-field-expression tname target target-result-equiv
                                        alternative field expr ctxt)))
     :rule-classes :congruence)

    Theorem: check-sum-field-expression-of-identifier-fix-alternative

    (defthm check-sum-field-expression-of-identifier-fix-alternative
      (equal (check-sum-field-expression tname target target-result
                                         (identifier-fix alternative)
                                         field expr ctxt)
             (check-sum-field-expression tname target target-result
                                         alternative field expr ctxt)))

    Theorem: check-sum-field-expression-identifier-equiv-congruence-on-alternative

    (defthm
     check-sum-field-expression-identifier-equiv-congruence-on-alternative
     (implies
      (identifier-equiv alternative alternative-equiv)
      (equal
        (check-sum-field-expression tname target target-result
                                    alternative field expr ctxt)
        (check-sum-field-expression tname target target-result
                                    alternative-equiv field expr ctxt)))
     :rule-classes :congruence)

    Theorem: check-sum-field-expression-of-identifier-fix-field

    (defthm check-sum-field-expression-of-identifier-fix-field
     (equal
          (check-sum-field-expression tname target target-result
                                      alternative (identifier-fix field)
                                      expr ctxt)
          (check-sum-field-expression tname target target-result
                                      alternative field expr ctxt)))

    Theorem: check-sum-field-expression-identifier-equiv-congruence-on-field

    (defthm
        check-sum-field-expression-identifier-equiv-congruence-on-field
     (implies
      (identifier-equiv field field-equiv)
      (equal
        (check-sum-field-expression tname target target-result
                                    alternative field expr ctxt)
        (check-sum-field-expression tname target target-result
                                    alternative field-equiv expr ctxt)))
     :rule-classes :congruence)

    Theorem: check-sum-field-expression-of-expression-fix-expr

    (defthm check-sum-field-expression-of-expression-fix-expr
      (equal (check-sum-field-expression
                  tname target target-result
                  alternative field (expression-fix expr)
                  ctxt)
             (check-sum-field-expression tname target target-result
                                         alternative field expr ctxt)))

    Theorem: check-sum-field-expression-expression-equiv-congruence-on-expr

    (defthm
         check-sum-field-expression-expression-equiv-congruence-on-expr
     (implies
      (expression-equiv expr expr-equiv)
      (equal
        (check-sum-field-expression tname target target-result
                                    alternative field expr ctxt)
        (check-sum-field-expression tname target target-result
                                    alternative field expr-equiv ctxt)))
     :rule-classes :congruence)

    Theorem: check-sum-field-expression-of-context-fix-ctxt

    (defthm check-sum-field-expression-of-context-fix-ctxt
     (equal
      (check-sum-field-expression tname target target-result alternative
                                  field expr (context-fix ctxt))
      (check-sum-field-expression tname target target-result
                                  alternative field expr ctxt)))

    Theorem: check-sum-field-expression-context-equiv-congruence-on-ctxt

    (defthm check-sum-field-expression-context-equiv-congruence-on-ctxt
     (implies
      (context-equiv ctxt ctxt-equiv)
      (equal
        (check-sum-field-expression tname target target-result
                                    alternative field expr ctxt)
        (check-sum-field-expression tname target target-result
                                    alternative field expr ctxt-equiv)))
     :rule-classes :congruence)