Question: # | We will define the syntax and semantics for SAEL, the Simple Arithmetic Expression Language. SAEL is our object language. The metalanguage we (

#|
We will define the syntax and semantics for SAEL, the Simple
Arithmetic Expression Language. SAEL is our object language. The
metalanguage we (well, you) will use to define SAEL is ACL2s. The
meta-metalanguage we (well, us -- Ferdinand & Olin, in this
assignment) use to discuss all of this is Mathematical English.
Here's a "Fundies 1" style data definition.
A simple arithmetic expression, saexpr, is one of the following:
- a rational number (we use the builtin ACL2s type rational)
- a variable (we use the builtin ACL2s type var)
- a unary expression, which is a list of the form
(-) ; Negation
(/) ; Reciprocal
where is an arithmetic expression
- a binary expression, which is a list of the form
()
where is one of +,-,*,/ or ^
and both 's are simple arithmetic expressions.
|#
; It really isn't important to know the exact definition of varp,
; although you can query ACL2s with ":pe varp". Just notice that none
; of the operators are vars, but symbols such as x, y, z, etc are
; vars.
(check=(varp '-) nil)
(check=(varp '+) nil)
(check=(varp '*) nil)
(check=(varp '/) nil)
(check=(varp '^) nil)
(check (varp 'x))
;; Q1
;; Use defdata to define the unary operators. Fill in the XXXs
;; below. If you have questions, ask on Piazza.
(defdata uoper (enum XXX))
;; Q2
;; Use defdata to define boper the binary operators
(defdata boper (enum XXX))
(check (boperp '*))
(check (boperp '^))
(check=(boperp '!) nil)
;; Q3
;; Use defdata to define saexpr. We will want names for all the
;; subtypes, so use the following template. Note that data definitions
;; can be mutually recursive.
(defdata
(saexpr (or rational ; or is the same as oneof
var
usaexpr ; unary sael expression
bsaexpr)) ; binary sael expression
(usaexpr XXX)
(bsaexpr XXX))
;; We now have a formal definition of the SAEL language! That is, we
;; have a recognizer saexprp, which is defined in our metalanguage,
;; ACL2s, and which recognizes expressions in the object language,
;; SAEL. We formalized the syntax of SAEL expressions! Notice that
;; defdata made this very easy to do.
;; In short, a SAEL expression can now be represented as a
;; piece of ACL2 data, so we can operate on one with ACL2 code.
(check (saexprp '((x + y)-(/ z))))
(check=(saexprp '(x + y + z)) nil)
;; We are now going to define the semantics of SAEL expressions.
;; First, some helper definitions.
;; An assignment is an alist from vars to rationals.
;; An alist is just a list of conses.
;; In the RAP notes, the term "valuation" is also used instead of assignment.
(defdata assignment (alistof var rational))
(check (assignmentp '((x .1)(y .1/2))))
;; This is nil because (1),(1/2) are not rationals.
(check=(assignmentp '((x 1)(y 1/2))) nil)
;; Now, on to the semantics.
;; How do we assign semantics to SAEL expressions such as
;; (x + y)?
;; We will define saeval (below), a function that given an saexpr and
;; an assignment evaluates the expression, using the assignment to
;; specify the values of var's.
;; If a var appears in the saexpr but is not in the assignment, then
;; the value of the var should be 1.
;; Q4
;; Use the following helper function to lookup the value of v in a.
;; (Remember return 1 if v isn't in a.)
(definec lookup (v :var a :assignment) :rational
XXX)
# | We will define the syntax and semantics for

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!