[NGRAMS-REPO] correctly reroot recursively

parent 99bfb3cf
...@@ -44,6 +44,7 @@ import Data.Patch.Class (Replace, replace, Action(act), Applicable(..), ...@@ -44,6 +44,7 @@ import Data.Patch.Class (Replace, replace, Action(act), Applicable(..),
ConflictResolutionReplace, ours) ConflictResolutionReplace, ours)
import qualified Data.Map.Strict.Patch as PM import qualified Data.Map.Strict.Patch as PM
import Data.Monoid import Data.Monoid
import Data.Foldable
--import Data.Semigroup --import Data.Semigroup
import Data.Set (Set) import Data.Set (Set)
-- import qualified Data.List as List -- import qualified Data.List as List
...@@ -54,7 +55,7 @@ import Data.Map.Strict (Map) ...@@ -54,7 +55,7 @@ import Data.Map.Strict (Map)
import qualified Data.Set as Set import qualified Data.Set as Set
import Control.Category ((>>>)) import Control.Category ((>>>))
import Control.Concurrent import Control.Concurrent
import Control.Lens (makeLenses, makePrisms, Getter, Prism', prism', Iso', iso, from, (.~), (#), to, {-withIndex, folded, ifolded,-} view, use, (^.), (+~), (%~), (%=), at, _Just, Each(..), itraverse_, both, mapped) import Control.Lens (makeLenses, makePrisms, Getter, Prism', prism', Iso', iso, from, (.~), (.=), (#), to, folded, {-withIndex, ifolded,-} view, use, (^.), (^?), (+~), (%~), (%=), at, _Just, Each(..), itraverse_, both, mapped, forOf_)
import Control.Monad (guard) import Control.Monad (guard)
import Control.Monad.Error.Class (MonadError, throwError) import Control.Monad.Error.Class (MonadError, throwError)
import Control.Monad.Reader import Control.Monad.Reader
...@@ -131,6 +132,9 @@ mSetFromSet = MSet . Map.fromSet (const ()) ...@@ -131,6 +132,9 @@ mSetFromSet = MSet . Map.fromSet (const ())
mSetFromList :: Ord a => [a] -> MSet a mSetFromList :: Ord a => [a] -> MSet a
mSetFromList = MSet . Map.fromList . map (\x -> (x, ())) mSetFromList = MSet . Map.fromList . map (\x -> (x, ()))
instance Foldable MSet where
foldMap f (MSet m) = Map.foldMapWithKey (\k _ -> f k) m
instance (Ord a, FromJSON a) => FromJSON (MSet a) where instance (Ord a, FromJSON a) => FromJSON (MSet a) where
parseJSON = fmap mSetFromList . parseJSON parseJSON = fmap mSetFromList . parseJSON
...@@ -525,10 +529,19 @@ instance Arbitrary NgramsTablePatch where ...@@ -525,10 +529,19 @@ instance Arbitrary NgramsTablePatch where
type ReParent a = forall m. MonadState NgramsTableMap m => a -> m () type ReParent a = forall m. MonadState NgramsTableMap m => a -> m ()
reRootChildren :: Maybe NgramsTerm -> ReParent NgramsTerm
reRootChildren root ngram = do
nre <- use $ at ngram
forOf_ (_Just . nre_children . folded) nre $ \child -> do
at child . _Just . nre_root .= root
reRootChildren root child
reParent :: Maybe RootParent -> ReParent NgramsTerm reParent :: Maybe RootParent -> ReParent NgramsTerm
reParent rp child = at child . _Just %= ( (nre_parent .~ (_rp_parent <$> rp)) reParent rp child = do
. (nre_root .~ (_rp_root <$> rp)) at child . _Just %= ( (nre_parent .~ (_rp_parent <$> rp))
) . (nre_root .~ (_rp_root <$> rp))
)
reRootChildren (rp ^? _Just . rp_root) child
reParentAddRem :: RootParent -> NgramsTerm -> ReParent AddRem reParentAddRem :: RootParent -> NgramsTerm -> ReParent AddRem
reParentAddRem rp child p = reParentAddRem rp child p =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment