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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
------------------------------------------------------------------------------
--- The `Oberserve` library containing combinators to observe data.
---
--- @author Bernd Brassle, Olaf Chitil, Michael Hanus, Frank Huch
--- @version February 2023
------------------------------------------------------------------------------

module Observe
  (observe, observeG,
   oLit, oInt, oBool, oChar, oFloat,
   oOpaque,
   oOpaqueConstr,
   oList,
   oString,
   oPair, oTriple, o4Tuple, o5Tuple,
   oMaybe,
   oEither,
   oFun, (~>), oFunFG, (~~>), oFunG, (~~~>),
   o0,o1,o2,o3,o4,o5,
   clearLogFile,
   Observer,
   derive
  )
 where

import System.IO.Unsafe ( isVar, spawnConstraint, unsafePerformIO )

import Data.Global      ( GlobalT, globalT, readGlobalT, writeGlobalT )
import System.Process   ( system )

import Coosy.Derive     ( derive )
import Coosy.Trace


infixr 5 ~>, ~~>, ~~~>

type Observer a = (a -> Label -> EventID -> [EventID] -> a)

------------------------------------------------------------------------------
--- The basic operation to observe the evaluation of data structures.
--- It has a `Data` context so that it can also observe the instantiation
--- of free variables occurring in data structures.
observe :: Data a => Observer a -> String -> a -> a
observe observeA label x = initialObserver observeA 0 x label (-1) preds
 where preds free

initialObserver :: Data a => Observer a -> Int -> Observer a
initialObserver observeA argNr x label parent preds = unsafePerformIO $ do
  clearFileCheck
  observerIO observeA argNr x label parent preds

observer :: Data a => Observer a -> Int -> Observer a
observer observeA argNr x label parent preds = unsafePerformIO $
  observerIO observeA argNr x label parent preds

observerIO :: Data a =>
              Observer a -> Int -> a -> Label -> EventID -> [EventID] -> IO a
observerIO observeA argNr x l parent preds = do
  (eventID, newPreds) <- recordEvent l (Demand argNr) parent preds
  if isVar x
    then
      do (logVarID,logVarPreds) <- recordEvent l LogVar eventID newPreds
         spawnConstraint
           (seq (ensureNotFree x) (observeA x l logVarID logVarPreds =:= x))
           (return x)
    else return $ observeA x l eventID newPreds


--- The basic operation to observe the evaluation of ground data structures.
--- It does not require a `Data` context but it can not observe
--- the instantiation of free variables occurring in data structures.
--- Thus, it has to be ensured that free variables do not occur in the
--- observed structures, otherwise this observer always suspends.
observeG :: Observer a -> String -> a -> a
observeG observeA label x =
  initialObserverG observeA 0 x label (-1) preds
 where preds free

initialObserverG :: Observer a -> Int -> Observer a
initialObserverG observeA argNr x label parent preds = unsafePerformIO $ do
  clearFileCheck
  observerGIO observeA argNr x label parent preds

observerG :: Observer a -> Int -> Observer a
observerG observeA argNr x label parent preds = unsafePerformIO $
  observerGIO observeA argNr x label parent preds

observerGIO :: Observer a -> Int -> a -> Label -> EventID -> [EventID] -> IO a
observerGIO observeA argNr x l parent preds = do
  (eventID, newPreds) <- recordEvent l (Demand argNr) parent preds
  (ensureNotFree x) `seq` return (observeA x l eventID newPreds)

------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Combinators to construct observers for various kinds of data.

-- An observer for literals.
oLit :: Show a => Observer a
oLit l = o0 (show l) l

-- An observer for integers.
oInt :: Observer Int
oInt = oLit

-- An observer for Booleans.
oBool :: Observer Bool
oBool = oLit

-- An observer for chacacters.
oChar :: Observer Char
oChar = oLit

-- An observer for floats.
oFloat :: Observer Float
oFloat = oLit

-- An opaque observer which does not observe the values.
-- This might be useful to observe polymorphic types.
oOpaque :: Observer _
oOpaque x = o0 "#" x

oOpaqueConstr :: String -> Observer _
oOpaqueConstr constr x = o0 constr x

-- An observer combinator for lists.
oList :: Data a => Observer a -> Observer [a]
oList _        []     = o0 "[]" []
oList observeA (x:xs) = o2 observeA (oList observeA) "(:)" (:) x xs

-- An observer for strings.
oString :: Observer String
oString = oList oChar

-- An observer combinator for pairs.
oPair  :: (Data a, Data b) => Observer a -> Observer b -> Observer (a,b)
oPair observeA observeB (x,y) =
  o2 observeA observeB "(,)" (\a b -> (a,b)) x y

