CSE 230: Principles of Programming Languages
OBJ Homework Solutions

Below are the OBJ3 source code and output log for solutions to the OBJ problems in the second and third sets. Some sort of compromise is needed to solve problem 3 in set 2, because parentheses play a special role in OBJ, to show with the grouping of subterms. Here is my own favorite solution for this problem:

  obj REG is sort Reg .
    ops a b : -> Reg .
    op [_ _] : Reg Reg -> Reg .
    op _* : Reg -> Reg .
    op [_+_] : Reg Reg -> Reg .
  endo

  parse [[a b] [a b]] .
  parse [[a b] + [a b]] .
  parse [a b]* .
  parse [[([a b]*) [a b]] + [a a]] .
Notice that it uses quoted identifiers, but fails to exactly capture the syntax given by Stansifer because it uses [, ] instead of parentheses. For some reason, the OBJ3 parser really wants those parentheses in the last term to be parsed. Here is the output:
awk% obj
                     \|||||||||||||||||/
                   --- Welcome to OBJ3 ---
                     /|||||||||||||||||\
         OBJ3 version 2.04oxford built: 1994 Feb 28 Mon 15:07:40
            Copyright 1988,1989,1991 SRI International
                   2001 Jan 31 Wed 0:00:51
OBJ> in hw2
==========================================
obj REG
==========================================
parse ([ [ a b ] [ a b ] ])
parse Reg: ([([a b]) ([a b])])
==========================================
parse ([ [ a b ] + [ a b ] ])
parse Reg: ([([a b]) + ([a b])])
==========================================
parse ([ a b ] *)
parse Reg: (([a b]) *)
==========================================
parse ([ [ ( [ a b ] * ) [ a b ] ] + [ a a ] ])
parse Reg: ([([(([a b]) *) ([a b])]) + ([a a])])
==========================================
OBJ> q
Bye.
awk% 
Here is a more elegant solution, which however uses a more advanced feature, the associative attribute for a binary operation:
  obj REG is sort Reg .
    ops a b : -> Reg .
    op _ _ : Reg Reg -> Reg [assoc prec 20] .
    op _* : Reg -> Reg .
    op _+_ : Reg Reg -> Reg [assoc prec 30] .
  endo

  parse (a b) (a b).
  parse a b + a b .
  parse (a b)* .
  parse (((a b)*) (a b)) + (a a) .
However, this also fails to capture the exact syntax given by Stansifer, because expressions like a b a b are allowed. Here is the output:
awk% obj
                     \|||||||||||||||||/
                   --- Welcome to OBJ3 ---
                     /|||||||||||||||||\
         OBJ3 version 2.04oxford built: 1994 Feb 28 Mon 15:07:40
            Copyright 1988,1989,1991 SRI International
                   2001 Jan 31 Wed 1:27:32
OBJ> in hw23
==========================================
obj REG
==========================================
parse (( a b ) ( a b ))
parse Reg: ((a b) (a b))
==========================================
parse (a b + a b)
parse Reg: ((a b) + (a b))
==========================================
parse (( a b ) *)
parse Reg: ((a b) *)
==========================================
parse (( ( ( a b ) * ) ( a b ) ) + ( a a ))
parse Reg: ((((a b) *) (a b)) + (a a))
OBJ> q
Bye.
awk% 

Now here is OBJ3 code solving problem 3 of set 3 (it is due to Kai Lin):
  obj NATEXP is sort Exp .
    op 0 : -> Exp .
    op s_ : Exp -> Exp .
    op _+_ : Exp Exp -> Exp .
    op _*_ : Exp Exp -> Exp .
    ops x y z : -> Exp .
  endo

  obj INTL is pr NATEXP .
    pr NAT .
    vars E E1 E2 : Exp .
    op [[_]] : Exp -> Nat .
    eq [[x]] = 1 .
    eq [[y]] = 2 .
    eq [[z]] = 3 .
    eq [[0]] = 0 .
    eq [[ (s E) ]] = s [[ E ]] .
    eq [[ E1 + E2 ]] = [[ E1 ]] + [[ E2 ]] .
    eq [[ E1 * E2 ]] = [[ E1 ]] * [[ E2 ]] .
  endo

  red [[ s(x + y) + z ]] .
  red [[ (x * y) + s s 0 ]] .
  red [[ (s s 0 * x) + s(y * z) ]] . 
The operation [[_]] is the denotation function. Here is the output:
awk% obj
                     \|||||||||||||||||/
                   --- Welcome to OBJ3 ---
                     /|||||||||||||||||\
         OBJ3 version 2.04oxford built: 1994 Feb 28 Mon 15:07:40
            Copyright 1988,1989,1991 SRI International
                   2001 Jan 31 Wed 0:05:50
OBJ> in hw3
==========================================
obj NATEXP
==========================================
obj INTL
==========================================
reduce in INTL : [[s (x + y) + z]]
rewrites: 9
result NzNat: 7
==========================================
reduce in INTL : [[(x * y) + s (s 0)]]
rewrites: 11
result NzNat: 4
==========================================
reduce in INTL : [[(s (s 0) * x) + s (y * z)]]
rewrites: 16
result NzNat: 9
OBJ> q
Bye.
awk% 


To CSE 230 homepage
To CSE 230 homework page
Maintained by Joseph Goguen
Last modified: Wed Jan 31 13:53:15 PST 2001