Create a variable in a computation state.
(create-var var val compst) → result
If there are no frames, we add the variable to the static storage; otherwise, we add the variable to the top scope of the top frame. The variable comes with a value. If there is already a variable with the same name (in the static storage or in the top scope of the top frame), we return an error: C disallows variable redefinition. However, there may well be a variable with the same in a different scope: in this case, the new variable hides the other one.
Prior to storing the value, we remove its flexible array member, if any. See remove-flexible-array-member.
Function:
(defun create-var (var val compst) (declare (xargs :guard (and (identp var) (valuep val) (compustatep compst)))) (b* ((var (ident-fix var)) ((when (equal (compustate-frames-number compst) 0)) (b* ((static (compustate->static compst)) (pair (omap::assoc var static)) ((when (consp pair)) (error (list :var-redefinition var))) (new-static (omap::update var (remove-flexible-array-member val) static)) (new-compst (change-compustate compst :static new-static))) new-compst)) (frame (top-frame compst)) (scopes (frame->scopes frame)) (scope (car scopes)) (pair (omap::assoc var scope)) ((when (consp pair)) (error (list :var-redefinition var))) (new-scope (omap::update var (remove-flexible-array-member val) scope)) (new-scopes (cons new-scope (cdr scopes))) (new-frame (change-frame frame :scopes new-scopes)) (new-compst (push-frame new-frame (pop-frame compst)))) new-compst))
Theorem:
(defthm compustate-resultp-of-create-var (b* ((result (create-var var val compst))) (compustate-resultp result)) :rule-classes :rewrite)
Theorem:
(defthm compustate-frames-number-of-create-var (b* ((?result (create-var var val compst))) (implies (compustatep result) (equal (compustate-frames-number result) (compustate-frames-number compst)))))
Theorem:
(defthm compustate-scopes-numbers-of-create-var (implies (> (compustate-frames-number compst) 0) (b* ((?result (create-var var val compst))) (implies (compustatep result) (equal (compustate-scopes-numbers result) (compustate-scopes-numbers compst))))))
Theorem:
(defthm assoc-of-compustate-static-of-create-var (b* ((compst1 (create-var var val compst))) (implies (and (not (errorp compst1)) (identp var)) (equal (omap::assoc var1 (compustate->static compst1)) (if (and (equal var1 var) (equal (compustate-frames-number compst) 0)) (cons var (remove-flexible-array-member val)) (omap::assoc var1 (compustate->static compst)))))))
Theorem:
(defthm compustate->static-of-create-var-when-frames (b* ((compst1 (create-var var val compst))) (implies (and (not (errorp compst1)) (> (compustate-frames-number compst) 0)) (equal (compustate->static compst1) (compustate->static compst)))))
Theorem:
(defthm compustate->static-of-create-var (implies (not (errorp (create-var var val compst))) (equal (compustate->static (create-var var val compst)) (if (> (compustate-frames-number compst) 0) (compustate->static compst) (omap::update (ident-fix var) (remove-flexible-array-member val) (compustate->static compst))))))
Theorem:
(defthm compustate->heap-of-create-var (b* ((compst1 (create-var var val compst))) (implies (not (errorp compst1)) (equal (compustate->heap compst1) (compustate->heap compst)))))
Theorem:
(defthm exit-scope-of-create-var (implies (and (> (compustate-frames-number compst) 0) (> (compustate-top-frame-scopes-number compst) 1) (not (errorp (create-var var val compst)))) (equal (exit-scope (create-var var val compst)) (exit-scope compst))))
Theorem:
(defthm pop-frame-of-create-var (implies (and (> (compustate-frames-number compst) 0) (not (errorp (create-var var val compst)))) (equal (pop-frame (create-var var val compst)) (pop-frame compst))))
Theorem:
(defthm create-var-of-ident-fix-var (equal (create-var (ident-fix var) val compst) (create-var var val compst)))
Theorem:
(defthm create-var-ident-equiv-congruence-on-var (implies (ident-equiv var var-equiv) (equal (create-var var val compst) (create-var var-equiv val compst))) :rule-classes :congruence)
Theorem:
(defthm create-var-of-value-fix-val (equal (create-var var (value-fix val) compst) (create-var var val compst)))
Theorem:
(defthm create-var-value-equiv-congruence-on-val (implies (value-equiv val val-equiv) (equal (create-var var val compst) (create-var var val-equiv compst))) :rule-classes :congruence)
Theorem:
(defthm create-var-of-compustate-fix-compst (equal (create-var var val (compustate-fix compst)) (create-var var val compst)))
Theorem:
(defthm create-var-compustate-equiv-congruence-on-compst (implies (compustate-equiv compst compst-equiv) (equal (create-var var val compst) (create-var var val compst-equiv))) :rule-classes :congruence)