Haskell - determine set of free and bounded variables of a function -


i have determine set of free , bounded variables of function s1 , s2:

s1 := \x -> if y \z -> (x \y -> y) else (\z -> w) x 

so, s1 i'll write:

fv(s1):= fv (y) ∪ fv (x) ∪ fv (w) 

am correct? or should be:

fv(s1):= fv (y) ∪ fv (x) ∪ fv (y) ∪ fv (w) ∪ fv (x) 

since y , x 2 times free. once y in if , result of -> y , x: x free once in result of \z , second @ end.

the bounded variables be:

bv(s1):= bv (x) ∪ bv (z) ∪ bv (y) ∪ bv (z) 

since z occurs twice bounded var.

in same way i'd determine fvs , bvs of s2:

s2 := let f x1  x2 = y1  (\x -> x2) in let y1 = f w (f y2  y2), y2 = y1  in f  fv(s2):= fv (y1) ∪ fv (x2) ∪ fv (w) ∪ fv (y1)  bv(s2):= bv (f) ∪ bv (x1) ∪ bv (x2) ∪ bv (x) ∪ bv (y1) 

could please tell me whether i'm correct or wrong?

thanks in advance

general idea: bound vs free variables

a rule of thumb free , bound variables value of free variable affects value of expression.

for simple example, if defined identity x = x , separately said x = 6, wouldn't change fact identity 10 10; in identity x = x, variable x bound because stands whatever input there is. can write identity y = y without changing meaning of function.

conversely, if define gimmez x = z, z isn't bound. if separately z = 6 gimmez different if said z = putstrln "zed zed zed zed can tell i'm british?", or gimmez x = zz. in either case, function gimmez has changed - z wasn't bound.

can find-and-replace varname othervarname without changing meaning?

  • yes - varname bound.
  • no - varname appears free in expression.

let's @ actual examples

example 1

s1 := \x -> if y \z -> (x \y -> y) else (\z -> w)  

you wrote fv(s1):= fv (y) ∪ fv (x) ∪ fv (w) or fv(s1):= fv (y) ∪ fv (x) ∪ fv (y) ∪ fv (w) ∪ fv (x)

let's @ variables in turn

  • the initial \x -> binds x scope of lambda - end of line. it's not free variable @ all.
  • y free after if, bound in invalid expression (x \y -> y). second occurrence in bracket has value of bound y, rather free one. obliteration of otherwise free variable called shadowing. y free in expression, because of first appearance.
  • z bound in final lambda expression.
  • w isn't bound anywhere - it's free.

summary: y , z free. x , z bound, y shadowed - occurs bound variable later in expression. fv(s1) = {y,z}, , bv(s1)={x,z,y}

notation

i disagree use of notation fv(s1):= fv (y) ∪ fv (w). suggests find free variables of s1 should @ free variables of y , w. disagree - y , z are free variables of s1. true if wanted free variables of module in s1 defined need add fv (y) ∪ fv (w), that's different question.

(the notation indicates set union. things either in or out of set, don't need add them multiple times.)

example 2

s2 := let f x1  x2 = y1  (\x -> x2) in let y1 = f w (f y2  y2), y2 = y1  in f 

the let f x1 x2 = binds f, x1 , x2, if f feels different; introduce new variable shadow external definition same name, , find-and-replacing them in expression wouldn't change meaning, they're bound.

  • f, x1, x2 bound let.
  • y1 free first time appears - second let binding not in scope here.
  • x bound lambda
  • y1 bound let (more shadowing)
  • w free
  • y2 looks free @ first, retrospectively bound in expression y2 = y1
fv(s2):= {y1, w} bv(s2):= {f, x1, x2, x, y1, y2} 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -