Skip to content

Commit 3738b83

Browse files
committed
Instance.hs: move satisfiesSchema down a bit. #148
1 parent f6fa803 commit 3738b83

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/Language/CQL/Instance.hs

+20-22
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,6 @@ import Language.CQL.Typeside as Typeside
6363
import Prelude hiding (EQ)
6464

6565

66-
--------------------------------------------------------------------------------------------------------------------
67-
-- Algebras
68-
69-
-- | Checks that an 'Instance' satisfies its 'Schema'.
70-
satisfiesSchema
71-
:: (MultiTyMap '[Show] '[var, ty, sym, en, fk, att, gen, sk, x, y], Eq x)
72-
=> Instance var ty sym en fk att gen sk x y
73-
-> Err ()
74-
satisfiesSchema (Instance sch pres' dp' alg) = do
75-
mapM_ (\( EQ (l, r)) -> if hasTypeType l then report (show l) (show r) (instEqT l r) else report (show l) (show r) (instEqE l r)) $ Set.toList $ IP.eqs pres'
76-
mapM_ (\(en'', EQ (l, r)) -> report (show l) (show r) (schEqT l r en'')) $ Set.toList $ obs_eqs sch
77-
mapM_ (\(en'', EQ (l, r)) -> report (show l) (show r) (schEqE l r en'')) $ Set.toList $ path_eqs sch
78-
where
79-
instEqE l r = nf alg (down1 l) == nf alg (down1 r)
80-
instEqT l r = dp' $ EQ ((repr'' alg (nf'' alg l)), (repr'' alg (nf'' alg r))) --morally we should create a new dp for the talg, but that's computationally intractable and this check still helps
81-
report _ _ True = return ()
82-
report l r False = Left $ "Not satisified: " ++ l ++ " = " ++ r
83-
schEqE l r e = foldr (\x b -> (evalSchTerm' alg x l == evalSchTerm' alg x r) && b) True (en alg e)
84-
schEqT l r e = foldr (\x b -> dp' (EQ (repr'' alg (evalSchTerm alg x l), repr'' alg (evalSchTerm alg x r))) && b) True (en alg e)
85-
86-
-------------------------------------------------------------------------------------------------------------------
87-
8866
-- | A database instance on a schema. Contains a presentation, an algebra, and a decision procedure.
8967
data Instance var ty sym en fk att gen sk x y
9068
= Instance
@@ -142,6 +120,26 @@ algebraToPresentation alg@(Algebra sch en' _ _ _ ty' _ _ _) =
142120
reify :: (Ord x, Ord en) => (en -> Set x) -> Set en -> [(x, en)]
143121
reify f s = concat $ Set.toList $ Set.map (\en'-> Set.toList $ Set.map (, en') $ f en') s
144122

123+
-- | Checks that an 'Instance' satisfies its 'Schema'.
124+
satisfiesSchema
125+
:: (MultiTyMap '[Show] '[var, ty, sym, en, fk, att, gen, sk, x, y], Eq x)
126+
=> Instance var ty sym en fk att gen sk x y
127+
-> Err ()
128+
satisfiesSchema (Instance sch pres' dp' alg) = do
129+
mapM_ (\( EQ (l, r)) -> if hasTypeType l then report (show l) (show r) (instEqT l r) else report (show l) (show r) (instEqE l r)) $ Set.toList $ IP.eqs pres'
130+
mapM_ (\(en'', EQ (l, r)) -> report (show l) (show r) (schEqT l r en'')) $ Set.toList $ obs_eqs sch
131+
mapM_ (\(en'', EQ (l, r)) -> report (show l) (show r) (schEqE l r en'')) $ Set.toList $ path_eqs sch
132+
where
133+
-- Morally, we should create a new dp (decision procedure) for the talg, but that's computationally intractable, and this check still helps.
134+
instEqE l r = nf alg (down1 l) == nf alg (down1 r)
135+
instEqT l r = dp' $ EQ ((repr'' alg (nf'' alg l)), (repr'' alg (nf'' alg r)))
136+
137+
report _ _ True = return ()
138+
report l r False = Left $ "Not satisfied: " ++ l ++ " = " ++ r
139+
140+
schEqE l r e = foldr (\x b -> (evalSchTerm' alg x l == evalSchTerm' alg x r) && b) True (en alg e)
141+
schEqT l r e = foldr (\x b -> dp' (EQ (repr'' alg (evalSchTerm alg x l), repr'' alg (evalSchTerm alg x r))) && b) True (en alg e)
142+
145143
-- | Constructs an algebra from a saturated theory with a free type algebra.
146144
-- Needs to have satisfaction checked.
147145
saturatedInstance

0 commit comments

Comments
 (0)