phonetic-languages-constraints-array 0.2.0.0 → 0.3.0.0
raw patch · 4 files changed
+101/−8 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Phladiprelio.Constraints: filterSignDistanceIJ :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)
+ Phladiprelio.Constraints: filterSignDistanceIJK3 :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)
+ Phladiprelio.Constraints: filterUnsignDistanceIJ :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)
+ Phladiprelio.Constraints: filterUnsignDistanceIJK3 :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)
+ Phladiprelio.Constraints: unsafeSignDistanceIJ :: Int -> Int -> Int -> Array Int Int -> Bool
+ Phladiprelio.Constraints: unsafeUnsignDistanceIJ :: Int -> Int -> Int -> Array Int Int -> Bool
+ Phladiprelio.ConstraintsEncoded: H :: a -> a -> a -> EncodedContraints a b
+ Phladiprelio.ConstraintsEncoded: R :: a -> a -> a -> EncodedContraints a b
+ Phladiprelio.ConstraintsEncoded: V :: a -> a -> EncodedContraints a b
+ Phladiprelio.ConstraintsEncoded: W :: a -> a -> EncodedContraints a b
+ Phladiprelio.ConstraintsEncoded: isH :: EncodedCnstrs -> Bool
+ Phladiprelio.ConstraintsEncoded: isR :: EncodedCnstrs -> Bool
+ Phladiprelio.ConstraintsEncoded: isV :: EncodedCnstrs -> Bool
+ Phladiprelio.ConstraintsEncoded: isW :: EncodedCnstrs -> Bool
Files
- CHANGELOG.md +5/−0
- Phladiprelio/Constraints.hs +55/−5
- Phladiprelio/ConstraintsEncoded.hs +40/−2
- phonetic-languages-constraints-array.cabal +1/−1
CHANGELOG.md view
@@ -17,3 +17,8 @@ * Second version. Switched to NoImplicitPrelude extension. Changed the names of the modules. Updated the dependencies boundaries. +## 0.3.0.0 -- 2023-05-15++* Third version. Added new types of constraints based on the signed and unsigned distance(s)+between two or three elements (encoded with the capital letters V, W, H, R).+
Phladiprelio/Constraints.hs view
@@ -29,20 +29,27 @@ -- ** With fixed points , fixedPointsG , fixedPointsS+ -- * Distances between elements+ , unsafeSignDistanceIJ+ , unsafeUnsignDistanceIJ+ , filterSignDistanceIJ+ , filterUnsignDistanceIJ+ , filterSignDistanceIJK3+ , filterUnsignDistanceIJK3 ) where -import GHC.Base-import GHC.Num ((-))+import GHC.Base hiding (foldr)+import GHC.Num ((-),(+)) import Data.Maybe (fromJust) import Data.SubG (InsertLeft(..),filterG) import GHC.Arr-import Data.Foldable (foldl', all)+import Data.Foldable (all, foldr) -- | Being given the data satisfying the constraints in the module header checks whether in the 'Array' the first argument stands before the second one. unsafeOrderIJ :: Int -> Int -> Array Int Int -> Bool-unsafeOrderIJ i j = (\(_,_,r) -> if r == 0 then True else False) . foldl' helpG (i,j,0)+unsafeOrderIJ i j = (\(_,_,r) -> if r == 0 then True else False) . foldr helpG (j,i,0) -helpG (t,u,n) z+helpG z (t,u,n) | z == t = (t,u,1) | z == u = case n of@@ -51,9 +58,52 @@ | otherwise = (t,u,n) {-# INLINE helpG #-} +unsafeSignDistanceIJ + :: Int + -> Int + -> Int -- ^ Can be of both signs, but not equal to 0. The positive value gives 'True' for the first argument being find earlier in the 'Array' than the second and the distance between their positions are equal to 'abs' @d@ (this argument). The negative value gives 'True' for the second argument being earlier in the 'Array' than the first one and the distance between their positions are equal to 'abs' @d@ (this argument).+ -> Array Int Int + -> Bool+unsafeSignDistanceIJ i j d = (\(_,_,r) -> if r > 100 then (100 - r) == d else r == d) . foldr helpG2 (j, i, -1)++helpG2 z (t, u, n)+ | n < 0 = if (z /= t && z /= u) then (t, u, n) else (t, u, if z == t then 1 else 101)+ | z /= u && z /= t && t >= 0 = (t, u, n + 1)+ | otherwise = (-1, u, n)++unsafeUnsignDistanceIJ + :: Int + -> Int + -> Int -- ^ Only for positive values can give 'True', if the distance between the positions of the elements equal to the first two arguments are equal to this argument. Otherwise, 'False'.+ -> Array Int Int + -> Bool+unsafeUnsignDistanceIJ i j d = (\(_,_,r) -> r == d) . foldr helpG3 (j, i, -1)++helpG3 z (t, u, n)+ | n < 0 = if (z /= t && z /= u) then (t, u, n) else (t, u, 1)+ | z /= u && z /= t && t >= 0 = (t, u, n + 1)+ | otherwise = (-1, u, n)++ -- | Being given the data satisfying the constraints in the module header returns the elements that satisfy 'unsafeOrderIJ' as a predicate. filterOrderIJ :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> t (Array Int Int) -> t (Array Int Int) filterOrderIJ i j = filterG (unsafeOrderIJ i j)++-- | +filterSignDistanceIJ :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)+filterSignDistanceIJ i j d = filterG (unsafeSignDistanceIJ i j d)++-- | +filterUnsignDistanceIJ :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)+filterUnsignDistanceIJ i j d = filterG (unsafeUnsignDistanceIJ i j d)++-- | +filterSignDistanceIJK3 :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)+filterSignDistanceIJK3 i j k d1 d2 = filterG (\arr -> unsafeSignDistanceIJ i j d1 arr && unsafeSignDistanceIJ j k d2 arr)++-- | +filterUnsignDistanceIJK3 :: (InsertLeft t (Array Int Int), Monoid (t (Array Int Int))) => Int -> Int -> Int -> Int -> Int -> t (Array Int Int) -> t (Array Int Int)+filterUnsignDistanceIJK3 i j k d1 d2 = filterG (\arr -> unsafeUnsignDistanceIJ i j d1 arr && unsafeUnsignDistanceIJ j k d2 arr) -- | Being given the data satisfying the constraints in the module header reduces the number of further computations in the foldable structure of -- the permutations each one being represented as 'Array' 'Int' 'Int' where the elements are all the numbers in the range [0..n] without duplication if the
Phladiprelio/ConstraintsEncoded.hs view
@@ -34,11 +34,15 @@ , isT , isSA , isSB+ , isV+ , isW+ , isH+ , isR ) where import GHC.Base import GHC.List-import GHC.Num ((-))+import GHC.Num ((-),abs) import Text.Show (show) import Text.Read (readMaybe) import Data.Maybe@@ -46,7 +50,7 @@ import Phladiprelio.Constraints import Data.SubG (InsertLeft(..)) -data EncodedContraints a b = E a | P a b | Q a a a a a | T a a a a | SA a a b | SB a a b | F a a a deriving (Eq, Ord)+data EncodedContraints a b = E a | P a b | Q a a a a a | T a a a a | SA a a b | SB a a b | F a a a | V a a | W a a | H a a a | R a a a deriving (Eq, Ord) -- | Inspired by the: https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Maybe.html -- Is provided here as a more general way to read the 'String' into a 'EncodedCnstrs' than more restricted@@ -96,6 +100,10 @@ (_,_,_,Nothing) -> Nothing ~(Just x1, Just x2, Just x3, Just x4) -> Just (Q undefined x1 x2 x3 x4) 'P' -> if null ts then Just (E 0) else let l = length ts in Just . P n . listArray (0,l-1) . map (\x -> case (fromJust (readMaybe [x]::Maybe Int)) of {0 -> 9; n -> n - 1}) $ ts+ 'W' -> if length ts /= 2 then Just (E 0) else let [k,t] = map (\x -> case (fromJust (readMaybe [x]::Maybe Int)) of {0 -> 9; n -> n - 1}) $ ts in Just . W k $ t+ 'V' -> if length ts /= 2 then Just (E 0) else let [k,t] = map (\x -> case (fromJust (readMaybe [x]::Maybe Int)) of {0 -> 9; n -> n - 1}) $ ts in Just . V k $ t+ 'H' -> if length ts /= 3 then Just (E 0) else let [k,t,w] = map (\x -> case (fromJust (readMaybe [x]::Maybe Int)) of {0 -> 9; n -> n - 1}) $ ts in Just . H k t $ w + 'R' -> if length ts /= 3 then Just (E 0) else let [k,t,w] = map (\x -> case (fromJust (readMaybe [x]::Maybe Int)) of {0 -> 9; n -> n - 1}) $ ts in Just . R k t $ w _ -> Nothing | otherwise = Nothing @@ -109,6 +117,7 @@ setWordsN n (Just (SA _ i v)) = Just (SA n i v) setWordsN n (Just (SB _ i v)) = Just (SB n i v) setWordsN n (Just (F _ i j)) = Just (F n i j)+setWordsN _ cnstr = cnstr -- | A safer variant of the 'readMaybeEC' more suitable for applications, e. g. for phonetic-languages series of packages. readMaybeECG :: Int -> String -> Maybe EncodedCnstrs@@ -129,6 +138,10 @@ decodeConstraint1 (SA _ i v) = unsafeSeveralA i v decodeConstraint1 (SB _ i v) = unsafeSeveralB i v decodeConstraint1 (F _ i j) = filterOrderIJ i j+decodeConstraint1 (V i j) = filterSignDistanceIJ i j (j - i)+decodeConstraint1 (W i j) = filterUnsignDistanceIJ i j (abs $ j - i)+decodeConstraint1 (H i j k) = filterSignDistanceIJK3 i j k (j - i) (k - j)+decodeConstraint1 (R i j k) = filterUnsignDistanceIJK3 i j k (abs (j - i)) (abs (k - j)) -- | Must be applied to the correct array of permutation indeces. Otherwise, it gives runtime error (exception). All the integers inside the -- 'EncodedCnstrs' must be in the range [0..n] where @n@ corresponds to the maximum element in the permutation 'Array' 'Int' 'Int'. Besides,@@ -172,6 +185,22 @@ isSB (SB _ _ _) = True isSB _ = False +isV :: EncodedCnstrs -> Bool+isV (V _ _) = True+isV _ = False++isW :: EncodedCnstrs -> Bool+isW (W _ _) = True+isW _ = False++isH :: EncodedCnstrs -> Bool+isH (H _ _ _) = True+isH _ = False++isR :: EncodedCnstrs -> Bool+isR (R _ _ _) = True+isR _ = False+ {-| Works only with the correctly defined argument though it is not checked. Use with this caution. -} getIEl :: EncodedCnstrs -> Int@@ -182,6 +211,10 @@ getIEl (SA _ i _) = i getIEl (SB _ i _) = i getIEl (F _ i _) = i+getIEl (V i _) = i+getIEl (W i _) = i+getIEl (H i _ _) = i+getIEl (R i _ _) = i {-| Works only with the correctly defined arguments though it is not checked. Use with this caution. -}@@ -193,3 +226,8 @@ setIEl i (SA n _ v) = SA n i v setIEl i (SB n _ v) = SB n i v setIEl i (F n _ j) = F n i j+setIEl i (V _ j) = V i j+setIEl i (W _ j) = W i j+setIEl i (H _ j k) = H i j k+setIEl i (R _ j k) = R i j k+
phonetic-languages-constraints-array.cabal view
@@ -2,7 +2,7 @@ -- For further documentation, see http://haskell.org/cabal/users-guide/ name: phonetic-languages-constraints-array-version: 0.2.0.0+version: 0.3.0.0 synopsis: Constraints to filter the needed permutations description: Provides several the most important variants of constraints. Can be used with the phonetic-languages-common series of package. Instead of vectors, uses arrays. homepage: https://hackage.haskell.org/package/phonetic-languages-constraints-array