@@ -665,6 +665,28 @@ let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
665
665
let string_of_expression = ref (fun _ -> " " )
666
666
let debug = false
667
667
668
+ (* *
669
+ [simplify_and e1 e2] attempts to simplify the boolean AND expression [e1 && e2].
670
+ Returns [Some simplified] if simplification is possible, [None] otherwise.
671
+
672
+ Basic simplification rules:
673
+ - [false && e] -> [false]
674
+ - [true && e] -> [e]
675
+ - [e && false] -> [false]
676
+ - [e && true] -> [e]
677
+ - [(a && b) && e] -> If either [a && e] or [b && e] can be simplified,
678
+ creates new AND expression with simplified parts: [(a' && b')]
679
+ - [(a || b) && e] -> If either [a && e] or [b && e] can be simplified,
680
+ creates new OR expression with simplified parts: [(a' || b')]
681
+
682
+ Type check optimizations:
683
+ - [(typeof x === "boolean") && (x === true/false)] -> [x === true/false]
684
+ - [(typeof x === "string" || Array.isArray(x)) && (x === boolean/null/undefined)] -> [false]
685
+
686
+ Note: The function preserves the semantics of the original expression while
687
+ attempting to reduce it to a simpler form. If no simplification is possible,
688
+ returns [None].
689
+ *)
668
690
let rec simplify_and (e1 : t ) (e2 : t ) : t option =
669
691
if debug then
670
692
Printf. eprintf " simplify_and %s %s\n " (! string_of_expression e1)
@@ -771,6 +793,16 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
771
793
Some false_
772
794
| _ -> None
773
795
796
+ (* *
797
+ [simplify_or e1 e2] attempts to simplify the boolean OR expression [e1 || e2].
798
+ Returns [Some simplified] if simplification is possible, [None] otherwise.
799
+
800
+ Basic simplification rules:
801
+ - [true || e] -> [true]
802
+ - [e || true] -> [true]
803
+ - [false || e] -> [e]
804
+ - [e || false] -> [e]
805
+ *)
774
806
and simplify_or (e1 : t ) (e2 : t ) : t option =
775
807
if debug then
776
808
Printf. eprintf " simplify_or %s %s\n " (! string_of_expression e1)
@@ -817,10 +849,6 @@ let or_ ?comment (e1 : t) (e2 : t) =
817
849
| Some e -> e
818
850
| None -> {expression_desc = Bin (Or , e1, e2); comment})
819
851
820
- (* return a value of type boolean *)
821
- (* TODO:
822
- when comparison with Int
823
- it is right that !(x > 3 ) -> x <= 3 *)
824
852
let not (e : t ) : t =
825
853
match e.expression_desc with
826
854
| Number (Int {i; _} ) -> bool (i = 0l )
0 commit comments