1 / 16

Compound Data Structures

Compound Data Structures. Structural decomposition into other values. Lists: domain A* Constructors: NIL, CONS. Selectors: HEAD, TAIL. Tuples: domain A x B x C … Constructor: (…, …, …) Selector:  i Problem: imperative languages Variable forms of the objects exist.

chilley
Télécharger la présentation

Compound Data Structures

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. Compound Data Structures • Structural decomposition into other values. • Lists: domain A* • Constructors: NIL, CONS. • Selectors: HEAD, TAIL. • Tuples: domain A x B x C … • Constructor: (…, …, …) • Selector:  i • Problem: imperative languages • Variable forms of the objects exist. • Objects subcomponents can be altered. • Several versions of arrays variables.

  2. Arrays • Collection of homogeneous objects indexed by a set of scalar values. • Homogeneity  all components have same structure. • Allocation of storage and type­checking easy. • Both tasks can be performed by compiler. • Selector operation: indexing. • Scalar index set: primitive domain with relational and arithmetic operations. • Restricted by lower and upper bounds.

  3. Linear Vector of Values • IDArray = (Index  Location) x Lower­bound x Upper­Bound • Index is index set • lessthan, greaterthan, equals. • Lower­bound = Upper­bound = Index. • First component maps indices to the locations that contain the storable values. • Second and third component denote the bounds allowed on array indices.

  4. Multidimensional Arrays • Array may contain other arrays. • Three­dimensional array is vector whose components are two­dimensional vectors. • Hierarchy of arrays defined as infinite sum • 1DArray = (Index  Location) x Index x Index. • (n+1)DArray = (Index  nDArray) x Index x Index. • 1DArray maps indices to locations, 2DArray maps indices to 1D arrays, . . . .

  5. Multidimensional Arrays • a  MDArray = inkDArray(map, lower, upper) for some k >= 1 • access­array: Index  MDArray (Location + MDArray + Errvalue) • access­array = i. r. cases r of is1DArray(a)  index1 a i [] is2DArray(a)  index2 a i . . . [] iskDArray(a)  indexK a i . . . end

  6. index m = (map, lower, upper).  i. (i lessthan lower) or (i greaterthan upper)  inErrvalue() [] MInject(map(i)) 1Inject =  l.inLocation(l) . . . (n+1)Inject =  a. inMDArray( innDArray(a))

  7. Multidimensional Arrays • access­array is represented by an infinite function expression. • By using pair representation of disjoint union elements, operation is convertible to finite, computable format. • Operation performs one­level indexing upon array a returning another array if a has more than one dimension. • Still model is to clumsy to be used in practice. • Real programming languages allow arrays of numbers, record structures, sets, . . . !

  8. System of Type Declarations T  Type­structure S  Subscript T ::= nat| bool| array [N1... N2] of T |record D end D ::= D1;D2|var I:T C ::= …| I[S]:= E |... E ::= ... | I[S] |... S ::= E | E, S

  9. Denotable­value = (Natlocn + Boollocn + Array + Record + Errvalue) l  Natlocn = Boollocn = Location a  Array = (Nat  Denotable­value) x Nat x Nat r  Record = Environment = Id  Denotable­value

  10. Semantics of Type Declarations • T: Type­structure  Store  (Denotable­value x Poststore) • T[[nat]] =  s: let (l,p) = (allocate­locn s) in (inNatlocn(l), p) • T[[bool]] =  s: let (l,p) = (allocate­locn s) in (inBoollocn(l), p)

  11. T[[array [N1... N2]of T]] =  s: let n1 = N[[N1]] in let n2 = N[[N2]] in n1 greaterthan n2 (inErrvalue(), (signalerr s)) [] get­storage n1 (empty­array n1 n2 ) s • T[[record D end]] =  s: let (e, p) = (D[[D]] emptyenv s) in (inRecord(e), p) Type structure expressions are mapped to storage allocation actions!

  12. Semantics of Type Declarations • get­storage: Nat  Array  Store  (Denotable­value x Poststore) • get­storage =  n:  a:  s: n greater n 2  (inArray(a), return s) [] let (d, p) = T[[T]]s in (check(get­storage (n plus one) (augment­array n d a)))(p)

  13. augment­array: Nat  Denotable­value  Array  Array • augment­array =  n:  d: (map, lower, upper): ([n | d] map, lower, upper) • empty­array: Nat  Nat  Array • empty­array =  n1 :  n2 :(( n:inErrvalue()); n1 ; n2 ) • get­storage iterates from lower bound of array to upper bound allocating the proper amount of storage for a component. • augment­array inserts the component into the array.

  14. Declarations • D: Declaration  Environment  Store  (Environment x Poststore) • D[[D1;D2 ]] =  e:  s: let (e’, p’) = (D[[D1]]e s) in (check (D[[D2 ]]e’))(p) • D[[var I:T]] =  e:  s: let (d, p) = T[[T]]s in ((updateenv [[I]] d e), p) • A declaration activates the storage allocation strategy specified by its type structure.

  15. Array Indexing S: Subscript Array  Environment  Store  Storable­value S[[E]] =  a:e:  s: cases (E[[E]]e s) of . . . [] isNat(n) access­array n a … end S[[E, S]] =  a:  e:  s: cases (E[[E]]e s) of … [] isNat(n)  (cases (access­array n a) of … [] isArray(a’ )  S[[S]]a’ e s ...end) ... end

  16. Array Assignment C[[I[S] := E]] =  e:  s: cases (accessenv [[I]] e) of ... [] isArray(a)  (cases (S[[S]]a e s) of ... isNatlocn(l) (cases (E[[E]]e s) of ... [] isNat(n)  return(update l inNat(n) s) ...end) ...end) ...end Assignment is first order (location, not an array, is on left­hand­side).

More Related