Skip to content

Commit 4786ccc

Browse files
committed
Implement and test every (forall) list function
1 parent eff4dbb commit 4786ccc

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/structuredPoly.ml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,37 @@ let fold_left =
498498
] );
499499
]))))
500500

501+
let fold_left_poly_bool =
502+
typed_term
503+
(UnivApplication
504+
(UnivApplication (fold_left.term, base_type (UnivTypeVar 0)), bool_type))
505+
506+
let every =
507+
typed_term
508+
(UnivQuantifier
509+
(Abstraction
510+
[
511+
( poly_to_bool_op,
512+
Abstraction
513+
[
514+
( polymoprhic_list_type.full,
515+
trinary_apply fold_left_poly_bool.term
516+
(Abstraction
517+
[
518+
( bool_type,
519+
Abstraction
520+
[
521+
( base_type (UnivTypeVar 0),
522+
binary_apply and_lambda.term (Variable 1)
523+
(Application (Variable 3, Variable 0)) );
524+
] );
525+
])
526+
true_lambda.term (Variable 0) );
527+
] );
528+
]))
529+
501530
(* List functions we should implement:
502-
* fold_left/fold_right
503-
* forall/exists
531+
* exists
504532
* equal
505533
* find (return element and/or index)
506534
* flatten

test/structuredLambdaPoly.ml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ let fold_int_int =
202202
typed_term
203203
(UnivApplication (UnivApplication (fold_left.term, ind_integer), ind_integer))
204204

205+
let every_int = typed_term (UnivApplication (every.term, ind_integer))
206+
205207
let true_predicate =
206208
typed_term
207209
(UnivQuantifier
@@ -218,6 +220,22 @@ let true_predicate_int =
218220
let false_predicate_int =
219221
typed_term (UnivApplication (false_predicate.term, ind_integer))
220222

223+
(* Represents `forall X. (X -> Bool) -> X List -> Bool`, the type for every and exists *)
224+
let list_predicate_type =
225+
map_type
226+
(fun list ->
227+
[
228+
UnivQuantification
229+
[
230+
Intersection
231+
[
232+
( [ Intersection [ ([ UnivTypeVar 0 ], bool_type.union) ] ],
233+
[ Intersection [ (list, bool_type.union) ] ] );
234+
];
235+
];
236+
])
237+
polymoprhic_list_type.full
238+
221239
let () =
222240
test "Polymoprhic identity function type"
223241
(is_equivalent_type polymoprhic_identity.stype expected_poly_identity_type)
@@ -574,3 +592,31 @@ let () =
574592
(trinary_apply fold_int_int.term add.term zero.term
575593
incrementing_integer_list.term)
576594
(num_term 10))
595+
596+
let () =
597+
test "Every function has the right type"
598+
(is_subtype every.stype list_predicate_type)
599+
600+
let () =
601+
test "Every function called on empty list returns true"
602+
(evaluates_to
603+
(binary_apply every_int.term is_even.term empty_list.term)
604+
true_lambda.term)
605+
606+
let () =
607+
test "Every is_even on simple list is false"
608+
(evaluates_to
609+
(binary_apply every_int.term is_even.term simple_integer_list.term)
610+
false_lambda.term)
611+
612+
let () =
613+
test "Every is_positive on incrementing integer list is true"
614+
(evaluates_to
615+
(binary_apply every_int.term
616+
(Abstraction
617+
[
618+
(ind_positive_number, true_lambda.term);
619+
(ind_non_positive_number, false_lambda.term);
620+
])
621+
incrementing_integer_list.term)
622+
true_lambda.term)

0 commit comments

Comments
 (0)