test closure and higher order function

This commit is contained in:
Galfour 2019-04-18 21:29:35 +00:00
parent ef4a5030fa
commit 0521c3d3b7
8 changed files with 71 additions and 9 deletions

View File

@ -3,3 +3,9 @@ function foo (const i : int) : int is
block { skip } with i + j ; block { skip } with i + j ;
block { skip } with bar (i) block { skip } with bar (i)
function toto (const i : int) : int is
function tata (const j : int) : int is
block { skip } with i + j ;
function titi (const j : int) : int is
block { skip } with i + j ;
block { skip } with tata(i) + titi(i)

View File

@ -0,0 +1,6 @@
function foobar (const i : int) : int is
function foo (const i : int) : int is
block { skip } with i ;
function bar (const f : int -> int) : int is
block { skip } with f ( i ) ;
block { skip } with bar (foo) ;

View File

@ -193,6 +193,7 @@ and type_expr =
| TSum of (variant reg, vbar) nsepseq reg | TSum of (variant reg, vbar) nsepseq reg
| TRecord of record_type | TRecord of record_type
| TApp of (type_name * type_tuple) reg | TApp of (type_name * type_tuple) reg
| TFun of (type_expr * arrow * type_expr) reg
| TPar of type_expr par reg | TPar of type_expr par reg
| TAlias of variable | TAlias of variable
@ -672,6 +673,7 @@ let type_expr_to_region = function
| TSum {region; _} | TSum {region; _}
| TRecord {region; _} | TRecord {region; _}
| TApp {region; _} | TApp {region; _}
| TFun {region; _}
| TPar {region; _} | TPar {region; _}
| TAlias {region; _} -> region | TAlias {region; _} -> region

View File

@ -177,6 +177,7 @@ and type_expr =
| TSum of (variant reg, vbar) nsepseq reg | TSum of (variant reg, vbar) nsepseq reg
| TRecord of record_type | TRecord of record_type
| TApp of (type_name * type_tuple) reg | TApp of (type_name * type_tuple) reg
| TFun of (type_expr * arrow * type_expr) reg
| TPar of type_expr par reg | TPar of type_expr par reg
| TAlias of variable | TAlias of variable

View File

@ -152,10 +152,17 @@ type_expr:
| record_type { TRecord $1 } | record_type { TRecord $1 }
cartesian: cartesian:
nsepseq(core_type,TIMES) { nsepseq(function_type,TIMES) {
let region = nsepseq_to_region type_expr_to_region $1 let region = nsepseq_to_region type_expr_to_region $1
in {region; value=$1}} in {region; value=$1}}
function_type:
core_type { $1 }
| core_type ARROW function_type {
let region = cover (type_expr_to_region $1) (type_expr_to_region $3)
in TFun {region; value = ($1, $2, $3)}
}
core_type: core_type:
type_name { type_name {
TAlias $1 TAlias $1
@ -346,17 +353,17 @@ entry_param_decl:
in EntryStore {region; value}} in EntryStore {region; value}}
param_type: param_type:
nsepseq(core_param_type,TIMES) { nsepseq(function_type,TIMES) {
let region = nsepseq_to_region type_expr_to_region $1 let region = nsepseq_to_region type_expr_to_region $1
in TProd {region; value=$1}} in TProd {region; value=$1}}
core_param_type: /* core_param_type: */
type_name { /* type_name { */
TAlias $1 /* TAlias $1 */
} /* } */
| type_name type_tuple { /* | type_name type_tuple { */
let region = cover $1.region $2.region /* let region = cover $1.region $2.region */
in TApp {region; value = $1,$2}} /* in TApp {region; value = $1,$2}} */
block: block:
Begin series(statement,End) { Begin series(statement,End) {

View File

@ -88,6 +88,7 @@ and print_type_expr = function
| TSum sum_type -> print_sum_type sum_type | TSum sum_type -> print_sum_type sum_type
| TRecord record_type -> print_record_type record_type | TRecord record_type -> print_record_type record_type
| TApp type_app -> print_type_app type_app | TApp type_app -> print_type_app type_app
| TFun type_fun -> print_type_fun type_fun
| TPar par_type -> print_par_type par_type | TPar par_type -> print_par_type par_type
| TAlias type_alias -> print_var type_alias | TAlias type_alias -> print_var type_alias
@ -111,6 +112,12 @@ and print_type_app {value; _} =
print_var type_name; print_var type_name;
print_type_tuple type_tuple print_type_tuple type_tuple
and print_type_fun {value; _} =
let type_expr_a, arrow, type_expr_b = value in
print_type_expr type_expr_a;
print_token arrow "->";
print_type_expr type_expr_b
and print_par_type {value; _} = and print_par_type {value; _} =
let {lpar; inside; rpar} = value in let {lpar; inside; rpar} = value in
print_token lpar "("; print_token lpar "(";

View File

@ -25,6 +25,12 @@ let rec simpl_type_expression (t:Raw.type_expr) : type_expression result =
| None -> | None ->
ok @@ T_variable v.value ok @@ T_variable v.value
) )
| TFun x -> (
let%bind (a , b) =
let (a , _ , b) = x.value in
bind_map_pair simpl_type_expression (a , b) in
ok @@ T_function (a , b)
)
| TApp x -> | TApp x ->
let (name, tuple) = x.value in let (name, tuple) = x.value in
let lst = npseq_to_list tuple.value.inside in let lst = npseq_to_list tuple.value.inside in

View File

@ -54,6 +54,32 @@ let closure () : unit result =
bind_list bind_list
@@ List.map aux @@ List.map aux
@@ [0 ; 2 ; 42 ; 163 ; -1] in @@ [0 ; 2 ; 42 ; 163 ; -1] in
let%bind _toto = trace (simple_error "toto") @@
let aux n =
let open AST_Typed.Combinators in
let input = e_a_int n in
let%bind result = easy_run_typed "toto" program input in
let expected = e_a_int ( 4 * n ) in
AST_Typed.assert_value_eq (expected, result)
in
bind_list
@@ List.map aux
@@ [0 ; 2 ; 42 ; 163 ; -1] in
ok ()
let higher_order () : unit result =
let%bind program = type_file "./contracts/high-order.ligo" in
let%bind _foo = trace (simple_error "test foo") @@
let aux n =
let open AST_Typed.Combinators in
let input = e_a_int n in
let%bind result = easy_run_typed "foobar" program input in
let expected = e_a_int ( n ) in
AST_Typed.assert_value_eq (expected, result)
in
bind_list
@@ List.map aux
@@ [0 ; 2 ; 42 ; 163 ; -1] in
ok () ok ()
let shared_function () : unit result = let shared_function () : unit result =
@ -542,4 +568,5 @@ let main = "Integration (End to End)", [
test "quote declarations" quote_declarations ; test "quote declarations" quote_declarations ;
test "#include directives" include_ ; test "#include directives" include_ ;
test "counter contract" counter_contract ; test "counter contract" counter_contract ;
test "higher order" higher_order ;
] ]