Imperative Languages - PowerPoint PPT Presentation

About This Presentation
Title:

Imperative Languages

Description:

Safe method if f is strict. Call by name simplification. f executed with non evaluated e; ... store s in ``read only'' mode. No second store required for ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 20
Provided by: scienc
Category:

less

Transcript and Presenter's Notes

Title: Imperative Languages


1
Imperative Languages
  • Section 5 Continued ...

2
A Dynamically Typed Language
  • Variable may take on values from different data
    types.
  • Runtime type checking required.
  • Input and output included.
  • Introduction of type tags''
  • Storablevalue Tr Nat
  • Store Id ? Storablevalue
  • Errvalue Unit
  • Expressiblevalue
  • Storablevalue Errvalue

3
Valuation Functions
  • State Store x Input x Output
  • store, input and output buffers
  • PostState OK Err
  • OK State, Err State
  • successful evaluation or type error
  • P Program ? Store ? Input ? Poststate?
  • Program takes input store and input buffer and
    returns a new state

4
  • C Command ? State ? Poststate?
  • Command takes state and returns a new state.
  • E Expression ? Store ? Expressiblevalue
  • Expression takes store and returns a value.

5
Composition of States
  • CC1 C2 CC1 checkcmd CC2
  • Command State ? Poststate ?
  • checkcmd Command x Command ? Command
  • h 1 checkcmd h 2
  • ?a. let z h1(a) in
  • cases z of
  • isOK(s, i, o) ? h 2 (s i o)
  • isErr(s, i, o) ? z
  • end

6
  • 1.Give current state a to CC 1 producing a
    poststate z CC 2 a.
  • 2.If z is a proper state a and if the state
  • component is OK, produce CC 2 a
  • 3.If z is errnoneous, CC 2 is ignored and
  • z is the result.
  • Similar for check of expression results.

7
Error Handling
  • Algebra operations abort normal evaluation when
    type error occurs.
  • Representation of lowlevel (e.g. hardwarelevel)
    fault detection and branching mechanisms.
  • Machine action on fault, branch out of the
    program.
  • Semantics on fault, branch out of function
    expression
  • Propagation of type errors yields same result as
    machine action.

8
  • Altering the Properties of Stores
  • 1. Store critical to evaluation of a phrase.
  • 2. Only one copy of store exists during
  • execution.
  • 3. Store serves as means of communication
  • between phrases.
  • Typical features of a store in sequential
  • programming languages.
  • What happens if we relax each of these
  • restrictions?

9
Delayed Evaluation
  • How to rewrite a function application f(e)?
  • Callbyvalue simplification
  • e evaluated before f is executed
  • Safe method if f is strict.
  • Callbyname simplification
  • f executed with nonevaluated e
  • Safe method also if f is nonstrict.
  • f Nat ? ? Nat ?
  • f ? x zero
  • f(?) zero
  • Ee ?
  • f(Ee) ) ? ?
  • Simplification of argument may require in finite
    number of steps!

10
Nonstrict Store Updates
  • May store operate on improper values?
  • Store Id ? Nat?
  • Improper values may be stored.
  • update Id ? Nat? ? Store ? Store
  • -- update ? i ? n ? si ? ns
  • -- updateI(EEs) s) is defined even in
  • theloop forever situation'' EEs ?.
  • -- Unevaluated expressions may be stored in s.
  • EEs needs not be evaluated until used.
  • -- Delayed (lazy) evaluation.
  • -- Value must be determined with respect to the
    store that was active when E was saved.

11
  • EXAMPLE
  • begin
  • X 0
  • Y X1
  • X 4
  • return Y
  • K Block ? Store ? ? Nat ?
  • Kbegin C return E
  • ? s EE (CCs)

12
  • Kbegin X0 Y X1 X4 return Ys0
  • EY (CX0 Y X1 X4s0 )
  • EY (CY X1 X4 (CX0s0 ))
  • EY (CY X1 X4
  • (update X (E0s0 ) s0 ))
  • EY (CY X1 X4 s1 )
  • s 1 (E0s0 ) needs not be simplified!

13
  • EXAMPLE
  • s2 update Y (EX1s1 ) s1
  • s3 update X (E4s 2 ) s2
  • EY (CY X1 X4 s1 )
  • EYs3
  • access Y s3
  • EX1s1
  • EXs1 plus one
  • (access X s1 ) plus one
  • E0s0 plus one
  • zero plus one
  • one

14
NonStrict Command Execution
  • Carry delayed evaluation up to level of commands.
  • Make C, E, and K nonstrict in their store
    arguments.
  • Only those commands need to be evaluated that
    have an effect on the output of a program!

15
  • EXAMPLE
  • begin X0 diverge X2 return X1
  • Kbegin X0 diverge X2 return X1s0
  • EX1 (CX0 diverge X2s0 )
  • EX1 (CX2(Cdiverge X2s0 ))
  • EX1(CX2s1 )
  • EX1(update X (E2s1 ) s1 )
  • EX1( X ? (E2s1 ) s1 )
  • EX( X ? (E2s1 ) s1 ) plus one
  • E2s 1 plus one
  • two plus one
  • three
  • s 1 CX0 diverges 0 needs not be
  • simplified!

16
Retaining Multiple Stores
  • EE 1 E 2 ? s.EE 1 s plus EE 2 s
  • E uses store s in read only'' mode.
  • No second store required for implementation.
  • Ebegin C return E
  • ? s let s CCs in EEs
  • Side effects in expression evaluation possible.
  • Multiple stores required for implementation.

17
  • EE 1 E 2
  • ? s let (v s ) EE 1 s
  • (v s ) EE 2 s
  • in (v plus v s )
  • Ebegin C return E
  • ? s let s CCs in (EEs, s)
  • Local updates remain in global store.
  • Expression evaluation returns new store.

18
NonCommunicating Commands
  • Store facilitates the building up of side
  • effects that lead to some final value.
  • Command advances computation by reading and
    modifying the values left in the store by
    previous commands.
  • Otherwise, communication breaks down and language
    loses sequential flavor.

19
  • combine D x D ? D
  • Domain s2 Store Id ? D
  • C Command ? Store ? ? Store ?
  • CC 1C 2 ?s. join (CC1s) (CC2 s)
  • join Store ? ? Store ? ? Store ?
  • join ?s1 ?s2(?is1 (i) combine s2 (i))
  • Parallel but noninterfering parallelism.
Write a Comment
User Comments (0)
About PowerShow.com