360 likes | 457 Views
Type Generativity and Sharing. Aaron White Fall Semester, 2002. Generativity?. Generative type declarations yield “new” types datatype t F(S).t Incompatible w/ all other types Incompatible w/ concrete rep (outside the scope of its definition). Generativity?.
E N D
Type Generativity and Sharing Aaron White Fall Semester, 2002
Generativity? • Generative type declarations yield “new” types • datatype t • F(S).t • Incompatible w/ all other types • Incompatible w/ concrete rep (outside the scope of its definition)
Generativity? • Mitchell & Plotkins existentials for abstraction were ok, but open failed to provide a unique witness. • Dot notation alleviates this, but “type identity” is lost. • Generativity, along with manifest types (translucent sums) resolves this (in the syntactic approach)
Why Generativity? • Key mechanism for abstraction • Would like functor applications to yield new type F(S1).t != F(S2).t • Still, type propagation must be respected • Would like result types to be compatible w/ argument types (for instance: we wish F to propagate input type t to the result structure) S.t = F(S).t
Two Approaches to Generativity • Stamp based • SML style • Syntactic • Modula-2 • Does not extend well
Leroy proposes… … a syntactic theory of generativity, that is equivalent to stamp based “operational” account for generativity. Also claims, syntactic approach is more powerful.
Stamp-based Generativity • Syntactic objects elaborated into semantic objects • Mapping environment from types t to stamp τ • “New types” generate fresh stamps • Sharing specifications are checked during elaboration for stamp equality
Programs are of the form: structure x = struct datatype t type t’ = t (etc…) end; functor f(x:S) = struct .. end Stamp-based elaboration formalism
Stamp-based elaboration formalism • FS(Σ) is the set of stamps occurring free in Σ • Г is an elaboration environment (identifiers to semantic objects) • W is the set of “active” stamps
Path Elaboration Selected Elaboration Rules
Structure binding Type binding Selected Elaboration Rules
Selected Elaboration Rules Opening structures Datatype definition (generative)
Selected Elaboration Rules Elaborating type declaration in a signature Elaborating sharing in a signature
Selected Elaboration Rules Functor application (generative)
Problems w/ Syntactic Generativity • Not so straightforward as one would hope • F(struct … end).t should not equal F(struct … end).t, despite being syntactically the same • structure x = struct datatype t; val v end structure y = (x : sig type t end) x.t != y.t syntactically, though a different view shouldn’t generate a new type.
Problems cont. • More problems… structure s : (sig type t; val v:t end); val x: s.t structure s = struct datatype t end x : s.t??
Solutions… • Restrict functor applications to paths in the usual way. • Use of manifest (translucent) types to propagate sharing information. • Separate identifiers and names
Identifiers and Names • Each time an name for an identifier is written down, append a mark to ensure the identifier is unique. • Use the name for component access in a structure, but use the identifier to bind variables and check for type equivalence. • Identifiers can be ‘marked’ during type checking, and needn’t appear in the program text.
Example structure s = … val y = s.t structure s = … val x: s.t structure s = … x: ?, y: ? structure s = … val y = s.t structure s’ = … val x: s’.t structure s’’ = … x: s’.t, y: s.t
Programs are of the form: structure x = struct datatype t type t’ = t (etc…) end; etc. Syntactic type system formalism • Except now manifest (translucent) types can appear in a signature specification: • type ti = T
Syntactic type system formalism • BV(D) is the set of bound identifiers in the specification D (specification = contents of a signature) • S/p strengthens the signature s that can be assigned to path p. Selfification.
Datatype declaration (generative) Type binding (non generative) Selected syntactic typing rules
Selected syntactic elaboration rules • Datatype elaboration • Type binding elaboration
Signature strengthening • Allows selfification • (type ti; D)/p = type ti = p.t; D/p • This would not be possible w/o the manifest type construct, and w/o signature strengthening, we lose type propagation in his framework
The Reduction • To inject TypModl (stamp based) into TypModl’ (path based), TypModl programs must first be normalized • A-Normalization • S-Normalization • Finally, some syntactic swapping
A-Normalization • Since TypModl restricts functor arguments to paths, must massage TypModl to the same. Intermediate applications are bound to temporary structures (N is the set of fresh identifiers)
S-Normalization • Since TypModl’ can only express sharing constraints in the form of manifest types in signatures (type t = p.t), TypModl must be massaged once more to ‘localize’ sharing constraints.
Localize? • The sharing constraint must immediately follow what it is locally constraining; sig specs must end up in the form “type t; sharing t = p”
Why normalization? • Because, once we have A-Normalized, and S-Normalized, we are prepared to express TypModl in TypModl’ after swapping some syntax:
The Claim (modulo the remaining syntactic differences)
To prove the claim… • Establishes relation between the semantic objects, and the signature expressions. • This translation from signature expressions to semantic objects is deterministic! (as opposed to elaboration of non-manifest types in signatures in TypModl)
To prove the claim… By normalizing and then using manifest types, our constraint on the stamp for ti is immediately obvious, making elaboration deterministic.
To prove the claim… • Using the translation, it can be shown that if T is a type expression, [T] is what T would elaborate into. • Further, if S is a TypModl signature, [S] is principal • This is because sharing constraints in the signature get satisfied immediately, and generative types are assigned fresh stamps in the usual fashion
To prove the claim… • The ‘big’ inductive claim, if s is a TypModl signature, then there exists S in TypModl’ such that the following holds. • The converse relating TypModl’ signature translation to TypModl signature elaboration results is proven true. then and
Conclusion • TypModl reduces to TypModl’ • The path-based model at least as expressive as the stamp based model presented. • Somewhat deceiving, as the path-based system is fragile, and fails at extension, whereas the stamp-based model, despite its shortcomings, is somewhat more robust.