From 7e32ef57c21f572731bc9e99c02288b0a6ff1596 Mon Sep 17 00:00:00 2001 From: radrow Date: Wed, 26 Aug 2020 11:56:18 +0200 Subject: [PATCH 1/3] Added `contains` functions in List and Option. Fixed one type error catch --- docs/sophia_stdlib.md | 14 ++++++++++++++ priv/stdlib/List.aes | 8 ++++++-- priv/stdlib/Option.aes | 4 ++++ test/aeso_compiler_tests.erl | 13 ++++++++----- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/docs/sophia_stdlib.md b/docs/sophia_stdlib.md index ab31beb..6554810 100644 --- a/docs/sophia_stdlib.md +++ b/docs/sophia_stdlib.md @@ -700,6 +700,13 @@ List.last(l : list('a)) : option('a) Returns `Some` of the last element of a list or `None` if the list is empty. +### contains +``` +List.contains(e : 'a, l : list('a)) : bool +``` +Checks if list `l` contains element `e`. Equivalent to `List.find(x => x == e, l) != None`. + + ### find ``` List.find(p : 'a => bool, l : list('a)) : option('a) @@ -1040,6 +1047,13 @@ Option.force(o : option('a)) : 'a Forcefully escapes `option` wrapping assuming it is `Some`. Throws error on `None`. +### contains +``` +Option.contains(e : 'a, o : option('a)) : bool +``` +Returns `true` if and only if `o` contains element equal to `e`. Equivalent to `Option.match(false, x => x == e, o)`. + + ### on_elem ``` Option.on_elem(o : option('a), f : 'a => unit) : unit diff --git a/priv/stdlib/List.aes b/priv/stdlib/List.aes index 3405035..3a56856 100644 --- a/priv/stdlib/List.aes +++ b/priv/stdlib/List.aes @@ -15,10 +15,14 @@ namespace List = _::t => Some(t) function last(l : list('a)) : option('a) = switch(l) - [] => None - [x] => Some(x) + [] => None + [x] => Some(x) _::t => last(t) + function contains(e : 'a, l : list('a)) = switch(l) + [] => false + h::t => h == e || contains(e, t) + /** Finds first element of `l` fulfilling predicate `p` as `Some` or `None` * if no such element exists. */ diff --git a/priv/stdlib/Option.aes b/priv/stdlib/Option.aes index 4895e18..5e6622f 100644 --- a/priv/stdlib/Option.aes +++ b/priv/stdlib/Option.aes @@ -26,6 +26,10 @@ namespace Option = None => abort("Forced None value") Some(x) => x + function contains(e : 'a, o : option('a)) = switch(o) + None => false + Some(x) => x == e + function on_elem(o : option('a), f : 'a => unit) : unit = match((), f, o) function map(f : 'a => 'b, o : option('a)) : option('b) = switch(o) diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index e8f5931..1254b2c 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -39,7 +39,7 @@ simple_compile_test_() -> error(ErrBin) end end} || ContractName <- compilable_contracts(), Backend <- [aevm, fate], - not lists:member(ContractName, not_yet_compilable(Backend))] ++ + not lists:member(ContractName, not_compilable_on(Backend))] ++ [ {"Test file not found error", fun() -> {error, Errors} = aeso_compiler:file("does_not_exist.aes"), @@ -168,8 +168,11 @@ compilable_contracts() -> "lhs_matching" ]. -not_yet_compilable(fate) -> []; -not_yet_compilable(aevm) -> []. +not_compilable_on(fate) -> []; +not_compilable_on(aevm) -> + ["stdlib_include", + "manual_stdlib_include" + ]. %% Contracts that should produce type errors @@ -316,8 +319,8 @@ failing_contracts() -> " x : int\n" "against the expected type\n" " string">>, - <>, + <>, <>, < Date: Wed, 26 Aug 2020 12:10:24 +0200 Subject: [PATCH 2/3] minor optimization --- priv/stdlib/Option.aes | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/priv/stdlib/Option.aes b/priv/stdlib/Option.aes index 5e6622f..e364363 100644 --- a/priv/stdlib/Option.aes +++ b/priv/stdlib/Option.aes @@ -26,9 +26,7 @@ namespace Option = None => abort("Forced None value") Some(x) => x - function contains(e : 'a, o : option('a)) = switch(o) - None => false - Some(x) => x == e + function contains(e : 'a, o : option('a)) = o == Some(e) function on_elem(o : option('a), f : 'a => unit) : unit = match((), f, o) From 3d73e52d48bb0b02fa00ae84e2736a9f0f67e6ad Mon Sep 17 00:00:00 2001 From: radrow Date: Wed, 26 Aug 2020 15:56:21 +0200 Subject: [PATCH 3/3] Fix tests --- test/aeso_compiler_tests.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index 1254b2c..208839d 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -319,8 +319,8 @@ failing_contracts() -> " x : int\n" "against the expected type\n" " string">>, - <>, + <>, <>, <