-- An observer combinator for triples.
oTriple :: (Data a, Data b, Data c) =>
           Observer a -> Observer b -> Observer c -> Observer (a,b,c)
oTriple observeA observeB observeC (x,y,z) =
  o3 observeA observeB observeC "(,,)" (\a b c -> (a,b,c)) x y z

-- An observer combinator for quadruples.
o4Tuple :: (Data a, Data b, Data c, Data d) =>
           Observer a -> Observer b -> Observer c -> Observer d
                                                  -> Observer (a,b,c,d)
o4Tuple observeA observeB observeC observeD (x1,x2,x3,x4) =
  o4 observeA observeB observeC observeD "(,,,)"
     (\a b c d -> (a,b,c,d)) x1 x2 x3 x4

-- An observer combinator for 5-tuples.
o5Tuple :: (Data a, Data b, Data c, Data d, Data e) =>
           Observer a -> Observer b -> Observer c -> Observer d -> Observer e
                                                  -> Observer (a,b,c,d,e)
o5Tuple observeA observeB observeC observeD observeE (x1,x2,x3,x4,x5) =
  o5 observeA observeB observeC observeD observeE "(,,,,)"
     (\a b c d e -> (a,b,c,d,e)) x1 x2 x3 x4 x5

-- An observer combinator for the `Maybe` type.
oMaybe :: Data a => Observer a -> Observer (Maybe a)
oMaybe _        Nothing  = o0 "Nothing" Nothing
oMaybe observeA (Just x) = o1 observeA "Just" Just x

-- An observer combinator for the `Either` type.
oEither :: (Data a, Data b) => Observer a -> Observer b -> Observer (Either a b)
oEither observeA _ (Left a)  = o1 observeA "Left" Left a
oEither _ observeB (Right b) = o1 observeB "Right" Right b

-- An observer combinator for the `IO` type.
oIO :: Data a => Observer a -> Observer (IO a)
oIO observeA action parent preds label =
  action >>= \res -> o1 observeA "<IO>" return res parent preds label

-- Construct an observer for functions where the argument and result might be
-- a free variable.
oFun :: (Data a, Data b) => Observer a -> Observer b -> Observer (a -> b)
oFun observeA observeB f label parent preds arg =
  (unsafePerformIO $ do
    (eventID,newPreds) <- recordEvent label Fun parent preds
    return (\x -> (observer observeB 2
                     (f (observer observeA 1 x label eventID newPreds))
                     label eventID newPreds)))
  arg

-- Construct an observer for functions where the argument and result might be
-- a free variable.
(~>) :: (Data a, Data b) => Observer a -> Observer b -> Observer (a -> b)
a ~> b = oFun a b

-- Construct an observer for functions where the argument might be
-- a free variable and the result is always non-free (e.g., a functional value).
oFunFG :: Data a => Observer a -> Observer b -> Observer (a -> b)
oFunFG observeA observeB f label parent preds arg =
  (unsafePerformIO $ do
    (eventID,newPreds) <- recordEvent label Fun parent preds
    return (\x -> observerG observeB 2
                    (f (observer observeA 1 x label eventID newPreds))
                    label eventID newPreds))
  arg

-- Construct an observer for functions where the argument might be
-- a free variable and the result is always non-free (e.g., a functional value).
(~~>) :: Data a => Observer a -> Observer b -> Observer (a -> b)
a ~~> b = oFunFG a b

-- Construct an observer for functions where the argument and the result
-- are never free variables.
oFunG :: Observer a -> Observer b -> Observer (a -> b)
oFunG observeA observeB f label parent preds arg =
  (unsafePerformIO $ do
    (eventID,newPreds) <- recordEvent label Fun parent preds
    return (\x -> observerG observeB 2
                    (f (observerG observeA 1 x label eventID newPreds))
                    label eventID newPreds))
  arg

-- Construct an observer for functions where the argument and the result
-- are never free variables.
(~~~>) :: Observer a -> Observer b -> Observer (a -> b)
a ~~~> b = oFunG a b

------------------------------------------------------------------------------
-- Observers for constructors.

-- An observer combinator for 0-ary constructors.
o0 :: String -> Observer _
o0 constrStr x label parent preds = unsafePerformIO $ do
  recordEvent label (Value 0 constrStr) parent preds
  return x

-- An observer combinator for unary constructors.
o1 :: Data a => Observer a -> String
   -> (a -> b) -> a -> Label -> EventID -> [EventID] -> b
