% $Id: FreeVarIds.oz,v 1.6 2009/01/10 19:21:35 leavens Exp $ % AUTHOR: Gary T. Leavens \insert 'SetOps.oz' declare fun {FreeVarIds Stmt} %% ENSURES: Result is the set of variable identifiers %% that occur free in Stmt case Stmt of skipStmt then {EmptySet} [] seqStmt(StmtList) then {UnionList {Map StmtList FreeVarIds}} [] localStmt(V Body) then {Minus {FreeVarIds Body} {AsSet [V]}} [] assignStmt(V Exp) then {Union {AsSet [V]} {FreeVarIdsExp Exp}} [] ifStmt(TestExp S1 S2) then {Union {FreeVarIdsExp TestExp} {Union {FreeVarIds S1} {FreeVarIds S2}}} [] caseStmt(Exp Pattern S1 S2) then {Union {FreeVarIdsExp Exp} {Union {Minus {FreeVarIds S1} {DeclaredIdsPattern Pattern}} {FreeVarIds S2}}} [] applyStmt(ProcExp ArgExpList) then {Union {FreeVarIdsExp ProcExp} {UnionList {Map ArgExpList FreeVarIdsExp}}} [] namedFunStmt(Name Formals Body) then {Union {AsSet [Name]} {Minus {FreeVarIdsExp Body} {UnionList {Map Formals DeclaredIdsPattern}}}} [] inStmt(Pattern Exp Body) then {Union {FreeVarIdsExp Exp} {Minus {FreeVarIds Body} {DeclaredIdsPattern Pattern}}} end end fun {FreeVarIdsExp Exp} %% ENSURES: Result is the set of variable identifiers %% that occur free in Exp case Exp of varIdExp(V) then {AsSet [V]} [] atomExp(_) then {EmptySet} [] boolExp(_) then {EmptySet} [] numExp(_) then {EmptySet} [] recordExp(LabelExp FieldList) then {Union {FreeVarIdsExp LabelExp} {UnionList {Map FieldList FreeVarIdsField}}} [] procExp(Formals Body) then {Minus {FreeVarIds Body} {UnionList {Map Formals DeclaredIdsPattern}}} [] ifExp(TestExp E1 E2) then {Union {FreeVarIdsExp TestExp} {Union {FreeVarIdsExp E1} {FreeVarIdsExp E2}}} [] caseExp(Exp Pattern E1 E2) then {Union {FreeVarIdsExp Exp} {Union {Minus {FreeVarIdsExp E1} {DeclaredIdsPattern Pattern}} {FreeVarIdsExp E2}}} [] applyExp(FunExp ArgExpList) then {Union {FreeVarIdsExp FunExp} {UnionList {Map ArgExpList FreeVarIdsExp}}} end end fun {DeclaredIdsPattern Pat} %% ENSURES: Result is the set of variable identifiers %% that occur free in Pat case Pat of varIdPat(V) then {AsSet [V]} [] atomPat(_) then {EmptySet} [] recordPat(_ FieldList) then {UnionList {Map FieldList FreeVarIdsField}} end end fun {FreeVarIdsField Field} %% ENSURES: Result is the set of variable identifiers %% that occur free in Field case Field of colonFld(_ Exp) then {FreeVarIdsExp Exp} [] posFld(Exp) then {FreeVarIdsExp Exp} end end