-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathInternal.hs
More file actions
220 lines (169 loc) · 7.14 KB
/
Internal.hs
File metadata and controls
220 lines (169 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
{-
SPDX-License-Identifier: AGPL-3.0-only
This file is part of `statebox/cql`, the categorical query language.
Copyright (C) 2019 Stichting Statebox <https://statebox.nl>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
{-# LANGUAGE ViewPatterns
, FlexibleContexts
, FlexibleInstances
, TypeFamilies
, UndecidableInstances
, MultiParamTypeClasses
, FunctionalDependencies
#-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ImportQualifiedPost #-}
module Language.CQL.Internal where
import Prelude hiding (abs, any)
import Control.Arrow
import Control.Monad
import Control.Monad.Trans.UnionFind (Point, UnionFindT, fresh)
import Control.Monad.Trans.UnionFind qualified as U
import Data.List qualified as L
--import Data.Sequence (Seq)
import Data.Foldable (traverse_)
import Data.Graph.Inductive hiding (Graph)
import Data.Map (Map)
import Data.Maybe (fromJust)
newtype Conjunctions t = Conjunction [Equation t]
data Equation t
= Equal (Term t) (Term t)
| NotEqual (Term t) (Term t)
data Term t = Function t [Term t]
deriving stock (Eq, Ord)
data Satisfiability t = Satisfiable (Model t) | Unsatisfiable
deriving stock (Show, Eq)
type Model t = Map (Term t) (Term t)
(===) :: Term t -> Term t -> Equation t
(===) = Equal
infix 4 ===
(=/=) :: Term t -> Term t -> Equation t
(=/=) = NotEqual
infix 4 =/=
instance Show t => Show (Term t) where
show (Function sym childs) =
show sym ++ "(" ++ L.intercalate "," (map show childs) ++ ")"
class Conjunction t a | a -> t where
(/\) :: Equation t -> a -> Conjunctions t
infixr 3 /\
instance (Conjunction t (Equation t)) where
(/\) e1 e2 = Conjunction [e1, e2]
instance Conjunction t (Conjunctions t) where
(/\) e1 (Conjunction e2) = Conjunction (e1:e2)
newtype Graph t = Graph (NodeMap (Term t), Gr (t, Point (LNode t)) Int)
newtype Vert t = Vert (LNode (t, Point (LNode t)))
interleave :: [(a,a)] -> [a]
interleave ((x,y):rest) = x : y : interleave rest
interleave [] = []
termGraph :: (Monad m, Ord t) => [Equation t] -> UnionFindT (LNode t) m (Graph t)
termGraph = termGraph' . interleave . terms
termGraph' :: (Monad m, Ord t) => [Term t] -> UnionFindT (LNode t) m (Graph t)
termGraph' ts = do
let (nodeMap, gr) = snd $ run empty $ traverse_ insertTerm ts
vars <- traverse genVars (labNodes gr)
return $ Graph (nodeMap, mkGraph vars (labEdges gr))
where
insertTerm :: Ord t => Term t -> NodeMapM (Term t) Int Gr ()
insertTerm trm@(Function _ childs) = do
_ <- insMapNodeM trm
forM_ (zip childs [1..]) $ \(child,i) -> do
_ <- insMapNodeM child
insMapEdgeM (trm,child,i)
insertTerm child
genVars (node, Function name _) = do
var <- fresh (node,name)
return (node,(name,var))
vertex :: Ord t => Graph t -> Term t -> Vert t
vertex gr@(Graph (nodeMap,_)) trm =
let (node,_) = mkNode_ nodeMap trm
in label gr node
graph :: Graph t -> Gr (t, Point (LNode t)) Int
graph (Graph (_, gr)) = gr
vertices :: Graph t -> [Vert t]
vertices = map Vert . labNodes . graph
outDegree :: Graph t -> Vert t -> Int
outDegree (graph -> gr) (Vert (x, _)) = outdeg gr x
label :: Graph t -> Node -> Vert t
label (graph -> gr) a = Vert (a, fromJust (lab gr a))
equivalent :: (Monad m) => Vert t -> Vert t -> UnionFindT (LNode t) m Bool
equivalent (Vert (_,(_,x))) (Vert (_,(_,y))) = U.equivalent x y
union :: (Monad m) => Vert t -> Vert t -> UnionFindT (LNode t) m ()
union (Vert (_,(_,x))) (Vert (_,(_,y))) = U.union x y
predecessors :: Graph t -> Vert t -> [Vert t]
predecessors gr (Vert (x,_)) = label gr <$> pre (graph gr) x
successors :: Graph t -> Vert t -> [Vert t]
successors gr (Vert (x,_)) = label gr <$> suc (graph gr) x
terms :: [Equation t] -> [(Term t, Term t)]
terms = map go
where
go e = case e of
Equal t1 t2 -> (t1,t2)
NotEqual t1 t2 -> (t1,t2)
term :: Graph t -> Vert t -> Term t
term (Graph (_,gr0)) (Vert (n0,_)) = go gr0 n0
where
go :: Gr (t, a) Int -> Node -> Term t
go gr n =
case match n gr of
(Nothing,_) -> error "context is Nothing"
(Just (_,_,(sym,_),out0),gr') ->
Function sym $ map (go gr') $ sortEdges out0
sortEdges out0 = map snd $ L.sortOn fst out0
partition :: Ord t => Graph t -> (Equation t -> Bool) -> [Equation t] -> ([(Vert t,Vert t)],[(Vert t,Vert t)])
partition gr f equations =
let (as,bs) = L.partition f equations
in (map (vertex gr *** vertex gr) $ terms as, map (vertex gr *** vertex gr) $ terms bs)
unless :: Monad m => m Bool -> m () -> m ()
unless mbool m = do
b <- mbool
Control.Monad.unless b m
instance Show t => Show (Vert t) where
show (Vert (n, _)) = show n
positive :: Equation t -> Bool
positive t =
case t of
Equal _ _ -> True
NotEqual _ _ -> False
any :: Monad m => (a -> b -> m Bool) -> [(a,b)] -> m Bool
any _ [] = return False
any f ((a,b):abs) = do
r <- f a b
if r
then return True
else any f abs
{--
Copyright (c) 2014, Sven Keidel
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Sven Keidel nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--}