Skip to content

Instantly share code, notes, and snippets.

@anlun
Created February 25, 2016 15:37
Show Gist options
  • Save anlun/30d0a5c3f3ee28663490 to your computer and use it in GitHub Desktop.
Save anlun/30d0a5c3f3ee28663490 to your computer and use it in GitHub Desktop.
A semantics with macros rules. Redex.
#lang racket
(require redex/reduction-semantics)
(require (for-syntax racket/match))
(define-language L
(E ::= (list v ... E e ...) (car E) (cdr E) hole)
(v ::= (list v ...) natural)
(e ::= (list e ...) (car e) (cdr e) natural))
(define-syntax-rule (==> a b)
(--> (in-hole E a) (in-hole E b)))
(define-syntax (reverse-me stx)
(datum->syntax stx (reverse (cdr (syntax->datum stx)))))
(define-syntax (reduction-relation-my stx)
(datum->syntax stx
(append
'(reduction-relation L)
(map (λ (x)
(match x
[`(==> ,from ,to)
`(--> (in-hole E ,from) (in-hole E ,to))]
[_ x]))
(cdr (syntax->datum stx))))))
(define red
(reduction-relation-my
(==> (car (list v_1 v_2 ...)) v_1)
(==> (cdr (list v_1 v_2 ...)) (list v_2 ...))))
(test-->> red
(term (list (car (cdr (cdr (cdr (list 0 1 2 3 4)))))))
(term (list 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment