Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions reduct/boolean-reduct/cut-unnecessary-and.metta
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@
;; whether the transformation function is applied or not.
;; Example: input: POA ==> (AND (OR (AND B D) (NOT E))), parent ==> (OR C (AND (OR (AND B D) (NOT E))))
;; output: ((OR C (AND B D) (NOT E)) () True)
(: andCut (-> Expression Expression (Expression Expression Bool)))
; (: andCut (-> Expression Expression (Expression Expression Bool)))
(= (andCut $parent $current)
(let*
(
; (() (println! (===Inside AndCut===)))
; (() (println! (Current $current : Parent : $parent)))
; ($log (println! (===Inside AndCut===)))
; ($log' (println! (Current $current : Parent : $parent)))
($parentSec (collapse (removeEmptyAND (superpose $parent))))
($currentSec (if (=== $parent $current) $parentSec (collapse (removeEmptyAND (superpose $current)))))
(($guardSet $child) (getLiteralChildren $currentSec))
($tail (if-decons-expr-custom $child $h $t (cdr-atom $child) "No child"))
(($h $t) (if (== (get-metatype $child) Expression) (decons-atom $child) (() ())))
($then (cdr-atom $child))
($tail (if-decons-expr-custom $child $h $t $then "No child"))
; ($log (println! ("Tail: " $tail)))
)
(if (and (== $tail ()) (== $guardSet ())) ;; Has no guardSet and has single child
(let*
(
; (() (println! (===Inside AndCut===)))
; (() (println! (Current $current : Parent : $parent)))
($childFirst (if-decons-expr-custom $child $h $t $h (empty)))
; ($log (println! (===Inside AndCut===)))
; ($log' (println! (Current $current : Parent : $parent)))
($childFirst (if-decons-expr-custom $child $h $t $h ()))
(($grandLiterals $grandChildren) (getLiteralChildren $childFirst))
($allLiterals (collapse (getGuardSet (superpose $grandChildren))))
($common (intersections $allLiterals))
Expand All @@ -40,15 +43,15 @@
(addChildren $updatedParent $grandChildren)))
($updatedParent'' (addLiterals $updatedParent' $grandLiterals))

; (() (println! (=== Inside AndCut Log ===)))
; (() (println! (All Literals: $allLiterals)))
; (() (println! (GrandChildren: $grandChildren : GrandLiterals: $grandLiterals)))
; (() (println! (UpdatedParent: $updatedParent : UpdatedParent': $updatedParent' )))
; (() (println! (UpdatedParent'': $updatedParent'' )))
; (() (println! (ChildFirst: $childFirst)))
; (() (println! (Child: $child)))
; (() (println! (AllLiterals: $allLiterals)))
; (() (println! (Common: $common)))
; ($l (println! (===Inside AndCut Log===)))
; ($l (println! (All Literals: $allLiterals)))
; ($l (println! (GrandChildren: $grandChildren : GrandLiterals: $grandLiterals)))
; ($l (println! (UpdatedParent: $updatedParent : UpdatedParent': $updatedParent' )))
; ($l (println! (UpdatedParent'': $updatedParent'' )))
; ($l (println! (ChildFirst: $childFirst)))
; ($l (println! (Child: $child)))
; ($l (println! (AllLiterals: $allLiterals)))
; ($l (println! (Common: $common)))

)
(if (== $common ())
Expand All @@ -62,7 +65,7 @@
)

;; Function to find intersections between all literals in a nested tuple
(: intersections (-> Expression Expression))
; (: intersections (-> Expression Expression))
(= (intersections $nestedTuple)
(case $nestedTuple
((() $nestedTuple)
Expand All @@ -78,7 +81,7 @@

;; A helper function used to apply andCut in the until function.
;; This function keeps the state of the until function updated for a single function application.
(: applyAndCut (-> (Expression Bool (Expression Expression)) (Expression Bool (Expression Expression))))
; (: applyAndCut (-> (Expression Bool (Expression Expression)) (Expression Bool (Expression Expression))))
(= (applyAndCut ($parent $bool ($child $remainingChildren)))
(let*
(
Expand Down
10 changes: 5 additions & 5 deletions reduct/boolean-reduct/cut-unnecessary-or.metta
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
;; are not met, it returns the state unchanged.
;; Example: input: POA ==> (OR (AND (NOT D))), parent => (AND B (OR (AND (NOT D))))
;; output: ((AND B (NOT D)) () True)
(: orCut (-> Expression Expression (Expression Expression Bool)))
; (: orCut (-> Expression Expression (Expression Expression Bool)))
(= (orCut $parent $current)
(let*
(
;; (() (println! (=== Inside OrCut ===)))
;; (() (println! (Parameters ==> Parent: $parent Current: $current)))
($log (println! (==== Inside OrCut ====)))
; ($log' (println! (Parameters ==> Parent: $parent Current: $current)))
)
(case $current
(
Expand All @@ -31,7 +31,7 @@
;; A function to return the literals and children of any given node.
;; It is used for cases where we need all the subExpressions of AND & OR nodes.
;; Expressions like (NOT A) doesn't have any subExpression and are treated as a literal.
(: getSubExpression (-> Expression Expression))
; (: getSubExpression (-> Expression Expression))
(= (getSubExpression $exp)
(case $exp
(
Expand All @@ -52,7 +52,7 @@

;; A helper function used to apply orCut function in the until function.
;; This function keeps the state of the until function updated for a single function application.
(: applyOrCut (-> (Expression Bool (Expression Expression)) (Expression Bool (Expression Expression))))
; (: applyOrCut (-> (Expression Bool (Expression Expression)) (Expression Bool (Expression Expression))))
(= (applyOrCut ($parent $bool ($child $remainingChildren)))
(let*
(
Expand Down
19 changes: 9 additions & 10 deletions reduct/boolean-reduct/delete-inconsistent-handle.metta
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
)

;; This function is used to handle nested current expressions
(: applyDeleteIncons (-> Expression Expression Expression (Expression Expression Bool)))
; (: applyDeleteIncons (-> Expression Expression Expression (Expression Expression Bool)))
(= (applyDeleteIncons $parent $current $dominantSet)
(let*
(
Expand All @@ -71,12 +71,11 @@
)
(if (== $children ())
($newParent $newCurrent $applied)
; (deleteInconsistentHandle $parent $current $dominantSet)
(let*
(
; (() (println! (Applying DeleteIncons on children...)))
; ($log (println! (Applying DeleteIncons on children...)))
(($child $rest) (decons-atom $children))
; (() (println! (Child: $child Rest: $rest)))
; ($log' (println! (Child: $child Rest: $rest)))
($grandChild (getChildrenExp $child))
(($UpdatedChildren $unwt $appliedOnChild)
(if (== $grandChild ())
Expand All @@ -86,19 +85,19 @@
)
)
)
; (() (println! (UpdatedChildren: $UpdatedChildren)))
; ($log'' (println! (UpdatedChildren: $UpdatedChildren)))
(($updatedCurrent $updatedchild $appliedOnCurrent) (if (=== $child $UpdatedChildren) (deleteInconsistentHandle $newCurrent $child $dominantSet) ((findAndReplace $child $UpdatedChildren $newCurrent) $UpdatedChildren $appliedOnChild)))
($updatedParent (findAndReplace $newCurrent $updatedCurrent $newParent))
; (()(println! (Updated Parent: $UpdatedParent : UpdatedCurrent: $updatedCurrent Bool: (any $appliedOnCurrent $appliedOnChild $applied) )))
; ($log''' (println! (Updated Parent: $updatedParent : UpdatedCurrent: $updatedCurrent Bool: (any $appliedOnCurrent $appliedOnChild $applied) )))
)
; (applyDeleteIncons $UpdatedParent $updatedCurrent $dominantSet (any ($appliedOnCurrent $appliedOnChild $applied)) $rest)
(applyDeleteIncons $UpdatedParent $updatedCurrent $dominantSet $appliedOnCurrent $rest)
; (applyDeleteIncons $updatedParent $updatedCurrent $dominantSet (any ($appliedOnCurrent $appliedOnChild $applied)) $rest)
(applyDeleteIncons $updatedParent $updatedCurrent $dominantSet $appliedOnCurrent $rest)
)
)
)
)

(: applyDeleteIncons (-> Expression Expression Expression Bool Expression (Expression Expression Bool)))
; (: applyDeleteIncons (-> Expression Expression Expression Bool Expression (Expression Expression Bool)))
(= (applyDeleteIncons $parent $current $dominantSet $applied $children)
(if (== $children ())
($parent $current $applied)
Expand All @@ -117,7 +116,7 @@
)
)

(: applyDeleteIncons (-> Expression Expression Expression Expression Bool Bool (Expression Expression Bool)))
; (: applyDeleteIncons (-> Expression Expression Expression Expression Bool Bool (Expression Expression Bool)))
(= (applyDeleteIncons $parent $current $dominantSet $children $appliedToParent $checkingChild)
(if (== $children ())
(deleteInconsistentHandle $parent $current $dominantSet)
Expand Down
41 changes: 26 additions & 15 deletions reduct/boolean-reduct/rte-helpers.metta
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,43 @@
;; Only AND nodes have guard sets.
;; Example: (AND (NOT A) (NOT B) A) -> ( (NOT A) (NOT B) A)
(= (getGuardSet $exp)
(if-decons-expr-custom $exp $h $t
(if (== $h OR)
()
(getLiterals $exp))
$exp))
(if (== (get-metatype $exp) Expression)
(let ($h $t) (decons-atom $exp)
(eval (if-decons-expr-custom $exp $h $t
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So check this one out.

(if (== $h OR)
()
(getLiterals $exp))
$exp))
)
$exp)
)


;; A function similar to the getGuardSet function but returns a
;; non deterministic list instead of a collapsed atom.
;; Example: (AND (NOT A) (NOT B) A) -> [(NOT A), (NOT B), A]
(= (getGuardSetND $exp)
(if-decons-expr-custom $exp $h $t
(if (== $h OR)
(empty)
(getLiteralsND $exp))
$exp))
(if (or (not (== $exp ())) (== (get-metatype $exp) Expression))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here. Check it out if it works with if-decons-expr-custom only...

(let*
(
(($h $t) (decons-atom $exp))
($then (if (== $h OR) (empty) (getLiteralsND $exp)))
($result (if-decons-expr-custom $exp $h $t $then $exp))
)
$result
)
$exp
)
)

(= (addLiterals $exp $elements)
(let*
(
($op (car-atom $exp))
($guardSet (getLiterals $exp))
($newGuardSet (unionAtom $guardSet $elements))
($newGuardSet (union-atom $guardSet $elements))
($newGuardSet' (collapse (unique (superpose $newGuardSet))))
($updatedSubExpression (unionAtom $newGuardSet' (getChildrenExp $exp)))

($updatedSubExpression (union-atom $newGuardSet' (getChildrenExp $exp)))
($newExp (cons-atom $op $updatedSubExpression))
)
$newExp
Expand Down Expand Up @@ -75,8 +86,8 @@
(
($op (car-atom $exp))
($oldChildren (getChildrenExp $exp))
($newChildren (concatTuple $oldChildren $children))
($updatedSubExpression (concatTuple (getLiterals $exp) $newChildren))
($newChildren (eval (concatTuple $oldChildren $children)))
($updatedSubExpression (eval (concatTuple (getLiterals $exp) $newChildren)))
($newExp (cons-atom $op $updatedSubExpression))

; (() (println! (OldChildren: $oldChildren)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
! (register-module! ../../../../metta-moses)

! (import! &self metta-moses:reduct/boolean-reduct:rte-helpers)
! (import! &self metta-moses:utilities:general-helpers)
! (import! &self metta-moses:reduct/boolean-reduct:cut-unnecessary-and)

!(import! &self ../rte-helpers)
!(import! &self ../../../utilities/general-helpers)
!(import! &self ../cut-unnecessary-and)


;; Test 01
Expand All @@ -18,7 +15,7 @@
((OR (AND (OR (AND C D) (AND C F))) A) (AND (OR (AND C D) (AND C F))) False)
)

;; Test 03
; ;; Test 03
!(assertEqual
(andCut (OR A (AND (OR (AND B C) (AND E F) (AND G H))) F) (AND (OR (AND B C) (AND E F) (AND G H))))
((OR A F (AND B C) (AND E F) (AND G H)) () True)
Expand Down
46 changes: 46 additions & 0 deletions reduct/boolean-reduct/tests/cut-unnecessary-or-test.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
!(import! &self ../../../utilities/general-helpers)
!(import! &self ../rte-helpers)
!(import! &self ../cut-unnecessary-or)

;Test 01: OR expressions with one child - orCut will be applied
!(assertEqual
(orCut (AND B (OR (AND (NOT D)))) (OR (AND (NOT D))))
((AND B (NOT D)) () True)
)

!(assertEqual
(orCut (AND C (OR A)) (OR A))
((AND C A) () True)
)

!(assertEqual
(orCut (AND H (OR (AND A B (AND A B))) (NOT E)) (OR (AND A B (AND A B))))
((AND H (NOT E) A B (AND A B)) () True)
)

!(assertEqual
(orCut (AND (OR (AND A (NOT A) (OR A B )))) (OR (AND A (NOT A) (OR A B ))))
((AND A (NOT A) (OR A B)) () True)
)

!(assertEqual
(orCut (AND (OR (AND A (OR A (NOT B))))) (OR (AND A (OR A (NOT B)))))
((AND A (OR A (NOT B))) () True)
)

;Test 02:OR expression with more than one children - orCut won't be applied
!(assertEqual
(orCut (AND (OR A B)) (OR A B))
((AND (OR A B)) (OR A B) False)
)
;Test 03: AND and NOT expressions - orCut won't be applied
!(assertEqual
(orCut (OR (AND (AND A (OR A (NOT B))))) (AND (AND A (OR A (NOT B)))))
((OR (AND (AND A (OR A (NOT B))))) (AND (AND A (OR A (NOT B)))) False)
)
!(assertEqual
(orCut (AND (NOT (AND A (OR A (NOT B))))) (NOT (AND A (OR A (NOT B)))))
((AND (NOT (AND A (OR A (NOT B))))) (NOT (AND A (OR A (NOT B)))) False)
)

!(assertEqual (orCut (AND A B (OR C D)) ()) ((AND A B (OR C D)) () False))
47 changes: 0 additions & 47 deletions reduct/boolean-reduct/tests/cut-unnecessary-or-testold.metta

This file was deleted.

Loading