3

I'm trying to prepare semantics for a language. Some derivation may lead to a 'stuck' state. I want to have a test, what certain term can't be reduced to the 'stuck' state. Is it somehow possible to represent it using something like test-->E?

  • Yes, you can use `test-->E` if you have an explicit stuck term in your grammar. Is there something specific about `test-->E` that you can't get working? Obviously, your test will not complete if you give `test-->E` a non-terminating program, unless you restrict the test to a finite number of steps. – stchang Aug 03 '15 at 18:02
  • Actually I'm trying to make a test , which proves what there is no derivation to `stuck` state. Yes, I have explicit stuck term.It's OK, if a solution will work only for terminating programs. But I think it should also work for programs, for which Redex can find fixpoint. Speaking about `test-->E`, I want something like `test-->notE`. – user2208834 Aug 04 '15 at 10:32
  • Also I should add that my semantics nondeterministic one, so it's painful to use `test-->>` in this case and list all non stuck states. – user2208834 Aug 04 '15 at 10:38
  • And also i was a little bit wrong: in all cases above you should read `-->` as `-->>`. – user2208834 Aug 04 '15 at 10:39
  • If assuming termination is ok, then to test that a reduction is not stuck, can you just check that it reduces to a value? – stchang Aug 04 '15 at 17:50
  • Yes, in most cases I can. Before your answer I didn't know that it is possible to put predicate to test environment (`not-stuck?`). But in my very case it doesn't work because there is cycle in my reduction graph. It shouldn't be a problem to determine, however, that there is no `stuck` state in it, because the graph is finite, but Redex reports error. – user2208834 Aug 05 '15 at 07:49
  • What's the error you are seeing? – stchang Aug 05 '15 at 13:45
  • `found a cycle in the reduction graph` – user2208834 Aug 06 '15 at 14:25

1 Answers1

1

Here is an example adapted from the λv example on the Redex page but with a stuck state added. Is this helpful to you?

#lang racket
(require redex)

(define-language λv
  (e (e e ...) (if0 e e e) x v stuck)
  (v (λ (x ...) e) n +)
  (n natural)
  (E (v ... E e ...) (if0 E e e) hole)
  (x (variable-except λ + if0)))

(define red
  (reduction-relation
   λv
   (--> (in-hole E (+ n_1 n_2))
        (in-hole E ,(+ (term n_1) 
                       (term n_2)))
        "+")
   (--> (in-hole E (if0 0 e_1 e_2))
        (in-hole E e_1)
        "if0t")
   (--> (in-hole E (if0 number_1 e_1 e_2))
        (in-hole E e_2)
        "if0f"
        (side-condition 
          (not (= 0 (term number_1)))))
   (--> (in-hole E ((λ (x ..._1) e) v ..._1))
        (in-hole E (subst-n (x v) ... e))
        "βv")
   (--> (in-hole E (n v ..._1))
        stuck
        "βv-err")))

(define-metafunction λv
  subst-n : (x any) ... any -> any
  [(subst-n (x_1 any_1) (x_2 any_2) ... any_3)
   (subst x_1 any_1 (subst-n (x_2 any_2) ... 
                             any_3))]
  [(subst-n any_3) any_3])

(define-metafunction λv
  subst : x any any -> any
  ;; 1. x_1 bound, so don't continue in λ body
  [(subst x_1 any_1 (λ (x_2 ... x_1 x_3 ...) any_2))
   (λ (x_2 ... x_1 x_3 ...) any_2)
   (side-condition (not (member (term x_1)
                                (term (x_2 ...)))))]
  ;; 2. general purpose capture avoiding case
  [(subst x_1 any_1 (λ (x_2 ...) any_2))
   (λ (x_new ...) 
     (subst x_1 any_1
            (subst-vars (x_2 x_new) ... 
                        any_2)))
   (where (x_new ...)
          ,(variables-not-in
            (term (x_1 any_1 any_2)) 
            (term (x_2 ...))))]
  ;; 3. replace x_1 with e_1
  [(subst x_1 any_1 x_1) any_1]
  ;; 4. x_1 and x_2 are different, so don't replace
  [(subst x_1 any_1 x_2) x_2]
  ;; the last cases cover all other expressions
  [(subst x_1 any_1 (any_2 ...))
   ((subst x_1 any_1 any_2) ...)]
  [(subst x_1 any_1 any_2) any_2])

(define-metafunction λv
  subst-vars : (x any) ... any -> any
  [(subst-vars (x_1 any_1) x_1) any_1]
  [(subst-vars (x_1 any_1) (any_2 ...)) 
   ((subst-vars (x_1 any_1) any_2) ...)]
  [(subst-vars (x_1 any_1) any_2) any_2]
  [(subst-vars (x_1 any_1) (x_2 any_2) ... any_3) 
   (subst-vars (x_1 any_1) 
               (subst-vars (x_2 any_2) ... any_3))]
  [(subst-vars any) any])


(define not-stuck? (redex-match λv v))
(define stuck? (redex-match λv stuck))
(test-->>E red (term (+ 1 2)) not-stuck?)
(test-->>E red (term (1 2)) stuck?)
(test-results) ; => Both tests passed.
stchang
  • 2,555
  • 15
  • 17