140 likes | 232 Views
Implementing Relationships in Whiley. David J. Pearce School of Engineering and Computer Science, Victoria University of Wellington, New Zealand Dagstuhl Seminar 10152. Computation. Data. Computation Versus Model. Composition. Delegation. Querying. Polymorphism. Pointers. Computation.
E N D
ImplementingRelationships in Whiley David J. Pearce School of Engineering and Computer Science, Victoria University of Wellington, New Zealand Dagstuhl Seminar 10152
Computation Data Computation Versus Model
Composition Delegation Querying Polymorphism Pointers Computation Data Records Databases Datatypes Structuring Values Sets Computation Versus Model
User Book Compiler Loader * 1 ID Name Code Title Compile(…) LoadClass(…) Behavioural Structural * 0..1 What about Relationships ?
Structural Relationships • Can implement using pointers (as above) • Can implement using collections class User { private Set<Book> books; … } class Book { private User borrower; … }
Behavioural Relationships compiler loader • Must be implemented using Pointers class Compiler { private ClassLoader loader; … } class ClassLoader { private Compiler compiler; … }
Introducting Whiley !!! • Hybrid OO – Functional Language • Compiles to JVM • Performs Compile-Time Checking of Constraints
Functional Core • Functional functions • No aliasing or side-effects • Pass-by-value records, lists + sets define int where$ >= 0as nat int f(nat a, nat b)ensures $ > 0: ifa == b: return 1 else: return a + b
Imperative Outer Layer define process(int x, int y)as PointProc void PointProc::update(int z): this->y = z void System::main([string] args): PointProc pp = spawn (x:1,y:2) pp->update(3) print str(*pp) • Processes • Like objects, but stricter concurrency • Send and Receive Messages • Majority of work done with functions • Are necessary for I/O and … ?
Borrows Relationship in Whiley define (int id, string name)asUser define (int code, string title)asBook define{(User user, Book book)}where no{ x in $, y in $, x.book==y.book && x!=y } as Borrows int count(user u, Borrows bs): return|{ x|x in bs, x.user==u }| • Structural relationships implemented in functional core • (No need to implement them elsewhere)
Inheritance define int where$ >= 0asnat defineintwhere $ > 0 as pint defineintwhere $ < 10 as lten define nat where $ < 10 as bnat • Inheritance is implicit, not explicit • Multiple inheritance is no problem lten nat bnat pint
Compiler-Loader in Whiley define(…)as JvmClass defineprocess(string path,…)as Compiler defineprocess(Compiler cmp,…)as Loader JvmClass Loader::loadClass(string name): … … returncmp->compile(filename)
Another behavioural example • Delegator Pattern • Classic behavioural relationship • Implemented with processes in Whiley • Requires polymorphism … not implemented yet! FileReader BufferedReader StringReader
whiley.org (under construction)