0

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)))
ben rudgers
  • 3,647
  • 2
  • 20
  • 32
vlada
  • 167
  • 15
  • 1
    Could you be more specific than "it dont works"? What happens that shouldn't, or doesn't happen but should? – molbdnilo Jan 20 '15 at 13:06

1 Answers1

0

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))
David Merinos
  • 1,195
  • 1
  • 14
  • 36
  • Yes, probably that was error, but it still not working as it should. For example (shadowed? (id 'x)) should return #t, and (shadowed? (num 1)) should return #f. I get here error – vlada Jan 21 '15 at 21:29
  • @vladang Why should `(shadowed? (id 'x))` be `#t`? Your case for `id` says `error 'shadowed-variable? "free variable")`. Also, try `(remove-duplicates (flatten 1) symbol=?)` in the REPL and see what happens. – molbdnilo Jan 22 '15 at 10:34