summaryrefslogtreecommitdiff
path: root/src/disjoint.sml
blob: 784bd63591521f0e27ae37487c62a45e3bc70d5d (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
(* Copyright (c) 2008, Adam Chlipala
 * 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.
 * - The names of contributors may not 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.
 *)

structure Disjoint :> DISJOINT = struct

open Elab
open ElabOps

structure SS = BinarySetFn(struct
                           type ord_key = string
                           val compare = String.compare
                           end)

structure IS = IntBinarySet
structure IM = IntBinaryMap

type name_ineqs = {
     namesC : SS.set,
     namesR : IS.set,
     namesN : IS.set
}

val name_default = {
    namesC = SS.empty,
    namesR = IS.empty,
    namesN = IS.empty
}

type row_ineqs = {
     namesC : SS.set,
     namesR : IS.set,
     namesN : IS.set,
     rowsR : IS.set,
     rowsN : IS.set
}

val row_default = {
    namesC = SS.empty,
    namesR = IS.empty,
    namesN = IS.empty,
    rowsR = IS.empty,
    rowsN = IS.empty
}

fun nameToRow_ineqs {namesC, namesR, namesN} =
    {namesC = namesC,
     namesR = namesR,
     namesN = namesN,
     rowsR = IS.empty,
     rowsN = IS.empty}

type env = {
     namesR : name_ineqs IM.map,
     namesN : name_ineqs IM.map,
     rowsR : row_ineqs IM.map,
     rowsN : row_ineqs IM.map
}

val empty = {
    namesR = IM.empty,
    namesN = IM.empty,
    rowsR = IM.empty,
    rowsN = IM.empty
}

datatype piece =
         NameC of string
       | NameR of int
       | NameN of int
       | RowR of int
       | RowN of int
       | Unknown

fun nameToRow (c, loc) =
    (CRecord ((KUnit, loc), [((c, loc), (CUnit, loc))]), loc)

fun pieceToRow (p, loc) =
    case p of
        NameC s => nameToRow (CName s, loc)
      | NameR n => nameToRow (CRel n, loc)
      | NameN n => nameToRow (CNamed n, loc)
      | RowR n => (CRel n, loc)
      | RowN n => (CRel n, loc)
      | Unknown => raise Fail "Unknown to row"

fun decomposeRow env c =
    let
        fun decomposeName (c, acc) =
            case #1 (hnormCon env c) of
                CName s => NameC s :: acc
              | CRel n => NameR n :: acc
              | CNamed n => NameN n :: acc
              | _ => Unknown :: acc
                     
        fun decomposeRow (c, acc) =
            case #1 (hnormCon env c) of
                CRecord (_, xcs) => foldl (fn ((x, _), acc) => decomposeName (x, acc)) acc xcs
              | CConcat (c1, c2) => decomposeRow (c1, decomposeRow (c2, acc))
              | CRel n => RowR n :: acc
              | CNamed n => RowN n :: acc
              | _ => Unknown :: acc
    in
        decomposeRow (c, [])
    end

fun assertPiece_name (ps, ineqs : name_ineqs) =
    {namesC = foldl (fn (p', namesC) =>
                        case p' of
                            NameC s => SS.add (namesC, s)
                          | _ => namesC) (#namesC ineqs) ps,
     namesR = foldl (fn (p', namesR) =>
                        case p' of
                            NameR n => IS.add (namesR, n)
                          | _ => namesR) (#namesR ineqs) ps,
     namesN = foldl (fn (p', namesN) =>
                        case p' of
                            NameN n => IS.add (namesN, n)
                          | _ => namesN) (#namesN ineqs) ps}

fun assertPiece_row (ps, ineqs : row_ineqs) =
    {namesC = foldl (fn (p', namesC) =>
                        case p' of
                            NameC s => SS.add (namesC, s)
                          | _ => namesC) (#namesC ineqs) ps,
     namesR = foldl (fn (p', namesR) =>
                        case p' of
                            NameR n => IS.add (namesR, n)
                          | _ => namesR) (#namesR ineqs) ps,
     namesN = foldl (fn (p', namesN) =>
                        case p' of
                            NameN n => IS.add (namesN, n)
                          | _ => namesN) (#namesN ineqs) ps,
     rowsR = foldl (fn (p', rowsR) =>
                        case p' of
                            RowR n => IS.add (rowsR, n)
                          | _ => rowsR) (#rowsR ineqs) ps,
     rowsN = foldl (fn (p', rowsN) =>
                        case p' of
                            RowN n => IS.add (rowsN, n)
                          | _ => rowsN) (#rowsN ineqs) ps}

fun assertPiece ps (p, denv) =
    case p of
        Unknown => denv
      | NameC _ => denv

      | NameR n =>
        let
            val ineqs = Option.getOpt (IM.find (#namesR denv, n), name_default)
            val ineqs = assertPiece_name (ps, ineqs)
        in
            {namesR = IM.insert (#namesR denv, n, ineqs),
             namesN = #namesN denv,
             rowsR = #rowsR denv,
             rowsN = #rowsN denv}
        end

      | NameN n =>
        let
            val ineqs = Option.getOpt (IM.find (#namesN denv, n), name_default)
            val ineqs = assertPiece_name (ps, ineqs)
        in
            {namesR = #namesR denv,
             namesN = IM.insert (#namesN denv, n, ineqs),
             rowsR = #rowsR denv,
             rowsN = #rowsN denv}
        end

      | RowR n =>
        let
            val ineqs = Option.getOpt (IM.find (#rowsR denv, n), row_default)
            val ineqs = assertPiece_row (ps, ineqs)
        in
            {namesR = #namesR denv,
             namesN = #namesN denv,
             rowsR = IM.insert (#rowsR denv, n, ineqs),
             rowsN = #rowsN denv}
        end

      | RowN n =>
        let
            val ineqs = Option.getOpt (IM.find (#rowsN denv, n), row_default)
            val ineqs = assertPiece_row (ps, ineqs)
        in
            {namesR = #namesR denv,
             namesN = #namesN denv,
             rowsR = #rowsR denv,
             rowsN = IM.insert (#rowsN denv, n, ineqs)}
        end

fun assert env denv (c1, c2) =
    let
        val ps1 = decomposeRow env c1
        val ps2 = decomposeRow env c2

        val denv = foldl (assertPiece ps2) denv ps1
    in
        foldl (assertPiece ps1) denv ps2
    end

fun nameEnter {namesC, namesR, namesN} =
    {namesC = namesC,
     namesR = IS.map (fn n => n + 1) namesR,
     namesN = namesN}

fun rowEnter {namesC, namesR, namesN, rowsR, rowsN} =
    {namesC = namesC,
     namesR = IS.map (fn n => n + 1) namesR,
     namesN = namesN,
     rowsR = IS.map (fn n => n + 1) rowsR,
     rowsN = rowsN}

fun enter {namesR, namesN, rowsR, rowsN} =
    {namesR = IM.foldli (fn (n, ineqs, namesR) => IM.insert (namesR, n+1, nameEnter ineqs)) IM.empty namesR,
     namesN = IM.map nameEnter namesN,
     rowsR = IM.foldli (fn (n, ineqs, rowsR) => IM.insert (rowsR, n+1, rowEnter ineqs)) IM.empty rowsR,
     rowsN = IM.map rowEnter rowsN}

fun getIneqs (denv : env) p =
    case p of
        Unknown => raise Fail "getIneqs: Unknown"
      | NameC _ => raise Fail "getIneqs: NameC"
      | NameR n => nameToRow_ineqs (Option.getOpt (IM.find (#namesR denv, n), name_default))
      | NameN n => nameToRow_ineqs (Option.getOpt (IM.find (#namesN denv, n), name_default))
      | RowR n => Option.getOpt (IM.find (#rowsR denv, n), row_default)
      | RowN n => Option.getOpt (IM.find (#rowsN denv, n), row_default)

fun prove1' denv (p1, p2) =
    let
        val {namesC, namesR, namesN, rowsR, rowsN} = getIneqs denv p1
    in
        case p2 of
            Unknown => raise Fail "prove1': Unknown"
          | NameC s => SS.member (namesC, s)
          | NameR n => IS.member (namesR, n)
          | NameN n => IS.member (namesN, n)
          | RowR n => IS.member (rowsR, n)
          | RowN n => IS.member (rowsN, n)
    end

fun prove1 denv (p1, p2) =
    case (p1, p2) of
        (NameC s1, NameC s2) => s1 <> s2
      | (_, RowR _) => prove1' denv (p2, p1)
      | (_, RowN _) => prove1' denv (p2, p1)
      | _ => prove1' denv (p1, p2)

fun prove env denv (c1, c2, loc) =
    let
        val ps1 = decomposeRow env c1
        val ps2 = decomposeRow env c2

        val hasUnknown = List.exists (fn p => p = Unknown)
    in
        if hasUnknown ps1 orelse hasUnknown ps2 then
            (ErrorMsg.errorAt loc "Structure of row is too complicated to prove disjointness";
             Print.eprefaces' [("Row 1", ElabPrint.p_con env c1),
                               ("Row 2", ElabPrint.p_con env c2)];
             [])
        else
            foldl (fn (p1, rem) =>
                      foldl (fn (p2, rem) =>
                                if prove1 denv (p1, p2) then
                                    rem
                                else
                                    (pieceToRow (p1, loc), pieceToRow (p2, loc)) :: rem) rem ps2)
            [] ps1
    end

end