o1 observeA constrStr constr x label parent preds = unsafePerformIO $ do
  (eventID,newPreds) <- recordEvent label (Value 1 constrStr) parent preds
  return (constr (observer observeA 1 x label eventID newPreds))

-- An observer combinator for binary constructors.
o2 :: (Data a, Data b) => Observer a -> Observer b -> String ->
      (a -> b -> c) -> a -> b -> Label -> EventID -> [EventID] ->  c
o2 observeA observeB constrStr constr x1 x2 label parent preds =
  unsafePerformIO $ do
    (eventID,newPreds) <- recordEvent label (Value 2 constrStr) parent preds
    return (constr (observer observeA 1 x1 label eventID newPreds)
                   (observer observeB 2 x2 label eventID newPreds))

-- An observer combinator for ternary constructors.
o3 :: (Data a, Data b, Data c) =>
      Observer a -> Observer b -> Observer c -> String ->
      (a -> b -> c -> d) ->
      a -> b -> c -> Label -> EventID -> [EventID] -> d
o3 observeA observeB observeC constrStr constr x1 x2 x3 label parent preds =
  unsafePerformIO $ do
    (eventID,newPreds) <- recordEvent label (Value 3 constrStr) parent preds
    return (constr (observer observeA 1 x1 label eventID newPreds)
                   (observer observeB 2 x2 label eventID newPreds)
                   (observer observeC 3 x3 label eventID newPreds))

-- An observer combinator for constructors of arity 4.
o4 :: (Data a, Data b, Data c, Data d) =>
      Observer a -> Observer b -> Observer c -> Observer d -> String ->
      (a -> b -> c -> d -> e) ->
      a -> b -> c -> d -> Label -> EventID -> [EventID] -> e
o4 observeA observeB observeC observeD constrStr constr x1 x2 x3 x4
  label parent preds =
  unsafePerformIO $ do
    (eventID,newPreds) <- recordEvent label (Value 4 constrStr) parent preds
    return (constr (observer observeA 1 x1 label eventID newPreds)
                   (observer observeB 2 x2 label eventID newPreds)
                   (observer observeC 3 x3 label eventID newPreds)
                   (observer observeD 4 x4 label eventID newPreds))

-- An observer combinator for constructors of arity 5.
o5 :: (Data a, Data b, Data c, Data d, Data e) =>
      Observer a -> Observer b -> Observer c -> Observer d ->
      Observer e -> String ->
      (a -> b -> c -> d -> e -> f) ->
      a -> b -> c -> d -> e -> Label -> EventID -> [EventID] -> f
o5 observeA observeB observeC observeD observeE constrStr constr
  x1 x2 x3 x4 x5 label parent preds =
  unsafePerformIO $ do
    (eventID,newPreds) <- recordEvent label (Value 5 constrStr) parent preds
    return (constr (observer observeA 1 x1 label eventID newPreds)
                   (observer observeB 2 x2 label eventID newPreds)
                   (observer observeC 3 x3 label eventID newPreds)
                   (observer observeD 4 x4 label eventID newPreds)
                   (observer observeE 5 x5 label eventID newPreds))

------------------------------------------------------------------------------
-- Auxiliary definitions.

globalEventID :: GlobalT EventID
globalEventID = globalT "Mod.gt" 0

getNewID :: IO EventID
getNewID = do
   n <- readGlobalT globalEventID
   writeGlobalT globalEventID (n+1)
   return n

getPred :: EventID -> [EventID] -> (EventID,[EventID])
getPred p xs | isVar xs  = (p,xs)
             | otherwise = getPred (head xs) (tail xs)

writeToTraceFile :: Label -> Event -> IO ()
writeToTraceFile label event =
  appendFile (logFile label) (showEvent event ++ "\n")

recordEvent :: Label -> (EventID -> EventID -> EventID -> Event) ->
               EventID -> [EventID] -> IO (EventID,[EventID])
recordEvent label event parent preds = do
  eventID <- getNewID
  let (pred,logVar) = getPred parent preds
  doSolve (logVar =:= (eventID:newLogVar))
  writeToTraceFile label (event eventID parent pred)
  return (eventID, newLogVar)
 where newLogVar free

clearLogFile :: IO ()
clearLogFile = do
   system $ "touch " ++ logFile "" ++ "; rm " ++ logFile "*"
   writeGlobalT globalEventID 0

clearFileCheck :: IO ()
clearFileCheck =
  let clearFile = logFileClear in
   readFile clearFile >>= \clearStr ->
   if clearStr == "1"
     then do writeGlobalT globalEventID 0
             writeFile clearFile ""
     else return ()

------------------------------------------------------------------------------