11. Piccola A Small Composition Language PS — Piccola Roadmap > > > > > Applications = Components + Scripts Piccola layers Forms + Agents + Channels Wrapping components Status: semantics, types … © 2003, Oscar Nierstrasz 1.2 PS — Piccola Roadmap > > > > > Applications = Components + Scripts Piccola layers Forms + Agents + Channels Wrapping components Status: semantics, types … © 2003, Oscar Nierstrasz 1.3 PS — Piccola Applications = Components + Scripts Components both import and export services Scripts plug components together © 2003, Oscar Nierstrasz 1.4 PS — Piccola Towards a Composition Language Scripting languages Configure applications from components Coordination languages ADLs Specify architectural styles Glue languages Adapt components Configure applications from distributed services © 2003, Oscar Nierstrasz 1.5 PS — Piccola Roadmap > > > > > Applications = Components + Scripts Piccola layers Forms + Agents + Channels Wrapping components Status: semantics, types … © 2003, Oscar Nierstrasz 1.6 PS — Piccola Piccola — A Small Composition Language Piccola agents compose and coordinate external components Define styles Script connections Scripts Agents Coordinate activities Forms Channels Adapt protocols Components © 2003, Oscar Nierstrasz 1.7 PS — Piccola Piccola Layers Applications Compositional styles Standard libraries components + scripts streams, events, GUIs, ... basic coordination abstractions, built-in types Piccola operator syntax, introspection, component wrappers Piccola-calculus forms, agents, channels, services © 2003, Oscar Nierstrasz 1.8 PS — Piccola Roadmap > > > > > Applications = Components + Scripts Piccola layers Forms + Agents + Channels Wrapping components Status: semantics, types … © 2003, Oscar Nierstrasz 1.9 PS — Piccola Forms + Agents + Channels > Forms embody structure — immutable, extensible records — they unify components, services and namespaces > Agents embody behaviour — concurrently executing scripts — they unify concurrency and composition > Channels embody state — mailboxes for communicating agents — unify synchronization and communication. © 2003, Oscar Nierstrasz 1.10 PS — Piccola Piccola in a Nutshell > A form consists of a sequence of bindings: helloForm = text = "hello world " do : println text > # a form # a binding # a service A script composes components by invoking services makeFrame title = "AWT Demo " component = Button.new(helloForm) ? ActionPerformed helloForm.do > High-level connectors hide details of object wiring. © 2003, Oscar Nierstrasz 1.11 PS — Piccola Hello World © 2003, Oscar Nierstrasz 1.12 PS — Piccola Piccola Syntax Values are bound with “=”, services with “:” a b c d = = = = 1 (x=1, y=2) (b, z=3) c.z f: a println f() g u: u.x + u.y println g(b) h u v: u + v println (h 1 2) © 2003, Oscar Nierstrasz # # # # 1 3 3 binding nested binding extension projection # service (no arg) # application (empty form) # service # application # curried service # curried application 1.13 PS — Piccola Indentation Complex forms can be expressed by multiple indented lines b = x = 1 y = 2 # nested form println g b y=b.x # # # # 2 invoke println with arg… invoke g with arg … argument to g extended with new binding Cf. Haskell and Python © 2003, Oscar Nierstrasz 1.14 PS — Piccola Fixpoints Recursive services and forms are specified with the def keyword def fib n: if n<=2 then: 1 else: fib(n-1) + fib(n-2) # naïve fibonacci Note that if is a curried service … © 2003, Oscar Nierstrasz 1.15 PS — Piccola Control structures > Control structures are defined as standard Piccola services if bool cases: 'cases=(then:(),else:(),cases) bool.select(true=cases.then, false=cases.else)() © 2003, Oscar Nierstrasz 1.16 PS — Piccola Local bindings and defaults Bindings are made private with the ' operator Defaults are defined by extension if bool cases: 'default = then:() else:() 'cases = (default, cases) 'doit = bool.select true = cases.then false = cases.else doit() © 2003, Oscar Nierstrasz # define defaults # set defaults # select service # invoke it 1.17 PS — Piccola Agents and Channels You can start a concurrent agent by invoking run, passing it a form with a do service. run do: … # do something time-consuming A channel is a mailbox for communicating with agents: c = newChannel() run (do: println c.receive()) c.send("hello!") # create a channel # start an agent # send it a message hello! © 2003, Oscar Nierstrasz 1.18 PS — Piccola Infix and prefix operators Services may be defined as infix or prefix operators: newVar X: 'c = newChannel() 'c.send(X) *_: 'val=c.receive() 'c.send(val) val _<-_ X: 'val=c.receive() 'c.send(X) X x = newVar(1) println (*x) 1 println (x<-2) 2 NB: Without agents and channels, Piccola is purely functional © 2003, Oscar Nierstrasz 1.19 PS — Piccola Example: Futures Agents and channels can be used to define common coordination abstractions, like futures, semaphores, readers/writers synchronization policies and so on … A “future” is a ticket for a value that is still being computed: future task: 'result = newChannel() 'run(do: result.send(task.do())) result.receive f12 = future(do: fib(12)) println f12() 144 © 2003, Oscar Nierstrasz 1.20 PS — Piccola Explicit namespaces Everything is a form, including the current namespace, known as root. 'x=1 Is just sugar for: root=(root,x=1) © 2003, Oscar Nierstrasz 1.21 PS — Piccola Reflection Piccola provides various built-in services to inspect and manipulate forms countLabels f: 'count = newVar(0) 'forEachLabel form = f do L: count <- 1 + *count *count # a standard service println (countLabels root) 75 © 2003, Oscar Nierstrasz 1.22 PS — Piccola Roadmap > > > > > Applications = Components + Scripts Piccola layers Forms + Agents + Channels Wrapping components Status: semantics, types … © 2003, Oscar Nierstrasz 1.23 PS — Piccola Language bridging Piccola does not provide booleans, numbers or strings — instead, these are forms that wrap Host language objects as components — Host objects are automatically wrapped as forms when imported to Piccola, and unwrapped when Host services are invoked — Special wrappers may be registered to adapt the interfaces of selected components © 2003, Oscar Nierstrasz 1.24 PS — Piccola Wrapping AWT components Java AWT Frames are automatically wrapped to add a ? connector: def wrapComponent X: ... _?_ aHandler aClosure: ... ... wrapper.frame X: wrapComponent X ... registerWrapper "java.awt.Frame" wrapper.frame © 2003, Oscar Nierstrasz 1.25 PS — Piccola Roadmap > > > > > Applications = Components + Scripts Piccola layers Forms + Agents + Channels Wrapping components Status: semantics, types … © 2003, Oscar Nierstrasz 1.26 PS — Piccola Other features > > > > > Module system based on explicit namespaces Anonymous services (lambdas) Dynamic scoping on demand A simple testing framework Lists, Sets and Maps © 2003, Oscar Nierstrasz 1.27 PS — Piccola Piccola Semantics The semantics of Piccola is defined using a process calculus with first-class namespaces © 2003, Oscar Nierstrasz 1.28 PS — Piccola How forms support composition © 2003, Oscar Nierstrasz 1.29 PS — Piccola Results so far > Piccola implementations for Java and Squeak — Sophisticated language bridging — Efficient partial evaluation — Various experimental composition styles > Formal semantics — π calculus with first-class namespaces — Limited reasoning about styles and composition > Publications and software: www.iam.unibe.ch/~scg © 2003, Oscar Nierstrasz 1.30 PS — Piccola What’s missing? Explicit components, connectors & styles > Type-checking provided and required services > Reasoning about styles and compositions > Adapting/reengineering existing services > © 2003, Oscar Nierstrasz 1.31 PS — Piccola The future of composition? > > > > > Dynamic composition of ubiquitous services Service discovery and negotiation Reconfiguration validation and run-time monitoring New, fine-grained models of components, composition and evolution Component models for mobile, pervasive and embedded systems © 2003, Oscar Nierstrasz 1.32 PS — Piccola License > http://creativecommons.org/licenses/by-sa/2.5/ Attribution-ShareAlike 2.5 You are free: • to copy, distribute, display, and perform the work • to make derivative works • to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. • For any reuse or distribution, you must make clear to others the license terms of this work. • Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. © 2003, Oscar Nierstrasz 1.33