1
Fork 0
mirror of https://github.com/Jaxan/nominal-lstar.git synced 2025-04-27 14:47:45 +02:00

Got rid of redundant queries + minor changes

This commit is contained in:
Joshua Moerman 2021-02-25 14:46:12 +01:00
parent 7b41e7d97c
commit 2004c75471
2 changed files with 16 additions and 18 deletions

View file

@ -20,16 +20,16 @@ mqToSubset mq = mapFilter (\(i, o) -> maybeIf (fromBool o) i) . mq
-- A table is nothing more than a part of the language. -- A table is nothing more than a part of the language.
-- Invariant: content is always a subset of -- Invariant: content is always a subset of
-- `rows * columns` union `rows * alph * columns`. -- `domain` = `rows * columns` union `rows * alph * columns`.
data Table i = Table data Table i = Table
{ content :: Set [i] { content :: Set [i]
, domain :: Set [i]
, rowIndices :: Set (RowIndex i) , rowIndices :: Set (RowIndex i)
, colIndices :: Set (ColumnIndex i) , colIndices :: Set (ColumnIndex i)
, aa :: Set i , aa :: Set i
} }
deriving (Show, Ord, Eq, Generic, NominalType, Conditional, Contextual) deriving (Show, Ord, Eq, Generic, NominalType, Conditional, Contextual)
-- Currently, it may ask redundant membership queries
instance (NominalType i, Contextual i) => ObservationTable (Table i) i Bool where instance (NominalType i, Contextual i) => ObservationTable (Table i) i Bool where
type Row (Table i) = Set [i] type Row (Table i) = Set [i]
rows = rowIndices rows = rowIndices
@ -41,26 +41,31 @@ instance (NominalType i, Contextual i) => ObservationTable (Table i) i Bool wher
addRows mq newRows t@Table{..} = addRows mq newRows t@Table{..} =
t { content = content `union` newContent t { content = content `union` newContent
, domain = domain `union` newPartRed
, rowIndices = rowIndices `union` newRows , rowIndices = rowIndices `union` newRows
} }
where where
newRowsExt = pairsWith (\r a -> r ++ [a]) newRows aa newRowsExt = pairsWith (\r a -> r ++ [a]) newRows aa
newPart = pairsWith (++) (newRows `union` newRowsExt) colIndices newPart = pairsWith (++) (newRows `union` newRowsExt) colIndices
newContent = mqToSubset mq newPart newPartRed = newPart \\ domain
newContent = mqToSubset mq newPartRed
addColumns mq newColumns t@Table{..} = addColumns mq newColumns t@Table{..} =
t { content = content `union` newContent t { content = content `union` newContent
, domain = domain `union` newPartRed
, colIndices = colIndices `union` newColumns , colIndices = colIndices `union` newColumns
} }
where where
newColumnsExt = pairsWith (:) aa newColumns newColumnsExt = pairsWith (:) aa newColumns
newPart = pairsWith (++) rowIndices (newColumns `union` newColumnsExt) newPart = pairsWith (++) rowIndices (newColumns `union` newColumnsExt)
newContent = mqToSubset mq newPart newPartRed = newPart \\ domain
newContent = mqToSubset mq newPartRed
initialBTableWith :: NominalType i => MQ i Bool -> Set i -> Set (RowIndex i) -> Set (ColumnIndex i) -> Table i initialBTableWith :: NominalType i => MQ i Bool -> Set i -> Set (RowIndex i) -> Set (ColumnIndex i) -> Table i
initialBTableWith mq alphabet newRows newColumns = Table initialBTableWith mq alphabet newRows newColumns = Table
{ content = content { content = content
, domain = domain
, rowIndices = newRows , rowIndices = newRows
, colIndices = newColumns , colIndices = newColumns
, aa = alphabet , aa = alphabet

View file

@ -67,9 +67,8 @@ instance (NominalType i, NominalType o) => ObservationTable (Table i o) i o wher
newContent = mq newPartRed newContent = mq newPartRed
-- I could make a more specific implementation for booleans. -- We can reuse the above tables for the Boolean case and
-- But for now we reuse the above, and do minor optimisations -- perform some minor optimisations.
newtype Boolean table = B { unB :: table } newtype Boolean table = B { unB :: table }
deriving (Show, Ord, Eq, Generic, NominalType, Conditional, Contextual) deriving (Show, Ord, Eq, Generic, NominalType, Conditional, Contextual)
@ -78,28 +77,22 @@ type BTable i = Boolean (Table i Bool)
instance (NominalType i) => ObservationTable (BTable i) i Bool where instance (NominalType i) => ObservationTable (BTable i) i Bool where
-- Special case of a boolean: functions to Booleans are subsets -- Special case of a boolean: functions to Booleans are subsets
type Row (BTable i) = Set [i] type Row (BTable i) = Set [i]
-- All the reusable functions are simply coerced
rows = coerce (rows :: _ => Table i Bool -> _) rows = coerce (rows :: _ => Table i Bool -> _)
cols = coerce (cols :: _ => Table i Bool -> _) cols = coerce (cols :: _ => Table i Bool -> _)
rowsExt = coerce (rowsExt :: _ => Table i Bool -> _) rowsExt = coerce (rowsExt :: _ => Table i Bool -> _)
colsExt = coerce (colsExt :: _ => Table i Bool -> _) colsExt = coerce (colsExt :: _ => Table i Bool -> _)
alph = coerce (alph :: _ => Table i Bool -> _) alph = coerce (alph :: _ => Table i Bool -> _)
tableAt = coerce (tableAt :: _ => Table i Bool -> _) tableAt = coerce (tableAt :: _ => Table i Bool -> _)
--rows = rowIndices . unB addRows = coerce (addRows :: _ => _ -> _ -> Table i Bool -> Table i Bool)
--cols = colIndices . unB addColumns = coerce (addColumns :: _ => _ -> _ -> Table i Bool -> Table i Bool)
--alph = aa . unB
--tableAt = tableAt . unB
-- TODO: slightly inefficient -- These are specific to our representation of Row
row (B Table{..}) r = let lang = mapFilter (\(i, o) -> maybeIf (fromBool o) i) content row (B Table{..}) r = let lang = mapFilter (\(i, o) -> maybeIf (fromBool o) i) content
in filter (\a -> lang `contains` (r ++ a)) colIndices in filter (\a -> lang `contains` (r ++ a)) colIndices
rowEps (B Table{..}) = mapFilter (\(i, o) -> maybeIf (fromBool o /\ i `member` colIndices) i) content rowEps (B Table{..}) = mapFilter (\(i, o) -> maybeIf (fromBool o /\ i `member` colIndices) i) content
--addRows mq newRows = B . addRows mq newRows . unB
addRows = coerce (addRows :: _ => _ -> _ -> Table i Bool -> Table i Bool)
--addColumns mq newColumns = B . addColumns mq newColumns . unB
addColumns = coerce (addColumns :: _ => _ -> _ -> Table i Bool -> Table i Bool)
type BRow i = Row (BTable i)
initialTableWith :: (NominalType i, NominalType o) => MQ i o -> Set i -> Set (RowIndex i) -> Set (ColumnIndex i) -> Table i o initialTableWith :: (NominalType i, NominalType o) => MQ i o -> Set i -> Set (RowIndex i) -> Set (ColumnIndex i) -> Table i o
initialTableWith mq alphabet newRows newColumns = Table initialTableWith mq alphabet newRows newColumns = Table