Rules

No magic.

No silent failures.

Default behaviour must be the most fragile. That is, if non-default behaviour was desired, but not requested, this error should be obvious as soon as possible.

When we can't guarantee something, shift the responsibility and allow the programmer to decide for himself.

Value raw performance. If a certain feature seriously impacts performance, it should be optional or it should be possible to work around it.

Features

Syntax

I haven't finalized my thoughts on the syntax yet and any input (as for all the other things) will be very valuable.

Fauna will be similar to C-like languages where possible (curly braces etc) while trying to be more consistent and readable. Notable differences are loops (or quite probable lack thereof, I've not decided yet), some more useful replacement for the switch statement and shorter closure syntax.

Object level concurrency

At the core of Fauna is an idea of "Active Object", a merger between OOP and actor model philosophies.

All objects are actors, that is, each object has a separate execution thread, and never more than one. All actors are objects, that is, all actors have state and must respond to method calls.

Internally, we implement actors/objects as green threads. In VM, there is one real thread per core, plus a messenger thread, plus some load balancing mechanism.

Unshared mutable data

Not everything is an object. Integers, strings, arrays, maps, closures etc. are data. Data is not objects, it is owned by objects.

All data is owned by exactly one object at any given time. When sending a message to another object, data is copied (unless it's no longer in use by the object after the message is sent; see below).

Variables are mutable. Data structures are mutable and are always passed by value. Internally this is done by refcounting and recursive copy-on-write mechanism similar to how PHP's array works.

For example:

a = [[1, 2, 3], [4, 5, 6]], b = a, a[0][1] = 7, b[0][2] = 8, print a, // [[1, 7, 3], [4, 5, 6]] print b, // [[1, 2, 8], [4, 5, 6]] // At this point [4, 5, 6] is shared (refcount 2), the first element and outer arrays are not (refcount 1)

Data is refcounted at compile time where possible and at runtime in other cases. It should be freed/reused imediately on zero refcount. Compile time counting allows to reduce the refcount before the last use, allowing to avoid needless copying in case the last use is mutation or a method call.

Data and object accesses are syntactically different.

Typing

With regards to data, strong dynamic typing is employed. The only implicit conversion is to boolean. Type hinting for function arguments should be available.

With regards to objects, strong typing is not enforceable due to the distributed nature of the system, hence duck typing is employed. Some kind of interfaces should be implemented so that two well-intentioned nodes can avoid any misunderstanding.

Memory management

Due to how data structures are supposed to work, cyclical references should be impossible (i.e. a[#self] = a must create a copy of a before the assignment and assign it to a[#self] ).

Since there is always at most one thread working with every data structure, atomicity should not be a problem.

With these points in mind, it seems that reference counting alone should suffice for in-object memory management. However, should the need for a proper garbage collection ever arise, all data being tied to exactly one object at all times allows for ocassional mark-and-sweep garbage collection on a per-object basis without the need for a global lock.

Additionally, an effort could be made to keep all data created in a specific object at close memory addresses by preallocating chunks of memory of configurable size on object creation/expansion, which would allow for very efficient CPU cache utilization.

Statement concurrency

All statements return either data (e.g. i = 3 would return 3 ) or a promise (e.g. r = obj->fun() ). In the second case the statement is considered to be finished when the promise resolves.

By default, statement will stall further function execution until it finishes, which is what happens in most languages. However, if you prepend the statement with a ~ , the next statement will be executed immediately. You can then use ... on a separate line to wait for all unfinished statements.

A promise can be assigned to a variable and the variable is considered ready when said promise resolves. Since the statements will by default wait until any returned promise is resolved, a promise can be waited on by simply writing the variable it's in as a complete statement, e.g. ~ promise = obj->fun(); other->stuff(); promise; .

Promises don't go outside of the creating function, except between the function and any closures defined and called inside of it.

Statements can be also grouped in code blocks, which themselves return a promise. They can have internal concurrency controlled by the different terminators and can themselves have a terminator ( ; by default). They are considered finished depending on what the last terminator was, that is, whenever the next statement can start but there isn't one.

For example:

// These blocks will execute simultaneously and the order in which the messages will be printed is unknown. ~ { // This block will not stall execution objA->ping(); print 'Object A pinged'; // This message will be printed after the above method call finishes } ~ { // This block will execute immediately without waiting for previous two statements to finish objB->ping(); print 'Object B pinged'; } ... // Wait for both blocks to finish print 'Everything is OK'; // This message will be printed last

Additionally, there are sequential and concurrent binary operators, for example & and ~& .

Variables

Variables start with lowercase characters. Variable type is determined by its value by default and can be changed if a new value is assigned, but the type can be assigned explicitly, and in that case trying to assign a different type will fail.

x = 1 ; Number y = 2 ; Map < Atom : Number > point = [ #x : x , #y : y ] ; point = [ 1 , 2 ] ; // this will fail

Data types

Atom

Atoms are basically global constants without a value. They are also called "symbols" in Ruby. They either start with a # , or can be enclosed in backticks. Note that #foo and `foo` are equal. In the example above, #x and #y atoms are used as map keys.

Array

Arrays are zero-indexed collections of data. They are untyped by default, that is, can contain elements of mixed types, but the array type can be specified - either by defining a corresponding value, or by creating an array explicitly.

a = [1, 2, 3]; Array<Number> b = [1, 2, 3]; c = Array<Number>([1, 2, 3]);

Arrays, as all other data, are passed by value (see above).

Map

Maps are hash maps that can be indexed by any type of data and contain any type of data. They too can be typed.

a = [ #foo : 'atom' , 1 : 'number' , 'binary' : 'binary' , "string" : "string" ] ; Map < Binary : Binary > b = [ 'hurr' : 'durr' , 'herp' : 'derp' ] ; Map < Map < Atom : Number > : Atom > points = [ [ #x : 1 , #y : 1 ] : black , [ #x : 2 , #y : 2 ] : white ];

Boolean

Booleans are a special type that only allows values true and false . Boolean is also the only type that other types are implicitly converted to.

Null

Null is a special type/value that can be obtained by using the keyword null . Any operation with null will result in a failure, except for the ? and !? operators.

a = null; if a!? print 'Oops!'; b = a | Default; a == a; // this will crash

Closure

Closures are a bit tricky because we want lexical scope and mutable variables, but we also can't allow variable access from outside the object's thread, but we certainly want to be able to pass closures around.

We resolve this by introducing variable copying. The values of the variables at the time of closure's definition are copied to the closure and the closure does not have write access or the current value of the original variables.

Clousure syntax is minimal: a pair of parenthesis with a statement afterwards. For example:

fa = (x) x + '!'; greeting = 'Hello'; what = 'world'; fb = (&greeting) greeting + ' ' + what. // Copy the value of greeting to the closure greeting = 'Hi'; what = 'planet'; fc = (a, b, c) { a() + ' ' + b() + ' ' + c(); } print fc(fa, fb, () ':)'); // Hello planet ! :)

Trying to pass a closure that accesses outer scope's variables in a message will result in a failure.

Distributed computing

Since all data is tied to a specific object it should be of no concern where every object physically resides. With persistence and mirroring features discussed below an object could even exist in memory of several devices at once or none at all.

Live objects can be replicated to other nodes, however there should only be one "master" at every point in time, which should be negotiated between the mirrors. Alternatively, any operation on such objects might be considered a transaction that will only commit if there is no conflict between nodes.

With unique IDs for objects and a DHT addressing system objects could be freely moved between nodes and worldwide networks could be created. Whether it's a good idea is still debatable, but it would, in theory, allow a "true cloud" where everything is synchronized to everything else and physical location of devices makes no difference.

Transactions

Copy-on-write data and encapsulation allows relatively easily and effectively implementing ACID transactions on language level. Transactions will, however, be optional, since 1) they still impose a serious performance penalty 2) a transaction must begin and end at some point, therefore it can't all be a single transaction from the start of the program. Transactions are explicitly started with a do keyword with a statement or a code block.

Atomicity

On object level, all changes made during the transaction can easily be stored and rolled back. Between objects it is still possible to remember which message was sent from which transaction and the changes it caused. Transactions between nodes will require some kind of a two-phase commit protocol, specifics of which are currently beyond my understanding, but I'm sure it can be done.

A problem arises if we allow custom message processing, e.g. an Erlang-like receive loop. In that case it might not be possible to automatically determine which changes resulted from a specific message. We may require manual transaction processing in this case.

It is also obvious that many I/O operations cannot be rolled back and a failed transaction with I/O can leave network connections or files in a dirty state. We should therefore not allow dirty functions in a transaction by default, but only with a special flag, so that it becomes programmer's responsibility to clean up afterwards.

Consistency

In the meaning of data being valid, the encapsulation of data in a single object and the lack of multithread access should be enough to guarantee consistency in a single object. Consistency will also be implemented on a database level (see below).

In the meaning of data being same across all nodes, which only applies in the case of mirrored objects, the easiest way to achieve that is to have a single master per each mirrored object at all times and make every operation on such object a transaction.

Isolation

MVCC is employed to ensure that every transaction sees a consistent snapshot of objects and data. Multiple versions of changed data are kept around in objects until all transactions finish and for some time afterwards.

Only transactions are isolated from each other. For performance reasons, changes on objects that are not part of a transaction are commited instantly and the previous values are not recorded. The reasoning is that these by-definition-atomic transactions could have happened at any point in time anyway, therefore there is no reason to remember when they actually did.

A serious problem arises with trying to implement a global timestamp in a case when nodes don't operate as a well-defined cluster, but may connect to new nodes in the course of a transaction, or when clusters intersect. However, one might argue that the notion of a "moment in time" is pointless in such a scenario and no attempt should be made to provide complete isolation in this case.

Durability

Durability is optional and can be achieved either by using the built-in database, or by the object itself taking over the state of transaction, however in the latter case it is programmer's responsibility to ensure atomicity and provide rollback facilities.

Purity

The concept of purity is problematic in non-functional languages (like Fauna). There are at least six different meaningful levels on the pure-impure spectrum to consider:

A function that only works with its arguments, never accesses any objects and always produces the same result for specific input. A method that only reads the state of its own object and always produces the same result for specific input and state, A method that only modifies the state of its own object, never accesses any other objects or I/O and always produces the same result and new state for specific input and previous state, A method that never does I/O and never calls a method that does I/O. A method that does I/O or calls methods that do I/O, but will clean up after itself in case a transaction fails. A method that does not conform to any of the above restrictions.

Obviously, the highest level propagates up the stack. Levels 1-3 can be asserted at compile time. Level 4 can be checked at runtime - we should set a flag in the stack if a method claims to be level 4 and fail if it calls 5 or 6. Level 6 can also be asserted at compile time, and level 5 is basically level 6 with a special programmer-set flag that we'll blindly believe. If there is no flag and we don't determine what the method is at compile time, then we assume it's 4 until proven otherwise.

We should only disallow 6 in transactions by default, since we can guarantee ACID for 1-4 and it's not our fault if something goes wrong with 5 (although I believe the whole node should crash in that case).

Levels 1 and 2 allow a lot of optimizations and maybe even caching if it's feasible. Level 3 allows, among other things, logging the method and the call instead of the result, which may result in smaller log sizes (but slower replays, but who cares about that). Level 4, if not for anything else, provides a safety net for the programmer against any libraries that try to do something they're not expected to. Level 5 allows to run transactions where it would otherwise be impossible.

I hope I have convinced the reader that all of these kinds of restrictions may be useful in certain scenarios, but I can't decide on six different terms for them. But let's try.

pure const , since if we don't change the object local , since we don't touch other objects clean , since we don't leave any dirt trusted , since that's what programmer says dirty , because it is what it is

Security

It would be really nice if we could provide some serious security features but I only have abstract thoughts at this point.

We could split objects into "areas" and allow different objects and nodes access to different "areas". We could also provide sandboxing.

It could also be useful if we could allow object access on a per-method basis, but that could also set up the programmer to create vulnerabilities. Maybe we'd be better with proxies instead.

In any case, I believe we should somehow allow nodes owned by different people to communicate directly instead of making users bolt on some half-assed RPC solutions.

Database

Since thoughts of Fauna started after I failed to make an ACID key-value database in Erlang, it would be logical to implement a built-in database right from the start.

Mostly it will use the general Fauna features, like replication, transactions and serialization, but it will also provide proper persistence (HDD and SSD friendly), load balancing, indexes and stuff.

And stuff.