Course 02263 Formal Aspects of Software Engineering RSL Value Definitions & Functions Anne E. Haxthausen DTU Informatics (IMM) Technical University of Denmark aeha@dtu.dk c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 1/24 Contents Part 1: Value Definitions 3 Part 2: Functions 16 c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 2/24 Contents of Part 1: Value Definitions different forms: axiomatic: using signatures (i.e. typings) and axioms explicit definition of constants implicit definition of constants explicit definition of functions implicit definition of functions under specification four last forms can be transformed to first form axiomatic versus operational interpretation c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 3/24 Definition of constants Examples explicit = 1 implicit x > 0 axiomatic axiom x > 0 c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 4/24
Definition of functions Examples explicit f(x) x + 1 implicit f(x) as r post r > x axiomatic axiom x : Int f(x) > x c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 5/24 Underspecification axiom x > 0 x is underspecified, may be refined to: = 1 or or... = 2 c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 6/24 Expansion of Explicit Value Definition = 1 axiom x 1 binding : type_expr = _expr binding : type_expr axiom binding _expr c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 7/24 Expansion of Implicit Value Definitions x > 0 axiom x > 0 binding : type_expr _expr binding : type_expr axiom _expr c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 8/24
Expansion of Explicit Function Definitions f(x) x + 1 axiom x : Int f(x) x + 1 id : type_expr1 type_expr2 id(x) _expr id : type_expr1 type_expr2 axiom x : type_expr1 id(x) _expr c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 9/24 Expansion of Implicit Function Definitions f(x) as r post r > x axiom x : Int f(x) as r post r > x id : type_expr1 type_expr2 id(x) as y post _expr id : type_expr1 type_expr2 axiom x : type_expr1 id(x) as y post _expr c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 10/24 Example of Expansion scheme SET_DATABASE = class type Database = Person-set, Person = Text end empty : Database = {}, register : Person Database Database register(p,db) db {p}, check : Person Database Bool check(p,db) p db scheme SET_DATABASE = class type Database = Person-set, Person = Text empty : Database, register : Person Database Database, check : Person Database Bool axiom empty {}, p : Person, db : Database register(p, db) db {p}, p : Person, db : Database check(p, db) p db end c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 11/24 Axiomatic versus Operational Interpretation Two different ways to of interpreting function definitions in languages: operational (as in programming languages) axiomatic (as in some specification languages, e.g. RSL) c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 12/24
Operational versus Axiomatic Interpretation (I) f: Int Int f(i) i+1 Operational interpretation: f(5) is computed by 1. substituting 5 for i in the body: i+1 2. evaluating the resulting expression: 5+1 to get 6 Axiomatic interpretation: The function is described by two properties: 1. its type: it must map integer arguments to integer results 2. an equivalence: f(i) must be semantically i+1 for all integers i c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 13/24 RSL has axiomatic interpretation f: Int Int f(i) i+1 is just a short form of: f: Int Int axiom i:int f(i) i+1 c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 14/24 Operational versus Axiomatic Interpretation (II) f: Int Int f(i) f(i) Axiomatic interpretation: all partial functions from Int to Int. Operational interpretation: only the infinite loop ( corresponding to chaos) c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 15/24 Contents of Part 2: functions functions are s 17 function type expressions 18 function expressions 20 function application expressions 22 operators 23 higher order functions 24 c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 16/24
Functions are s Functions are first class s! Hence, RSL allows: higher-order functions that take functions as argument and/or return functions. quantification over function types c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 17/24 Function Type Expressions type_expr1 type_expr2 denotes the type consisting of all total functions from type_expr1 to type_expr2. type_expr1 type_expr 2 denotes the type consisting of all partial functions from type_expr1 to type_expr2. ftot : T1 T2, fpar : T1 T 2, x : T1 defined deterministic (not chaos) ftot(x) yes yes fpar(x) might be might be ftot(x) is deterministic means:! y : T2 ftot(x) y c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 18/24 Function Type Expressions, Examples Bool Bool denote the type consisting of the following total functions: λ b : Bool true λ b : Bool false λ b : Bool b λ b : Bool b Bool Bool denote the type consisting of the following partial functions: λ b : Bool true λ b : Bool false λ b : Bool b λ b : Bool b λ b : Bool chaos λ b : Bool true false... c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 19/24 Function Value Expressions names of user-defined function, e.g.: distance lambda abstractions (anonymous functions), e.g.: λ b:bool b c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 20/24
Lambda Abstraction Basic form: λ binding : type_expr _expr Examples: λ b : Bool b λ (x,y) : Int Int x + y λ (b,(x,y)) : Bool (Nat Nat) if b then x else y end Semantics: represents function of type: type_expr T, where T = type_of(_expr) Derived form: λ (typing1,...,typingn) _expr, n 0 Examples: λ (x : Int, y : Int) x + y, λ (b : Bool, x,y : Nat) if b then x else y end c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 21/24 Function Application Expressions Examples: distance((1.0,2.3), (5.13, 5.13)) (λ(x,y) : Nat Nat x = y)(2,7) Typical form: function-expr(expr1,..., exprn), n 0 Context conditions: (expr1,..., exprn) must be of the argument type of expr c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 22/24 Associated Built-in Operators =, =, : (T 2 T 3) (T1 T 2) (T1 T 3) f g λ x : T1 f(g(x)) c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 23/24 Higher Order Functions: Example 3 ways of defining twice: twice : (Int Int) Int Int twice(f) f f twice : (Int Int) Int Int twice(f) λ i : Int f(f(i)) twice : (Int Int) Int Int twice(f)(i) f(f(i)) Applications of twice: twice(λ i : Int i + 1) λ i : Int i + 2 twice(λ i : Int i + 1)(1) 3 c Anne E. Haxthausen, Spring 2014 02263 OH4 p. 24/24