(Programming Language), ok,, if then else,(), ()() 5.0 5.0.0, (Variable Declaration) var x : T x, T, x,,,, var x : T P = x, x' : T P P, () var x:t P,,, yz, var x : int x:=2. y := x+z = x, x' : int x' =2 y' =2+z z' =z = y' =2+z z' =z, var x : int y:=x = x, x' : int x' =x y' =x z' =z = z' =z z, x, y var x : int y := x x = y' =0 z' =z,,,, undefined,,
var x : T P = x : undefined x' : T, undefined P, et var x : T := e P = x : e x' : T P s,, var x, y, z : T P = x, x', y, y', z, z' : T P --------------------------------------------------------------------------------------------------------------------------------------------------------,, 5.0.1 w, x, yzwx, frame w, x, var frame w, x P = P y' =y z' =z Pwx, (import), Pyz, (, )frame (frame) frame x := e = frame x x' = e ------------------------------------------------------------------------------------------------------------------------------------------------------- s' = Ls, L s:= Ls, n, 0#L s := L = frame s var n : nat s' = L, s, L, n, -----------------------------------------------------------------------------------------------------------------------------------------------------------
5.1 5.1.0, (Array)A23 A(2) := 3 A, i, e, A i := e = A' i=e ( j j i A' j = A j ) x' =x y' =y..., Aie, A,, A(A2) := 3 = A' (A2)=3 ( j j A2 A' j=aj ) x' =x y' =y... x := e. P = (Pxe),, A2 :=3. i:=2. A i := 4. A i = A2 T, i=2, A2=A2T, A2:=3. i:=2. A i := 4. A i = A2 = A2:=3. i:=2. 4=A2 = A2:=3. 4=A2 = 4=3 = A2:=2. A(A2):=3. A2=2,, A2=3, A2:=2. A(A2):=3. A2=2 = A2:=2. A2=2 = 2 =2 = T :=, A i := e
= A' i = e ( j j i A' j=aj) x' =x y' =y... = A' = i e A x' =x y' = y... = A:= i e A, A := i e A A:=2 3 A. i:=2. A:=i 4 A. A i = A2 = A:=2 3 A. i:=2. (i 4 A) i = (i 4 A)2 = A:=2 3 A. (2 4 A)2 = (2 4 A)2 = A:=2 3 A. T = T A:=2 2 A. A:= A2 3 A. A2=2 = A:=2 2 A. (A2 3 A)2=2 = ((2 2 A)2 3 (2 2 A)2=2 = (2 3 2 2 A)2=2 = 3=2 =, A i := e A := i e A A ij := ea := ( i ; j ) e A; -------------------------------------------------------------------------------------------------------------------------------------------------------------- 5.1.1, (records)(structures), person person = "name" text "age" nat p p var p : person p := "name" "Josh" "age" 17 (component)(field) p"age" := 18 p := "age" 18 p --------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------- 5.2 5.2.0 While, while(while Loop) while b do P b, P, b,, while b T, P, whilewhile,, s' = s + L[n;..#L] t' =t+#l n W while b do P W if b then (P.W) else ok while n #L do (s:=s+ln. n:=n+1. t:=t+1) s' = s+ L[n;..#L] t' = t+#l n if n #L then (s:=s+ln. n:= n+1. t := t+1. s' =s+ L[n;..#L] t' =t+#l n) else ok, W if b then (P.W) else ok, while,, while while b do P = if b then (P. while b do P) else ok while 265 x y while x = y = 0 do if y > 0 then y : = y! 1 else ( x : = x! 1. var n : nat! y : = n) y 0 x 1 y y 0 x 1 y x y 0 t P! if x = y = 0 then ok else if y > 0 then ( y : = y! 1. t : = t + 1. P)
else ( x : = x # 1.( " n! y : = n). t : = t + 1. P) x y y n P n x y x y n x f x f f : nat! nat nat nat f s =! f [ 0;.. x] s f x t' = t + x + y + s! if x = y = 0 then ok else if y > 0 then ( y : = y! 1. t : = t + 1. t' = t + x + y + s) else ( x : = x! 1. y : = fx. t : = t + 1. t' = t + x + y + s) x = y = 0! ok " x = y = s = 0! t' = t! t ' = t + x + y + s y > 0 " ( y : = y! 1. t : = t + 1. t' = t + x + y + s) = y > 0 " t' = t + 1+ x + y! 1+ s! t ' = t + x + y + s x > 0 " y = 0 " ( x : = x! 1. y : = fx. t : = t + 1. t' = t + x + y + s) = x > 0 # y = 0 # t' = t + 1+ x " 1+ f ( x " 1) +! f [0;.. x " 1]! t ' = t + x + y + s x + y + x ------------------------------------------------------------------------------------------------------------------------------------------------------While 5.2.1 P loop P end exit when b b break exit 5.2.0,, L loop A. exit when b. C
end L A. if b then ok else (C. L),,, go to, go toexit n when bb n, P :: loop A. loop E B. exit 2 when c. D end. end Q P! AQ. Q! B. if c then ok else ( D. Q),, E, P! loop A. exit 1 when b. I C. loop D. exit 2 when e. F. exit 1 when g. H end. end Q P! A. if b then ok else ( C. Q)
Q! D. if e then ok else (F. if g then ( I. P) else ( H. Q)),,,, ----------------------------------------------------------------------------------------------------------------------------------------------- P!
!! j = m!!!!!!!!!! Fmn " m = n! ok
Fik # m " i < j < k " n! ( Fij. Fjk) Fmn! i := m;.. n m " i < n! Fi( i +1) Fmn m m n n m = n Fmn m j j n m! i < n j! i F = $ i, j : nat # x' = x " 2 Fmn # L' = # L # " i : 0,..# L! L' i = Li + 1 Fik i k Fik = # L' = # L # (" j : i,.. k! L' j = Lj + 1) # (" j : (0,.. i),( k,..# L)! L' j = Lj) F 0(# L) " 0 = # L! ok Fik # 0 " i < j < k "# L! ( Fij. Fjk) F0(# L)! i := 0;..# L Fi( i +1) Fi( i +1) Fi ( i + 1) " L : = i! Li + 1 L Fmn I m! I' n I I' I Ii Fii = ok Fik! ( Fij. Fjk)
n x'= 2! x : = 1. I0! I' n I 2 i = " i : nat! x = I 0! I' n! i := 0;.. n Ii! I' ( i + 1) Ii! I' ( i + 1)! x : = 2! x 5.2.4,, go togo to,, go to, go to,, go to,
go to (), (),,,,, go to -------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------- 5.3,,,,, deadline : = t + 5 if t! deadline then. else t := 5,,, w, wait until w = t := max t w, t := max t w, wait until w if t w then ok else ( t :=t+1. wait until w),, t w ok = t w ( t := t ) t := max t w t<w (t:=t+1. t:=max t w) = t+1 w ( t := max (t+1) w ) = t+1 w ( t := w ) = t< w ( t := max t w ) t := max t w, t : int,
wait until w 275(b) t s t s s --------------------------------------------------------------------------------------------------------------------------------------------- 5.4 5.4.0, assert b bb,,,,, assert b = if b then ok else (print "error". wait until ) b, assert b okb,, -------------------------------------------------------------------------------------------------------------------------------------------------------------- 5.4.1 PQ, P Q, PQ P " Q! P P " Q! Q, or, x:=0 or x:=1, x01 b, ensure b = if b then ok else = b ok ensure b assert b, ensure bbokb,, (b),,
, xy x :=0 or x:=1. ensure x=1 = x", y" (x"=0 y"=y x"=1 y"=y) x"=1 x' =x" y' =y" = x' =1 y' =y = x := 1 x:=0x:=1,, (), ensure,,,, (bookkeeping), Prolog, (),,, ------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------- 5.5 5.5.0 Result P, e,, P result e, Pe, var term, sum : rat :=1 for i:=1;..15 do (term := term/i. sum := sum+term) result sum result x x' = ( P result e) = P. x' =e termsum,, y:=y+1 result y y:=y+1, yy x := ( y := y+1 result y ) = x' = ( y :=y+1 result y) y' = y = ( y := y+1. x' =y ) y' =y = x' = y+1 y' =y
= x := y +1 (), result,, result result,, x+x=2 x, x = x, x x = ( y:=y+1 result y ),, result, x := ( P result e) ( P. x :=e ) P,, P x := e, x := y + ( P result e) ( var z := y P. x := z+e ) () result, result result result ---------------------------------------------------------------------------------------------------------------------------------------------------Result 5.5.1,,, result C, int bexp(int n) { int r=1; int i; for (i=0;i<n;i++) r=r*2; return r; } b exp = " n : int! var r : int := 1! for i := 0;.. n do r : = r! 2. assert r : int result r ),,,, result
, (90) ------------------------------------------------------------------------------------------------------------------------------------------------------------- 5.5.2 (Procedure) (),,,,,,,, xp P = λ x : int a' < x < b' ab,, P,, P3 = a' < 3 < b' P(a+1) = a' < a+1 < b' a' < x < b' a:=x 1. b:=x+1 PP ( λ p : D B ) a = (var p : D := a B),,,,,,,,, var,, "! (! " x : int x :=3 b := a ) a = a :=3. b:= a = a' = b' = 3,,,, (! " p : D B ) a = (λ p, p' : D. B ) a a',,, x,
x:=3. b:=a = b:=a. x:=3, -------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------- 5.6, r, i 2 p address of A[1] 4 A[0] 1 *p, A[1] 3 A[i], A[2] 2 A[3] 3, p, A[1], *pa[1] i 2, A[i]A[2] r, i, ri, (Alias),,,,,,,,, i 0 p 1 *p 2 r 3 A[0] 4 A[1] 5 A[2] : A[3] : A[i] address of A[1]
p := address of A[3]i := 4, i 2 p 3 A 4 0 1 : [1;3;2;3] :,, (), ------------------------------------------------------------------------------------------------------------------------------------------------------------- prob = r : real " 0! r! 1 T n : nat + 1 2! n % n % n (# n : nat + 1! 2 : prob) " ( $ n : nat + 1! 2 ) = 1 2! n n 3!n!m n, m : nat + 1 2 % n% m % n% m (# n, m : nat + 1! 2 : prob) " ( $ n, m : nat + 1! 2 ) = 1!n!m 2 n m!n!m n, m : nat + 1 2! " n" m m : nat + 1# 2 2! n
n!n!m 2 n!m 2! " 2 n m v p e! v # e " p n 2! n nat + 1 n 2 2! n!n!m $ n : nat + 1# n " 2 n m 2 nat + 1 n! m! " n" m n, m : nat + 1$ ( n " m) # 2!! '! $ # S " P x y x x ( x = 7)! 1/ 3 + ( x = 8)! 2 / 3 x ( 7 = 7)! 1/ 3 + (7 = 8)! 2 / 3 T! 1 / 3+ "! 2 / 3 1! 1/ 3 + 0! 2 / 3 x x x y x Y = ( y = 7)! 1/ 3 + ( y = 8)! 2 / 3 X! Y S! x, y # S " X " Y! x, y & ( x = y " x' = y' = 0 % x $ y " x' $ abs( x # y) " y' = 1)! (( x = 7) / 3 + ( x = 8)! 2 / 3)! (( y = 7) / 3 + ( y = 8)! 2 / 3) ( x' = y' = 0)! 5 / 9 + ( x' = y' = 1)! 4 / 9 x' = y' = 0 x' = y' = 1 n ok n n'! n, n' " n' = n! n ' = n T
! n '" n' = n n ok n' n' n n' = n + 1 n n' n + 1 n ok! ( n : = n + 1) n n = 5 n ok n'= 5 n : = n + 1 n'= 6 n'= 5 n'= 6! $ # S " p S ok! ( n : = n + 1) n! n' # ok " ( n : = n + 1) P # Q = " b : bool! b P Q b! P + ( 1" b)! Q P. Q =!# ''" P! '! ''! Q!! '' x x = 0! x'' " (( x'' = 0) / 3 + ( x'' = 1)! 2 / 3! (( x'' = 0)! (( x' = x'' + 2) / 2 + ( x' = x'' + 3) / 2) + ( x'' " 0)! (( x' = x'' + 4) / 4 + ( x' = x'' + 5)! 3/ 4))
( x' = 2) / 6 + ( x' = 3) / 6 + ( x' = 5) / 6 + ( x' = 6) / 2! $ # S " p p S ( p'. S) p' p n P, Q R n! P. Q = n! ( P. Q) = P. n! Q P + Q. R = ( P. R) + ( Q. R) P. Q + R = ( P. Q) + ( P. R) n : nat + 1 rand n 0,.. n rand n ( r : 0,.. n) / n r x = x T x + x = 2! x! ( r : 0,.. n) / n r : 0,.. n x r s r : 0,..2 # s : 0,..3# ( x : = r. x : = x + s) " 1/ 2 1/ 3!! "! r 0,..2! s : 0,..3" ( x' = r + : " s) / 6 (( x' = 0 + 0) + ( x' = 0 + 1) + ( x' = 0 + 2) + ( x' = 1+ 0) + ( x' = 1+ 1) + ( x' = 1+ 2)) / 6 ( x' = 0) / 6 + ( x' = 1) / 3 + ( x' = 2) / 3 + ( x' = 3) / 6 x' ( r : 0,.. n) / n x ( x': 0,..2) / 2.( x': x + (0,..3)) / 3! x' '#( x'': 0,..2) / 2" ( x': x'' + (0,..3)) / 3 1/ 2! ( x ': 0,..3) / 3 + 1/ 2! ( x':1,..4) / 3
( x' = 0) / 6 + ( x' = 1) / 3 + ( x' = 2) / 3 + ( x' = 3) / 6 rand 2 + rand3 ( n = 0! n = 3) / 6 + ( n = 1! n = 2) / 3 n rand8 < 3 b! r : 0,..8" ( b = ( r < 3)) / 8 ( b = T )! 3/ 8 + ( b =" )! 5 / 8! b / 4 b T! x ( x': (0,..13) + 1) /13. x < 7 ( x': x + (0,..13) + 1) / 13 x ' = x! x' ' $ ( x' ': 1,..14) / 13" (( x' ' < 7) " ( x': x' ' + 1,.. x' ' + 14) / 13 + ( x' ' # 7) " ( x' = x' ' )) (( 2 # x ' < 7) " ( x'! 1) + (7 # x' < 14) " 19 + (14 # x' < 20) " (20! x')) / 169 x' x' c d x n y n + 1 x c : = ( rand13) + 1. d : = ( rand13) + 1. c < n x : = c + d x : = c. c < n + 1 y : = c + d y : = c. y < x! 14 " x! 14 < y ( c ': (0,..13) + 1! d': (0,..13) + 1! x' = x! y' = y) /13/13. x := c < n c + d c. y : = c < n + 1 c + d c. y < x! 14 " x! 14 < y ( c ': (0,..13) + 1! d': (0,..13) + 1! x' = x! y' = y) /169. c < n + 1 c + d c c < n c + d c! 14! c < n c + d c! 14 c < n + 1 c + d c ( c ': (0,..13) + 1! d': (0,..13) + 1! x' = x! y' = y) /169. c = n " d > 14! n! c' ', d'', x'', y'' "
( c'': (0,..13) + 1" d'': (0,..13) + 1" x'' = x " y'' = y) /169# ( c'' = n " d'' > 14! n)! d' ':1,..14 # ( d'' > 14 " n) / 169 ( n!1) / 169 x ( n!1) / 169 y ( 14! n) / 169 n < 8 n + 1 n n! 8 n n +1 R R! u : = ( rand6) + 1. v : = ( rand6) + 1. u = v ok ( t : = t + 1. R) t t' #t ( t' " t)! (5 / 6)! 1/ 6 u : = ( rand6) + 1. v : = ( rand6) + 1. rand t' #t u = v t ' = t ( t : = t + 1.( t' " t)! (5 / 6)! 1/ 6) (( u ':1,..7)! v' = v! t' = t) / 6.( u' = u! ( v':1,..7)! t' = t) / 6. t'! t!1 u = v t ' = t ( t' # t + 1) " (5/ 6) / 6 (( u ':1,..7)! ( v':1,..7)! t' = t) / 36. t'! t!1 u = v t ' = t ( t' # t + 1) " (5/ 6) / 6! u' ', v'':1,..7! t'' #( t'' = t) / 36 " (( u'' = v'') " ( t' = # t'' ) t'! t''! 1 + ( u'' $ v'') " ( t' # t'' + 1) " (5/ 6) / 6) t'! t!1 1/ 36 " (6 " ( t' = t) + 30 " ( t' # t + 1) " (5/ 6) / 6) t' #t ( t' " t)! (5/ 6)! 1/ 6 t' t' % t t' # t'!( t' " t)! (5 / 6)! 1/ 6 = t + 5 $ ( x' = 0) /1+ ( x' = 1)! 2 / 3
x' p!! 5.8 (imperative), (),, ()
,, (142), λ L : [* rat] L, n n 0 L = ( λ n : 0,..#L+1 L[n;..#L] ) 0 λ L : [*rat] L =... L =..., L, n,, n 0,..#L+1 = (0,..#L), #L λ n : 0,..#L+1 L [n;..#l] = ( λ n : 0,..#L L [n;..#l] ) ( λ n : #L L [n;..#l] ), n < #L n = #L λ n : 0,..#L L[n;..#L] = λ n : 0,..#L L n + L[n+1;..#L] λ n : #L L[n;..#L] = λ n : #L 0 n, L[n+1;..#L] = ( λ n : 0,..#L+1 L[n;..#L] ) (n+1) if then else, ( λ v : A x ) ( λ v : B y ) = λ v : A, B if v : A then x else y,,, 1, 0, λ L : [*rat] #L,, n n, #L = ( λ n : 0,..#L+1 #L n) 0 λ n : 0,..#L+1 #L n = ( λ n : 0,..#L #L n) ( λ n : #L #L n ), 1, λ n : 0,..#L #L n = λ n : 0,..#L 1 + #L n 1, 0 λ n : #L #L n = λ n : #L 0 #L n 1 = ( λ n : 0,..#L+1 #L n) (n+1) (),, 1
, λ L : [*rat] #L #L = ( λ n : 0,..#L+1 #L n ) 0 λ n : 0,..#L+1 #L n = ( λ n : 0,..#L #L n) ( λ n : #L #L n ) λ n : 0,..#L #L n = λ n : 0,..#L #L n λ n : #L #L n = λ n : #L 0 #L n = 1+ ( λ n : 0,..#L+1 #L n ) (n+1) 5.8.0, x' : 2, 3, 4,, 2, 3, 4, Sx, S x = 0 Sx, S x > 0 Sx, S x! 1 Sx, S x >1 Sx, y y : S x S, x y y : S x ( xs, ys ) x S x null λ L : [*int] λ x : int n : 0,..#L L n = x Lx, LxxL,, xl,, xll, λ L : [*int] λ x : int if x : L(0,..#L) then n : 0,..#L L n = x else #L,.., P()S(), SP, S : P,, S : P,, ::, PS P :: S, 0,, xl (if x : L(0,..#L) then n : 0,..#L Ln = x else #L,.. ) ::
( λ i : nat if x : L (i,..#l) then n : i,..#l L n = x else #L,.. ) 0, = :: i if then else ( if x : L (i,..#l) then n : i,..#l. L n = x else #L,.. ) :: if i=#l then #L else if x = L i then i else (λ i if x : L (i,..#l) then n : i,..#l L n = x else #L,.. ) (i+1), λ L λ x 0,..#L +1, #L+1 0,..#L i+1 :: if i = #L then 0 0,..#L+1 :: ( λ i 0,..#L i +1 ) 0 else if x = L i then 0 else 1+ (λ i 0,..#L i +1) (i+1) --------------------------------------------------------------------------------------------------------------------------------------------------------,,, (), (" v : D! b) x = (bvx) x:=e.p = (Pxe) ----------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------