I've been reading recently about the smart contract languages and approaches in different platforms. My interest was in to what degree the approaches of functional programming (which I'm a fan of) are applied, especially pure functions
https://en.wikipedia.org/wiki/Pure_function. From my opinion writing contracts methods without mutable state would considerably improve code safety and testability and (if applied to cryptocurrency) reduced the amount of these infamous disasters with lost or frozen funds etc.
Here is the short list (too short actually, I should admit) of the current state of art in smart contracts what was able to find, used in production and Turing complete languages.
1.
Ethereum Solidity. Statically typed language, mutable state. There are
constant and
pure modifiers to mark functions as not changing any state, but this is used as additional restriction, not applicable to the whole contract code, if you need to change state, you only option is to do it inside functions body.
function SayHello() public constant returns(string says) {
return greeting;
}
function returnTrue() public pure returns(bool response) {
return true;
}
2.
Neo C#, VB.Net, F#, Java, Kotlin, Python, C, C++, Golang, JavaScript. The list of supported languages is impressive. Still, looking in the code examples, the constructions used are all in imperative style, expecting to operate with mutable state:
Storage.Put(Storage.CurrentContext, domain, to);
3.
Lisk, EOS, Stratis, Ardor, others? It sounds weird, but when I dig deeper, it turned out all of these platforms are either not supporting Turing complete smart contracts or are still in development. So that there are only 2 real platforms with possibility of writing complex smart contracts - Ethereum and NEO. Probably I'm missing something?
But back to functional approach and what I started with. I've been tinkering with the idea for some time and came up with the following code design prototype (not a real thing yet). The example is in JavaScript as it has better adoption, I plan to rewrite example in PureScript or Elm.
/**
* Move balance contract
*
* Simplified example of how a cryptocurrency logic could be implemented as a smart contract
* with functional approach
*
*
* @param {Object} ctx Execution context
* @param {BigInteger} amount Amount to move
*
* @return {Object} Array of properties to set after function execution
*/
function contract(ctx, amount) {
var callerBalance = getProperty(ctx.caller, 'balance', zero);
var calleeBalance = getProperty(ctx.callee, 'balance', zero);
var newCallerBalance = callerBalance.subtract(amount);
var newCalleeBalance = calleeBalance.add(amount);
return [
{ target: ctx.caller, balance: newCallerBalance },
{ target: ctx.callee, balance: newCalleeBalance },
];
}
In the above example (it's almost a pseudocode for now) I tried to demonstrate how could smart contracts be implemented to change state on blockchain and still not having mutable operators, in the sense that all the inner logic and state changes inside the function would not matter. Instead, only the return values are used to associate key/value store with new state changes.
What you think, does it sound any interesting?