Scheme Racket Shadowed variable checking

393 Views Asked by At

I need function to check is variable shadowed or not? Function should return #t or #f based on that is variable shadowed or not. I used DrRacket for implementing code (#lang plai). So far, I have this...

#lang plai

(define-type WAE
  [num (n number?)]
  [add (lhs WAE?)(rhs WAE?)]
  [sub (lhs WAE?)(rhs WAE?)]
  [with (name symbol?)(named-expr WAE?)(body WAE?)]
  [id (name symbol?)])

(define (parse sexpr)
  (cond
    [(number? sexpr) (num sexpr)]
    [(symbol? sexpr) (id sexpr)]
    [(list? sexpr)
      (case (first sexpr)
        [(add) (add (parse (second sexpr)) (parse (third sexpr)))]
        [(sub) (sub (parse (second sexpr)) (parse (third sexpr)))]
        [(with) (with (first (second sexpr)) 
                      (parse (second (second sexpr)))
                      (parse(third sexpr)) )]]
     [else (error "unexpected token")]))

(define (symbol<? a b)
  (string<? (symbol->string a) (symbol->string b)))

This is function that checks if variable is shadowed, but it dont works

(define (shadowed? wae)
  (let 
    ([l(type-case WAE wae
      [num (n) n]
      [add (lft rght) (+ (shadowed-variable? lft) (shadowed-variable? rght))]
      [sub (lft rght) (- (shadowed-variable? lft) (shadowed-variable? rght))]
      [with (x i b) (shadowed-variable? (sub b x (shadowed-variable? i)))]
      [id (s) (error 'shadowed-variable? "free variable")])]) 
    (if (remove-duplicates (flatten l) symbol=?) #t #f)))
1

There are 1 best solutions below

2
On

I don't know #lang plai but I believe you might have a syntax error in the second part of your code.

(define (shadowed? wae)
  (let 
   >>> Possible error >>> ([l(type-case WAE wae
      [num (n) n]
      [add (lft rght) (+ (shadowed-variable? lft) (shadowed-variable? rght))]
      [sub (lft rght) (- (shadowed-variable? lft) (shadowed-variable? rght))]
      [with (x i b) (shadowed-variable? (sub b x (shadowed-variable? i)))]
      [id (s) (error 'shadowed-variable? "free variable")])]) 
    >>>> Error >>> (if (remove-duplicates (flatten l) symbol=?) #t #f)))

Try this and let us know.

(define (shadowed? wae)
  (let 
    ([l (type-case WAE wae
      [num (n) n]
      [add (lft rght) (+ (shadowed-variable? lft) (shadowed-variable? rght))]
      [sub (lft rght) (- (shadowed-variable? lft) (shadowed-variable? rght))]
      [with (x i b) (shadowed-variable? (sub b x (shadowed-variable? i)))]
      [id (s) (error 'shadowed-variable? "free variable")])]) 
    (if (remove-duplicates (flatten l)) symbol=?) #t #f))