Added list comprehensions and standard List, Option, Func, Pair, and Triple library (#105)

* Added standard List library and list comprehensions

Added List library.

Flatmaps WIP

Fixed dependency in flat_map

fcode generation

Updated tests to use custom list lib

Added comprehension test

Added stdlib

sanity

Test

* Extended stdlib for lists. Added error message for redefinition of stdlibx

* Fixed type template

* Improved stdlib

* More functions

* Fixed cyclic includes

* Refixed imports and added few tests

* Added fail test

* Undelete removed type spec

* Remove typo

* Fix iter function

* Fixed typo

* Added if guards and let statements in list comp

* Added more fail tests

* Option stliv

* 2 and 3 tuple stdlib

* Updated stdlib to new syntax. Added recursor and changed all/any functions

* Fixed performance issues. Changed include management

* Fixed hash type
This commit is contained in:
Radosław Rowicki
2019-08-14 13:53:58 +02:00
committed by Hans Svensson
parent 69ad8ce9bc
commit b669d2df1e
29 changed files with 760 additions and 57 deletions
+4
View File
@@ -0,0 +1,4 @@
include "cyclic_include_forth.aes"
contract CI =
entrypoint ci() = Back.back() + Forth.forth()
+4
View File
@@ -0,0 +1,4 @@
include "cyclic_include_forth.aes"
namespace Back =
function back() = 2
+4
View File
@@ -0,0 +1,4 @@
include "cyclic_include_back.aes"
namespace Forth =
function forth() = 3
+3 -3
View File
@@ -1,5 +1,5 @@
namespace List =
namespace MyList =
function map1(f : 'a => 'b, xs : list('a)) =
switch(xs)
@@ -14,8 +14,8 @@ namespace List =
contract Deadcode =
entrypoint inc1(xs : list(int)) : list(int) =
List.map1((x) => x + 1, xs)
MyList.map1((x) => x + 1, xs)
entrypoint inc2(xs : list(int)) : list(int) =
List.map1((x) => x + 1, xs)
MyList.map1((x) => x + 1, xs)
+7
View File
@@ -0,0 +1,7 @@
include "included.aes"
include "../contracts/included.aes"
contract Include =
entrypoint foo() =
Included.foo()
+23
View File
@@ -0,0 +1,23 @@
contract ListComp =
entrypoint sample1() = [1,2,3]
entrypoint sample2() = [4,5]
entrypoint l1() = [x | x <- sample1()]
entrypoint l1_true() = [1,2,3]
entrypoint l2() = [x + y | x <- sample1(), y <- sample2()]
entrypoint l2_true() = [5,6,6,7,7,8]
entrypoint l3() = [x ++ y | x <- [[":)"] | x <- [1,2]]
, y <- [[":("]]]
entrypoint l3_true() = [[":)", ":("], [":)", ":("]]
entrypoint l4() = [(a, b, c) | let is_pit(a, b, c) = a*a + b*b == c*c
, let base = [1,2,3,4,5,6,7,8,9,10]
, a <- base
, b <- base, if (b >= a)
, c <- base, if (c >= b)
, if (is_pit(a, b, c))
]
entrypoint l4_true() = [(3, 4, 5), (6, 8, 10)]
+2
View File
@@ -0,0 +1,2 @@
contract BadComp =
entrypoint failing() = [x + 1 | x <- [1,2,3], let x = "XD"]
+2
View File
@@ -0,0 +1,2 @@
contract BadComp =
entrypoint failing() = [x | x <- [], if (3)]
+2
View File
@@ -0,0 +1,2 @@
contract ListCompBad =
entrypoint failing() = [x | x <- 1]
+7
View File
@@ -0,0 +1,7 @@
// This contract should be compiled with no_implicit_stdlib option.
// It should include Lists.aes implicitly however, because Option.aes depends on it.
include "Option.aes"
contract Test =
entrypoint i_should_build() =
List.is_empty(Option.to_list(None))
+2 -2
View File
@@ -28,8 +28,8 @@ contract MultiSig =
let n = length(owners) + 1
{ nRequired = nRequired,
nOwners = n,
owners = Map.from_list(List.zip([1..n], caller() :: owners)),
ownerIndex = Map.from_list(List.zip(caller() :: owners, [1..n])) }
owners = Map.from_list(MyList.zip([1..n], caller() :: owners)),
ownerIndex = Map.from_list(MyList.zip(caller() :: owners, [1..n])) }
function lookup(map, key) =
switch(Map.get(key, map))
+3 -3
View File
@@ -1,5 +1,5 @@
namespace List =
namespace MyList =
function map1(f : 'a => 'b, xs : list('a)) =
switch(xs)
@@ -14,8 +14,8 @@ namespace List =
contract Deadcode =
entrypoint inc1(xs : list(int)) : list(int) =
List.map1((x) => x + 1, xs)
MyList.map1((x) => x + 1, xs)
entrypoint inc2(xs : list(int)) : list(int) =
List.map2((x) => x + 1, xs)
MyList.map2((x) => x + 1, xs)
+3 -3
View File
@@ -47,7 +47,7 @@ module Voting : Voting = {
let init(proposalNames: args): state =
{ chairPerson: caller(),
voters: AddrMap.empty,
proposals: List.map((name) => {name: name, voteCount: 0}, proposalNames)
proposals: MyList.map((name) => {name: name, voteCount: 0}, proposalNames)
};
/* Boilerplate */
@@ -73,7 +73,7 @@ module Voting : Voting = {
};
let addVote(candidate, weight) = {
let proposal = List.nth(state().proposals, candidate);
let proposal = MyList.nth(state().proposals, candidate);
proposal.voteCount = proposal.voteCount + weight;
};
@@ -121,6 +121,6 @@ module Voting : Voting = {
/* const */
let currentTally() =
List.map((p) => (p.name, p.voteCount), state().proposals);
MyList.map((p) => (p.name, p.voteCount), state().proposals);
}
+1 -1
View File
@@ -13,7 +13,7 @@ open Voting;
let print_tally() = {
let tally = call(other, () => currentTally());
List.map(((name, count)) => Printf.printf("%s: %d\n", name, count), tally);
MyList.map(((name, count)) => Printf.printf("%s: %d\n", name, count), tally);
let winner = call(other, () => winnerName());
Printf.printf("Winner: %s\n", winner);
};
+3
View File
@@ -0,0 +1,3 @@
contract StdInc =
entrypoint test() = List.map((x) => Func.id(x), [1,2,3,4])
+3 -3
View File
@@ -36,7 +36,7 @@ contract Voting =
function init(proposalNames: list(string)): state =
{ chairPerson = caller(),
voters = Map.empty,
proposals = List.map((name) => {name = name, voteCount = 0}, proposalNames) }
proposals = MyList.map((name) => {name = name, voteCount = 0}, proposalNames) }
function initVoter() = { weight = 1, vote = NotVoted}
@@ -53,7 +53,7 @@ contract Voting =
_ => delegate
function addVote(candidate, weight) =
let proposal = List.nth(state.proposals, candidate)
let proposal = MyList.nth(state.proposals, candidate)
proposal{ voteCount = proposal.voteCount + weight }
function delegateVote(delegateTo: address, weight: uint) =
@@ -93,5 +93,5 @@ contract Voting =
// const
function currentTally() =
List.map((p) => (p.name, p.voteCount), state.proposals)
MyList.map((p) => (p.name, p.voteCount), state.proposals)