1 / 29

let/if-then-else

let/if-then-else. let In the let-expression ( let x1 == E1 ; … ; xn == En • E), the variables x1 … xn are local; their scope includes the expression E ( let r == iroot(a) • r*r < a < (r+1)*(r+1 ))

loring
Télécharger la présentation

let/if-then-else

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. let/if-then-else let In the let-expression (let x1 == E1; … ; xn == En •E), the variables x1 … xnare local; their scope includes the expression E (let r == iroot(a) • r*r < a < (r+1)*(r+1)) if then else - conditional expression: if P then E1 else E2 – E1 and E2 type should be the same | x | = if x > 0 then x else -x

  2. Z Schemas • Schema: math in a box, with a name attached • Schema calculus builds large schemas from smaller ones. • Model states and operations. • Can be used as declarations, predicates, expressions…

  3. Z Schema • The Z schema is a 2-dimensional graphical notation for describing: – state spaces – operations • Definition: A vertical-form schema is either of the form

  4. Z Schema Or • In the latter case, the predicate part is assumed to be ‘true’.

  5. Z Schema • The declarations part of the schema will contain: – a list of variable declarations; and – references to other schemas (this is called schema inclusion) • Variable declarations have the usual form: x1; x2; : : : ; xn : T; • The predicate part of a schema contains a list of predicates, separated either by semi-colons or new lines.

  6. Schema

  7. State and Operation Schema States A state is a variable assignment A schema describes a set of states Operations To describe an operation, a schema must describe pairs of states (pre/post)

  8. State Schema • The value of tel will be a partial function from NAME to PHONE, i.e., it will associate names with phone numbers

  9. Operation Schemas • In specifying a system operation, we must consider: • the objects that are known to remain unchanged by the operation • the objects that may be altered by the operation • pre-conditions • post-conditions

  10. Example: Phonebook Lookup Analyze the problem: • Accesses the PhoneBookschema • It does not change it • Takes a single ‘input’ —a name • Produces a single output —a phone number • Pre-condition: the name is known to the database.

  11. Example: Phonebook Lookup

  12. Schema calculus: Conjunction

  13. Example - Conjunction  Quotient n, d, q, r: N d  0 n = q * d + r Remainder r, d: N r < d Division = Quotient  Remainder Division n, d, q, r: N d  0 n = q * d + r r < d

  14. Schema calculus : Disjunction

  15. Example - Disjunction  Division n, d, q, r: N d  0 n = q * d + r r < d DivideByZero d, q, r: N report!: TEXT d = 0  q = 0  r = 0 report! = Error T_Division = Division  DivideByZero T_Division n, d, q, r: N report!: TEXT (d  0  r < d  n = q * d + r )  (d = 0  q = 0  r = 0  report! = Error)

  16. Z Specification Yes Yes No Yes Yes Yes Yes NL: newline Ident: variable/type names, e.g, ch?

  17. Z Specification Process

  18. Modeling Large Systems Large systems are composed of components collected into subsystems. Complex components can be built up from simpler ones using schema inclusion.

  19. Case Study A: Using Z to define an abstract data type state space (a set of variables and their constrains), initial state, operations State space: Initial State: Operation:

  20. Case Study B: Birthday book Model a system which records people's birthdays, and is able to issue a reminder when the day comes round. Step 1 Define given sets and types [NAME] [DATE] Step 2 Define state space • knownis the set of names with birthdays recorded • birthdayis a function which, when applied to certain names, gives the birthdays associated with them

  21. Case Study B: Birthday book Step 3 initial state Step 4 operations Add, find, reminder

  22. Case Study B: Birthday book Add Birthday

  23. Case Study B: Birthday book Find Birthday

  24. Case Study B: Birthday book Remind

  25. Case Study C: Editor Example Natural language (English) description: • User can type in text • User can move cursor forward and backward through the text • User can delete the character in front of the cursor

  26. State schemas Editor left, right: TEXT # ( left  right ) maxsize left is before the cursor, right is after

  27. Initialization schemas InitEditor Editor left = right = <> conventional name begins with Init Editor is included left and right come from Editor

  28. Operation schemas printing: P Char Insert  Editor ch?: CHAR ch?  printing left’ = left  <ch?> right’ = right global declaration of printing characters “” means that the state will change “?” means input (“!” means output) precondition left’ is the value after the operation

  29. Schema Inclusion InitEditor Editor left = right = <> InitEditor left, right: TEXT # ( left  right )  maxsize left = right = <> Editor left, right: TEXT # ( left  right )  maxsize

More Related