Skip to content

Commit

Permalink
Refactor extractSubstitution
Browse files Browse the repository at this point in the history
  • Loading branch information
geo2a committed Oct 23, 2024
1 parent f9f0d51 commit c974346
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions booster/library/Booster/Syntax/Json/Internalise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ import Data.ByteString.Char8 (ByteString, isPrefixOf)
import Data.ByteString.Char8 qualified as BS
import Data.Char (isLower)
import Data.Coerce (coerce)
import Data.Either (partitionEithers)
import Data.Foldable ()
import Data.Generics (extQ)
import Data.Graph (SCC (..), stronglyConnComp)
import Data.List (foldl', foldl1', nub)
import Data.List (foldl1', nub)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Set (Set)
Expand Down Expand Up @@ -510,21 +511,19 @@ mbSubstitution = \case
extractSubstitution ::
[Internal.Predicate] -> (Map Internal.Variable Internal.Term, Set Internal.Predicate)
extractSubstitution ps =
let (potentialSubstituion, otherPreds) = partitionSubstitutionPreds . map mbSubstitution . coerce $ ps
(newSubstitution, leftoverPreds) = mkSubstitution potentialSubstituion
in (newSubstitution, Set.fromList $ leftoverPreds <> map unsafeFromBoolPred otherPreds)
let (potentialSubstitution, otherPreds) = partitionSubstitutionPreds . map mbSubstitution . coerce $ ps
(newSubstitution, leftoverPreds) = mkSubstitution potentialSubstitution
in (newSubstitution, Set.fromList $ leftoverPreds <> otherPreds)
where
partitionSubstitutionPreds = foldl' accumulate ([], [])
partitionSubstitutionPreds = partitionEithers . map unpackBoolPred
where
accumulate (accSubst, accOther) = \case
pSubst@SubstitutionPred{} -> (pSubst : accSubst, accOther)
pOther -> (accSubst, pOther : accOther)

-- this conversion is safe withing this function since we pass Internal.Predicate as input
unsafeFromBoolPred :: InternalisedPredicate -> Internal.Predicate
unsafeFromBoolPred = \case
BoolPred p -> coerce p
_ -> error "extractSubsitution.unsafeFromBoolPred: impossible case"
unpackBoolPred :: InternalisedPredicate -> Either InternalisedPredicate Internal.Predicate
unpackBoolPred = \case
pSubst@SubstitutionPred{} -> Left pSubst
BoolPred p -> Right p
other ->
-- this case is impossible the input is a valid Internal.Predicate
error $ "extractSubstitution: unsupported predicate " <> show other

internalisePred ::
Flag "alias" ->
Expand Down

0 comments on commit c974346

Please sign in to comment.