racketsemanticsplt-redex

Trying to define a small language with redex


I'm following the amb tutorial for Redex and, at the same time, building a model for typed arithmetic expressions, as found in Pierce's Types and Programming Languages.

I have defined the syntax and type system for such small language, but I'm in trouble to define its small step semantics. Before I got to the problems, let me present the definitions I've got so far.

First, I've defined the syntax of the language.

(define-language ty-exp
  [E  (ttrue)
      (ffalse)
      (zero)
      (suc E)
      (ppred E)
      (iszero E)
      (iff E E E)]
  [T (nat)
     (bool)])

Next, I defined the type system without problems.

(define-judgment-form ty-exp
  #:mode (types I O)
  #:contract (types E T)

  [
   ----------------------"T-zero"
    (types (zero) (nat))
  ]

  [
   -------------------------- "T-false"
     (types (ffalse) (bool))
  ]

  [
   -------------------------- "T-true"
     (types (ttrue) (bool))
  ]

  [
     (types E (nat))
   -------------------------- "T-suc"
     (types (suc E) (nat))
  ]

  [
     (types E (nat))
   -------------------------- "T-pred"
     (types (ppred E) (nat))
  ]

  [
     (types E (nat))
   -------------------------- "T-iszero"
     (types (iszero E) (bool))
  ]

  [
   (types E_1 (bool))
   (types E_2 T_1)
   (types E_3 T_1)
   -------------------------- "T-iff"
   (types (iff E_1 E_2 E_3) (T_1))
  ]
)

As far as I understand, we need to define semantics using evaluation contexts. So my next step was to define such contexts and values for the language.

(define-extended-language ty-exp-ctx-val ty-exp
  (C (suc C)
     (ppred C)
     (iszero C)
     (iff C E E)
     hole)
  (NV (zero)
      (suc NV))
  (BV (ttrue)
      (ffalse))
  (V  (NV)
      (BV)))

Non-terminal C stands for contexts, NV for numerical values, BV for boolean values and V for values. Using the definition of values, I defined a function for testing if an expression is a value.

(define v? (redex-match ty-exp-ctx-val V))

Using this setup, I tried to defined the operational semantics for this language. In Pierce's book, such semantics (without evaluation contexts) is as follows:

e --> e'
---------------- (E-suc)
suc e --> suc e'

------------------ (E-pred-zero)
pred zero --> zero

      NV e
------------------- (E-pred-succ)
pred (suc e) --> e

e --> e'
------------------- (E-pred)
pred e --> pred e'


-------------------- (E-iszero-zero)
iszero zero --> true

NV e
------------------------ (E-iszero-succ)
iszero (suc e) --> false


e --> e'
-------------------------(E-iszero)
iszero e --> iszero e'

---------------------- (E-if-true)
if true e e' --> e

-----------------------(E-if-false)
if false e e' --> e'

e --> e'
-----------------------(E-if)
if e e1 e2 --> if e' e1 e2

In order to express such semantics using evaluation contexts, I removed rules E-suc, E-pred, E-izero and E-if and defined a rule for stepping in an expression context:

e --> e'
--------------(E-context)
E[e] --> E[e']

As far as I understand, we don't need to represent such context rule in redex. So, I have defined the semantics for this language as:

(define red
  (reduction-relation
   ty-exp-ctx-val
   #:domain E
   (--> (in-hole C (iff (ttrue) E_1 E_2))
        (in-hole C E_1)
        "E-if-true")
   (--> (in-hole C (iff (ffalse) E_1 E_2))
        (in-hole C E_2)
        "E-if-false")
   (--> (in-hole C (iszero (zero)))
        (in-hole C (ttrue))
        "E-iszero-zero")
   (--> (in-hole C (iszero (suc (E))))
        (in-hole C (ffalse))
        (side-condition (v? (term E)))
        "E-iszero-suc")
   (--> (in-hole C (ppred (zero)))
        (in-hole C (zero))
        "E-pred-zero")
   (--> (in-hole C (ppred (suc (E))))
        (in-hole C (E))
        (side-condition (v? (term E)))
        "E-pred-suc")
 ))

Now, we come to the problem: When I tried to execute

(traces red (term (iif (iszero zero) ttrue ffalse)))

Racket returns the following error message:

 reduction-relation: relation not defined for (iif (iszero (zero)) (ttrue) (ffalse))

Surely, I'm doing something silly, but I can't figure out what. Could someone help me with this?


Solution

  • After running the program, I see what the problem is.

    Try:

    (traces red (term (iff (iszero (zero)) (ttrue) (ffalse))))
    

    In

    (define-language ty-exp
      [E  (ttrue)
          (ffalse)
          (zero)
          (suc E)
          (ppred E)
          (iszero E)
          (iff E E E)]
      [T (nat)
         (bool)])
    

    you have parentheses around ttrue, ffalse and zero.