Domain Theory I Domain Theory I Wolfgang Schreiner Research Institute for Symbolic Computation (RISC-Linz) Johannes Kepler University, A-4040 Linz, Austria [email protected] http://www.risc.uni-linz.ac.at/people/schreine Wolfgang Schreiner RISC-Linz Domain Theory I Semantics of Loops B: Boolean-expression C: Command ... C ::= . . . j while B do C j . . . ... C[[while B do C]] = s: B[[B]]s ! C[[while B do C]](C[[C]]s) [] s Problem: meaning of a syntax phrase may be dened only in terms of its proper subparts. C[[while B do C]] = w ! Store s:B[[B]]s ! w(C[[C]]s) [] s where w: w= Store ? ? Recursion in syntax exchanged for recursion in function notation! Wolfgang Schreiner 1 Domain Theory I Recursive Function Denitions Function Denition q = n:n equals zero Possible ! one [] q (n plus one) Function Graphs: f(zero, one)g f(zero, one), (one, ?), (two, ?), . . . g { f(zero, one), (one, six), (two, six), . . . g { f(zero, one), (one, k ), (two, k ), . . . g { = Several functions satisfy specication; which one shall we choose? Wolfgang Schreiner 2 Domain Theory I Least Fixed Point Semantics Theory that establishes meaning of recursive specications: 1. Guarantees that every specication has a function satisfying it. 2. Provides means for choosing the \best" function out of the set of possibilities. 3. Ensures that the selected function corresponds to the conventional operational treatment of recursion. Argument is mapped to dened answer i simplication of the specication yields a result in a nite number of recursive invocations. Wolfgang Schreiner 3 Domain Theory I The Factorial Function fac(n) = n equals zero ! one [] n times (fac(n minus one)) Only one function satises specication: graph(factorial) = f(zero, one), (one, one), (two, two), (three, six), . . . , (i, i!), . . . g Wolfgang Schreiner 4 Domain Theory I Simplication fac(three) ! three equals zero ! one [] three times fac(three minus one) = three times fac(three minus one) = three times fac(two) ! three times (two equals zero ! one [] two times fac(two minus one)) = three times (two times fac(one)) ! three times (two times (one equals zero ! one [] one times fac(one minus one))) = three times (two times one (times fac(zero))) ! three times (two times (one times (zero equals zero ! one [] zero times fac(zero minus one)))) = three times (two times one (times one)) = six Wolfgang Schreiner 5 Domain Theory I Partial Functions Answer is produced in a nite number of unfolding steps. Idea: place limit on number of unfoldings and investigate resulting graphs zero: fg { one: f(zero, one)g { two: f(zero, one), (one, one)g { i : f(zero, one), (one, one), . . . (i, i )g { + 1 Graph ! at stage i denes function fac i. Consistency with each other: graph fac graph fac +1 { Consistency with ultimate solution: graph fac graph factorial { Consequently S =0 graph fac graph factorial { ( i) ( ( i) ( 1 i Wolfgang Schreiner ( i) ) i ) ( ) 6 Domain Theory I Partial Functions Any result is computed in a nite number of unfoldings. a; b) 2 graph(factorial) ! (a; b) 2 graph(faci) (for some i) ( Consequently S =0 graph fac graph factorial ) graph factorial ) = Thus ( ( 1 i S ( i) =0 graph(faci) 1 i Factorial function can be totally understood in terms of the nite subfunctions faci! Wolfgang Schreiner 7 Domain Theory I Partial Functions Representations of sub-functions fac0 = n: ? fac +1 = n: n equals zero ! one [] n times fac (n minus one) i Each i denition is non-recursive Recursive specication can be understood in terms of a family of non-recursive ones. Common format can be extracted \Functional" F F: (Nat ! Nat ) ! (Nat ! Nat ) F = f: n: n equals zero ! one [] n times f (n minus one) ? ? Each subfunction is an instance of the functional! Wolfgang Schreiner 8 Domain Theory I Functional and Fixed Point Partial functions fac +1 = F(fac ) = F (?) ? i n:?) := ( Function graph graph factorial Fixed ( i i ) = S =0 graph(F 1 i point property i ? ( )) graph(F(factorial)) = graph(factorial) F(factorial) = factorial The function factorial is a xed point of the functional F! Wolfgang Schreiner 9 Domain Theory I q Function Q = q:n:n equals zero ! one [] q (n plus one) Q0(?) = n:? graph(Q0(?)) = fg ( ) Q1(?) = n:n equals zero ! one n:?)(n plus one) [] ( = n:n equals zero ! one ? graph(Q1(?)) = f(zero, one)g [] Q2(?) = Q(Q1(?)) = n:n equals zero ! one n plus one equals zero ! one graph(Q2(?)) = f(zero, one)g [] (( Wolfgang Schreiner ) [] ? ) 10 Domain Theory I q Function Convergence has occured graph(Q (?)) = f(zero, one)g, i i Resulting S graph 1 i =0 graph(Q (?)) = f(zero; one)g 1 Fix i point property Q(qlimit) Still = qlimit many solutions possible graph(q ) = f (zero, one), (one, k), . . . , (i, k), . . . g k Least xed point property graph(qlimit) graph(q ) k The function qlimit is the least xed point of the functional Q! Wolfgang Schreiner 11 Domain Theory I Recursive Specications The meaning of a recursive specication f F f is taken to be x(F ), the least xed point of the functional denoted by F . S graph x F =0 graph F ? = ( The cpo ) = 1 i ( i ( ( ) )) domain D of F must be a pointed partial ordering on D, { every chain in D has a least upper bound in D , { D has a least element. { F must be continuous Preserves limits of chains. Semantic domains are cpos and their oper- ations are continuous. Pointed cpos are created from primitive domains and union domains by lifting. Wolfgang Schreiner 12 Domain Theory I Factorial Function F = f. n. n equals zero ! one [] n times (f(n minus one)) Simplication rule x F = F(x F) (x F)(three) = (F (x F))(three) = ( f. n. n equals zero ! one [] n times (f(n minus one))(x F))(three) = ( n. n equals zero ! one [] n times (x F)(n minus one))(three) = three equals zero ! one [] three times (x F)(three minus one) = three times (x F)(two) = three times (F (x F))(two) = ... = three times (two times (x F)(two)) Fixed point property justies rec. unfolding! Wolfgang Schreiner 13 Domain Theory I Double Recursion g = n. n equals zero ! one [] (g(n minus one) plus g(n minus one)) minus one graph(F0(?)) = fg graph(F1(?)) = f(zero, one)g graph(F1(?)) = f(zero, one), (one, one)g graph(F2(?)) = f(zero, one), (one, one), (two, one)g ... graph(F +1(?)) = f(zero, one), . . . , (i, one)g i x F = n. one Stepwise construction of graph yields insight! Wolfgang Schreiner 14 Domain Theory I Simultaneous Denitions f, g: Nat ! Nat f = x. x equals zero ! g(zero) [] f(g(x minus one)) plus two g = y. y equals zero ! zero [] y times f(y minus one) T = Nat ! Nat F: (T T) ! (T T) F = (f,g).(. . . , . . . ) F0(?) = (fg, fg) F1(?) = (fg, f(zero, zero)g) F2(?) = (f(zero,zero)g, f(zero, zero)g) ... F5(?) = (f(zero,zero), (one, two), (two, two)g, f(zero, zero), (one, zero), (two, four), (three, six)g) F (?) = F5 (?), i > x(F) = (f,g) ? ? i Wolfgang Schreiner 5 15 Domain Theory I While Loops C[[while B do C]] = x(f:s. B[[B]]s ! f (C[[C]]s) [] s) Function: Store ? ! Store ? Example: C[[while A>0 do (A:=A-1; B:=B+1)]] = x F where F = f:s: test s ! f (adjust s) [] s test = B[[A > 0]] adjust = C[[A:=A-1; B:=B+1]] Partial function graphs: Each pair in graph shows store prior to loop entry and after loop exit. Each graph F i+1(?) contains those pairs whose input stores nish processing in at most i iterations. Wolfgang Schreiner 16 Domain Theory I Example graph(F0(?))=fg graph(F1(?))=f ( f ([[A]],zero), ([[B]],zero), . . . g, f ([[A]],zero), ([[B]],zero), . . . g ), ... ( f ([[A]],zero), ([[B]],four), . . . g, f ([[A]],zero), ([[B]],four), . . . g ), . . . g graph(F2(?))=f ( f ([[A]],zero), ([[B]],zero), . . . g, f ([[A]],zero), ([[B]],zero), . . . g ), ... ( f ([[A]],zero), ([[B]],four), . . . g, f ([[A]],zero), ([[B]],four), . . . g ), ... ( f ([[A]],one), ([[B]],zero), . . . g, f ([[A]],zero), ([[B]],one), . . . g ), ... ( f ([[A]],one), ([[B]],four), . . . g, f ([[A]],zero), ([[B]],ve), . . . g ), . . . g Wolfgang Schreiner 17 Domain Theory I While Loops Representation by nite subfunctions C[[while B do C]] = Ff s:?, s.B[[B]]s ! ? [] s, s.B[[B]]s ! (B[[B]](C[[C]]s) ! ? [] C[[C]]s) [] s, s.B[[B]]s ! (B[[B]](C[[C]]s) ! (B[[B]](C[[C]](C[[C]]s)) ! ? [] C[[C]](C[[C]]s)) [] C[[C]]s) [] s, . . . g = Ff C[[diverge]], C[[if B then diverge else skip]], C[[if B then (C; if B then diverge else skip) else skip]], C[[if B then (C; if B then (C; if B then diverge else skip) else skip) else skip]], . . . g Loop iteration can be understood by sequence of non-iterating programs. Wolfgang Schreiner 18 Domain Theory I Reasoning about Least Fixed Points Fixed Point Induction Principle: To prove P x F , it suces to prove 1. P ? 2. P d ! P F d , for arbitrary d 2 D ( ( ) ) ( ) ( ( )) for pointed cpo D, continuous functional F D inclusive predicate P D ! B. : Inclusiveness ! D, and : of predicates If predicate holds for every element of chain, it also holds for its least upper bound. All universally quantied combinations of conjunctions/disjunctions that use only v over functional expressions are inclusive. Mainly useful for showing equivalences of program constructs. Wolfgang Schreiner 19 Domain Theory I Reasoning about Least Fixed Points C[[repeat C until B]] = x(f:s: let s = C[[C]]s in B[[B]]s ! s [] (fs )) C[[C; while :B do C]] ? C[[repeat C until B]] 0 0 0 0 = Proof: P (f; g ) = 8s:f (C[[C]]s) = (gs) 1. P(?, ?) holds obviously. 2. Prove P (F (f ); G(g )) F = (f:s: B[[: B]]s ! f (C[[C]]s) [] s) G = (f:s: let s = C[[C]]s in B[[B]]s ! s [] (fs )) 0 0 0 0 (a) F (f )(C[[C]]?) = ? = G(g )(?). (b) s 6 ?: i. C[[C]]s ?: F (f )(?)=?=(let s = ? in B[[B]]s ! s [] (gs )) = G(g )(?). ii. C[[C]]s s0 6 ?: F (f )(s0 )=B[[: B]]s0 ! f (C[[C]]s0)[]s0 = B[[B]]s0 ! s0 [] f (C[[C]]s0) = B[[B]]s0 ! s0 [] f (gs0) = G(g)(s) = 0 = 0 0 = Wolfgang Schreiner 0 = 20
© Copyright 2026 Paperzz