Upload
john-colin-white
View
222
Download
2
Embed Size (px)
Citation preview
FLEXIBO Language Design
Work-in-Progress
Yifeng Chen
University of Leicester
7 May 2004 UNU/IIST, Macau
Language Design
Programming languages form an interface between computer science and computer engineering.
Good software engineering practice eventually goes into language design.
Design Philosophy of FLEXIBO
Open source with different levels of trust in decentralized multi-user environment.
Developing || Testing || Running Limiting the effective scope of bugs. Specifications with optional controls. True flexibility = flexibility + inflexibility. Compilation as execution.
Main Features
Everything is a value. Classes, types, methods and
specifications of different “shapes”. Variables of different “colors”. Correctness, ownership, resources cont
rol and smooth exception handling. Run-time type checking for compilation. Full Java access
Example Program: Hello World!
print "Hello world!"
Imperative Programming
var x:= [12, "A string in an array"];
x[0] := x[0] + 1;
if x[0]>12 print "yes\n" else print "no\n";
{var x:="inside a block\n"; print x;
{var x:="inside another block\n"; print x;}};
while x>0 {print x; x:= x - 1};
while true {}
Class, Method and Invocation
var c := class Value {
var dynamic x :="static value in class\n";
var static n := method [x] {var x:="value of local variable\n"; return x;};
var static m := method x { return x}};
var o:= new c; print o.n ["value from argument\n"];
print o.m ! "value from argument\n";
print o. (1 + m ! 2)
Specification and implementation
var spec := specification [x,y] pre: true post: true ;
spec.implement[body (x := "I implement by adding a prefix to "+x; return x)];
print spec["the argument.\n",2];
spec := specification [x,y] pre: true post: true ;
print spec["the argument.\n",2];
Ownership Control
print project; // show the shared project
project.spec := "I have modified changed this specification.";
print project; // show its change
project.spec := "I have reset this specification.";
project := "I want to delete the whole project.";
print project; // This point is not reachable.
Exception Handling
try {print 1+true;}
catch "Cannot add" {print "I have caught it.\n"};
var timer:= new tclass;
try {timer.start[2000]; while true {}}
catch "" {print "I have caught my own timeout error.\n"};
timer:=new tclass;
try {timer.start[5000]; while true {}}
catch "" {print "I cannot catch the server's timeout.\n"};
Overloading Method Invocation
var c := class Value { var m := method [a] { return a };
var n := class Value {var invoke:= method [a] return "Overloading method invocation is "+a}; };
print (new c).m ["invoked in a usual way\n"]; // x->m->a
var x:=new c ; print ((x.m).invoke ["Invoked as a pre-defined method\n"]);
print (new c).n ["invoked in a usual way\n"]; // x->m->a
print ((x.n).invoke ["invoked as a normal method\n"]);
Side Effect and Evaluation Order
var a := "Evaluated as an argument";
var c := class Value {
var dynamic y;
var m := method [z] {print z}};
var env := class Value { var x := new c; };
env . ( (print "Evaluate x in env\n"; x). (print "Evaluate m in x\n"; m) [ (print "Evaluate a in the defined environment.\n"; a) ] )
Language Reflection
print quote (1 + "A quoted exp is not evaluated.");
var c:= class Value {
var v:="\nA value in a class\n";
var b:= body (print v) };
print (c.b).eval [ ];
Methods in Different “Shapes”
var c:= class Method {var dynamic msg:= "together with a document of the method.\n";
var init:= method x {print msg; return x}};
var m:= new c[quote (y), body (print y)];
print m["Invoking a user-defined kind of method.\n"]
Colored Variables (att:var)
var P := class Value {var type :="Type of Persons\n";
var returnType := method [] {return att:type}; };
var S := class P {var type := "Type of Staff\n"} ;
print (new P).returnType[]; // or (new P).att:returnType[]
print (new S).returnType[];
Colored Variables (def:var)var SimpleAccount := class Value {
var dynamic private deposit:=200;
var dynamic private withdrawal:=100;
var getTotal := method [] return (deposit - withdrawal);
var payable := method [payment] return (payment< def:getTotal[])};
var Account := class SimpleAccount {
var dynamic private savings:= 400;
var getTotal := method [] return (super.getTotal[] + savings); };
var sa := new SimpleAccount;
print sa.payable[200]; // not payable
var a := new Account;
print a.payable[200]
The Default Rule
y . ( x . m ! a )
y --- def variable (evaluated in defined env)
x --- att variable (evaluated in y)
m --- att variable (evaluated in x)
a --- def variable (evaluated in defined env)
The default rule implies dynamic binding.
Colored Variables (gen/raw:var)var Account := class Value {
var dynamic deposit:=200;
var dynamic withdrawal:=100;
var gen:balance((deposit - withdrawal),
method b { deposit := b + withdrawal}) };
var account := new Account;
print account.balance;
account.balance := 300; // updation
print account.balance;
print account.raw:balance // return the expressions
Types and Run-time Checking
print "This must be a string according to the type constraint.\n":String;
print Int + Float - Int; // This is used for user-defined type checking algorithm.
print "This string is wrongly typed!" : Int
User-defined Types
var YType := class Value { var dynamic type;
var init := method [ t ] { type:= t ; return this};
var check := method t {return type <= t.getType[]}; };
var Persons := class Value {var msg := "I am a person.\n"};
var Staff := class Persons {var msg := "I am a staff.\n"};
var YStaff := new YType[Staff];
var s:YStaff; // Upwards-closure type
s := new Persons; print s.msg;
var p:Staff; // Normal type requires exact match.
p := new Persons; print s.msg;
Future Work
Client-side HCI (using Java RMI). From FlexibO to C++. Exploring the new mechanisms. Integration of UML. Predicative semantics and proof of
development non-monotonicity.