A Formal Method for Program Slicing * Yingzhou Zhang 1,2 , Baowen Xu 1,2 , Jose Emilio Labra Gayo 3 ( 1 Dep. of Computer Science and Engineering, Southeast Univ., Nanjing 210096, China) ( 2 Jiangsu Institute of Software Quality, Nanjing 210096, China) ( 3 Dep.of Computer Science, Univ. of Oviedo, C/Calvo Sotelo s/n C.P. 33007, Oviedo, Spain) mathzyz@163.com , bwxu@seu.edu.cn , labra@lsi.uniovi.es Abstract Program slicing is a well-known program analysis technique that extracts the elements of a program related to a particular computation. Based on modular monadic semantics of a programming language, this paper presents a new formal method for slicing, called modular monadic slicing, by abstracting the computation of slicing as a slice monad transformer. With the use of slice transformer, the feature of program slicing can be combined in a modular way into semantic descriptions of the program analyzed. According to these, this paper gives both monadic dynamic and static slicing algorithms. They compute program slices directly on abstract syntax, without the needs to explicitly construct intermediate structures such as dependence graphs, or to record an execution history in dynamic slicing algorithm. 1. Introduction Program slicing is a well-known program analysis technique that extracts the elements of a program related to a particular computation. A program slice consists of those statements of a program that may directly or indirectly affect the variables computed at a given program point, referred to as a slicing criterion. Program slicing has applications in program comprehension, testing and debugging, re-engineering, and software maintenance [1-4]. In reverse engineering, program slicing provides a toolset for abstracting out of the source codes the design decisions and rationale from the initial development and understanding the algorithms chosen. * This work was supported in part by the National Natural Science Foundation of China (60373066, 90412003), Young Scientist's Foundation of NSFC (60303024), National Research Foundation for the Doctoral Program of Higher Education of China (20020286004). In software maintenance, program slicing can help maintainers to determine whether a change at some place in a program will affect the behavior of other parts of the program. Program slicing can be used in software quality assurance to locate all code that contributes to the value of variables that might be part of a safety critical component. The original program slicing method was expressed as a sequence of data flow analysis problems [5]. An alternative approach relied on program dependence graphs (PDG) [6]. Most of the existing slicing methods were evolved from these two approaches. As the behavior of a program is determined by the semantics of the language, it is reasonable to expect an approach for program slicing based on formal semantics of a program. The program slicing methods focused on the semantics of programs are mainly based on the standard denotational semantics, i.e. denotational slicing [7-9]. Denotational semantics, however, lack modularity and reusability [10-14]. A practicable solution was to use monads [15] to structure denotational semantics, with the help of monad transformers [10, 16, 17] which can transform a given monad into a new one with new operations. S.Liang et al. used monads and monad transformers to specify the semantics of programming language and called it modular monadic semantics [18]. Based on this, this paper proposes a first approach for program slicing based on modular monadic semantics, called modular monadic slicing. It can compute slices directly on abstract syntax, without explicit construction of intermediate structures such as dependence graphs in the corresponding slicers. The rest of the paper is organized as follows: In Section 2, we briefly introduce the fundamental concepts of modular monadic semantics through a simple example language. The monadic program slicing algorithms and their complexity are discussed in Section 3. In Section 4, we illustrate our monadic slicing algorithm by analyzing in detail a sample of the example