summaryrefslogtreecommitdiff
path: root/app/Builtins.hs
blob: a2736607b8a304b36383528156b4ed95639552e9 (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
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
337
338
339
340
module Builtins where

import Code
  ( Builtin(..)
  , Cho(..)
  , Code
  , Datum(..)
  , Dereferenced(..)
  , Heap(..)
  , Instr(..)
  , Interp(..)
  , InterpFn
  , InterpFn
  , derefHeap
  , heapStruct
  , newHeapVars
  )
import qualified Compiler as Co
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State.Lazy (get, gets, modify)
import Data.Functor.Identity (runIdentity)
import Data.List (intercalate)
import qualified Data.Map as M
import Data.Maybe (fromJust)
import Env (PrlgEnv(..), findAtom, findStruct, prlgError)
import qualified IR
import Interpreter (backtrack)
import qualified Operators as O
import System.Console.Haskeline (getInputChar, outputStr, outputStrLn)

bi = Builtin

continue = pure Nothing

showTerm itos heap = runIdentity . heapStruct atom struct hrec heap
  where
    atom (Atom a) = pure $ "'" ++ itos M.! a ++ "'"
    atom (Number n) = pure (show n)
    atom VoidRef = pure "_"
    struct (Struct (IR.Id h _)) args =
      pure $ "'" ++ itos M.! h ++ "'(" ++ intercalate "," args ++ ")"
    hrec (HeapRef hr) ref =
      pure $
      (if hr == ref
         then "_X"
         else "_Rec") ++
      show hr

printLocals :: InterpFn
printLocals = do
  scope <- gets (gvar . cur)
  heap <- gets (heap . cur)
  IR.StrTable _ _ itos <- gets strtable
  flip traverse (M.assocs scope) $ \(local, ref) ->
    lift . outputStrLn $
    "_Local" ++ show local ++ " = " ++ showTerm itos heap ref
  continue

promptRetry :: InterpFn
promptRetry = do
  last <- gets (null . cho)
  if last
    then continue
    else promptRetry'

promptRetry' :: InterpFn
promptRetry' = do
  x <- lift $ getInputChar "? "
  case x of
    Just ';' -> backtrack
    _ -> continue

withArgs :: [Int] -> ([Int] -> InterpFn) -> InterpFn
withArgs as f = do
  scope <- gets (hvar . cur)
  if all (`M.member` scope) as
    then f $ map (scope M.!) as
    else prlgError "arguments not bound"

write' :: InterpFn -> InterpFn
write' c =
  withArgs [0] $ \[arg] -> do
    heap <- gets (heap . cur)
    IR.StrTable _ _ itos <- gets strtable
    lift . outputStr $ showTerm itos heap arg
    c --this now allows error fallthrough but we might like EitherT

write = write' continue

nl :: InterpFn
nl = do
  lift $ outputStrLn ""
  continue

writeln :: InterpFn
writeln = write' nl

assertFact :: (Code -> IR.Id -> PrlgEnv ()) -> InterpFn
assertFact addClause =
  withArgs [0] $ \[arg] -> do
    heap <- gets (heap . cur)
    case Co.compileGoal . Co.squashVars <$>
         Co.heapStructPrlgInt Nothing heap arg of
      Just (U (Struct s):head) -> do
        addClause (head ++ [NoGoal]) s
        continue
      _ -> prlgError "assert fact failure"

assertRule :: (Code -> IR.Id -> PrlgEnv ()) -> InterpFn
assertRule addClause =
  withArgs [0, 1] $ \args -> do
    scope <- gets (hvar . cur)
    heap <- gets (heap . cur)
    comma <- findAtom ","
    cut <- findAtom "!"
    case Co.squashVars . IR.CallI 0 <$>
         traverse (Co.heapStructPrlgInt Nothing heap) args of
      Just (IR.CallI 0 [hs, gs]) ->
        let (U (Struct s):cs) =
              Co.compileGoal hs ++ Co.seqGoals (Co.compileGoals comma cut gs)
         in do addClause cs s
               continue
      _ -> prlgError "assert clause failure"

retractall :: InterpFn
retractall =
  withArgs [0] $ \[arg] -> do
    heap <- gets (heap . cur)
    case derefHeap heap arg of
      BoundRef _ (Atom a) ->
        dropProcedure (IR.Id {IR.arity = 0, IR.str = a}) >> continue
      BoundRef _ (Struct id) -> dropProcedure id >> continue
      _ -> prlgError "retractall needs a struct"

call :: InterpFn
call =
  withArgs [0] $ \[arg] -> do
    heap@(Heap _ hmap) <- gets (heap . cur)
    let exec base struct arity = do
          cur <- gets cur
          modify $ \s ->
            s
              { cur =
                  cur
                    { gol =
                        [Call, Goal, U struct] ++
                        [U $ hmap M.! (base + i) | i <- [1 .. arity]] ++ gol cur
                    }
              }
          continue
    case derefHeap heap arg of
      BoundRef addr struct@(Struct IR.Id {IR.arity = arity}) ->
        exec addr struct arity
      BoundRef addr (Atom a) ->
        exec addr (Struct IR.Id {IR.arity = 0, IR.str = a}) 0
      _ -> prlgError "not callable"

exec :: InterpFn
exec =
  withArgs [0] $ \[arg] -> do
    heap <- gets (heap . cur)
    case Co.squashVars <$> Co.heapStructPrlgInt Nothing heap arg of
      Just gs -> do
        cur <- gets cur
        comma <- findAtom ","
        cut <- findAtom "!"
        modify $ \s ->
          s
            { cur =
                cur
                  { hvar = M.empty
                  , hed = Co.seqGoals (Co.compileGoals comma cut gs)
                  , gol = [LastCall]
                  }
            }
        continue
      _ -> prlgError "goal exec failure"

{- struct assembly/disassembly -}
struct :: InterpFn
struct = do
  heap <- gets (heap . cur)
  scope <- gets (hvar . cur)
  case derefHeap heap <$> scope M.!? 0 of
    Just (BoundRef addr (Struct IR.Id {IR.arity = arity, IR.str = str})) ->
      structUnify arity str
    _ -> structAssemble

heapListLength listAtom heap ref = go 0 ref ref (id, fromJust . step)
  where
    nil r
      | BoundRef _ str <- derefHeap heap r = str == Atom listAtom
      | otherwise = False
    step r
      | BoundRef addr (Struct IR.Id {IR.arity = 2, IR.str = listAtom'}) <-
         derefHeap heap r
      , listAtom == listAtom' = Just (addr + 2)
      | otherwise = Nothing
    go n fast slow (f1, f2)
      | nil fast = Just n
      | Just fast' <- step fast =
        if slow == fast'
          then Nothing
          else go (n + 1) fast' (f1 slow) (f2, f1)
      | otherwise = Nothing

structAssemble :: InterpFn
structAssemble = do
  heap <- gets (heap . cur)
  scope <- gets (hvar . cur)
  case derefHeap heap <$> scope M.!? 1 of
    Just (BoundRef addr (Atom str)) -> do
      listAtom <- findAtom "[]"
      case scope M.!? 2 >>= heapListLength listAtom heap of
        Just arity -> structUnify arity str
        _ -> prlgError "struct arity unknown"
    _ -> prlgError "struct id unknown"

structUnify arity str = do
  cur <- gets cur
  let h = heap cur
      scope = hvar cur
  listAtom <- findAtom "[]"
  let hcode = map U $ maybe VoidRef HeapRef . (scope M.!?) <$> [0 .. 2]
      (h', pvars) = newHeapVars arity h
      structData =
        Struct IR.Id {IR.arity = arity, IR.str = str} : map HeapRef pvars
      paramsData =
        concatMap
          (\pv -> [Struct IR.Id {IR.arity = 2, IR.str = listAtom}, HeapRef pv])
          pvars ++
        [Atom listAtom]
      gcode = map U $ structData ++ [Atom str] ++ paramsData
  modify $ \s ->
    s {cur = cur {heap = h', gol = gcode ++ gol cur, hed = hcode ++ hed cur}}
  continue

{- operator management -}
op :: InterpFn
op = do
  heap <- gets (heap . cur)
  scope <- gets (hvar . cur)
  IR.StrTable _ _ itos <- gets strtable
  case sequence $ map (fmap (derefHeap heap) . (scope M.!?)) [0 .. 2] of
    Just [BoundRef _ (Number prio), BoundRef _ (Atom fixityAtom), BoundRef _ (Atom opatom)]
      | Just op <-
         (,) <$> itos M.!? opatom <*>
         (O.Op prio <$> ((itos M.!? fixityAtom) >>= O.fixity)) -> do
        modify $ \s -> s {ops = op : ops s}
        continue
    _ -> prlgError "bad op spec"

stashOps :: InterpFn
stashOps = do
  currentOps <- gets ops
  modify $ \s -> s {opstash = currentOps : opstash s}
  continue

popOps :: InterpFn
popOps = do
  currentOps <- gets opstash
  case currentOps of
    [] -> prlgError "no op stash to pop"
    (ops':opss) -> do
      modify $ \s -> s {ops = ops', opstash = opss}
      continue

{- adding the builtins -}
addOp :: (String, O.Op) -> PrlgEnv ()
addOp op = modify $ \s -> s {ops = op : ops s}

modDef :: ([Code] -> Maybe [Code]) -> IR.Id -> PrlgEnv ()
modDef fn struct =
  modify $ \s -> s {defs = M.alter (maybe (fn []) fn) struct $ defs s}

addClauseA :: Code -> IR.Id -> PrlgEnv ()
addClauseA code = modDef $ Just . (code :)

addClauseZ :: Code -> IR.Id -> PrlgEnv ()
addClauseZ code = modDef $ Just . (++ [code])

addProcedure :: [Code] -> IR.Id -> PrlgEnv ()
addProcedure heads = modDef $ Just . const heads

dropProcedure :: IR.Id -> PrlgEnv ()
dropProcedure = modDef $ const Nothing

addProc :: [Code] -> String -> Int -> PrlgEnv ()
addProc c n a = findStruct n a >>= addProcedure c

addBi :: InterpFn -> String -> Int -> PrlgEnv ()
addBi b n a =
  addProc [[U (LocalRef $ r - 1) | r <- [1 .. a]] ++ [Invoke $ bi b]] n a

{- actual prlgude -}
addPrelude :: PrlgEnv ()
addPrelude = do
  pure undefined
  {- primitives -}
  addBi (pure Nothing) "true" 0
  addBi backtrack "fail" 0
  addOp $ O.xfx "=" 700
  addProc [[U (LocalRef 0), U (LocalRef 0), NoGoal]] "=" 2
  {- clauses -}
  addOp $ O.xfy "," 1000
  addOp $ O.xfx ":-" 1200
  addOp $ O.fx ":-" 1200
  horn1 <- findStruct ":-" 1
  horn2 <- findStruct ":-" 2
  let assertCode ac =
        [ [ U (Struct horn2)
          , U (LocalRef 0)
          , U (LocalRef 1)
          , Cut
          , Invoke . bi $ assertRule ac
          ]
        , [U (Struct horn1), U (LocalRef 0), Cut, Invoke $ bi exec]
        , [U (LocalRef 0), Invoke . bi $ assertFact ac]
        ]
   in do addProc (assertCode addClauseA) "asserta" 1
         addProc (assertCode addClauseZ) "assertz" 1
         addProc (assertCode addClauseZ) "assert" 1
  addBi retractall "retractall" 1
  addBi call "call" 1
  addBi struct "struct" 3
  {- operators -}
  addBi op "op" 3
  addBi stashOps "stash_operators" 0
  addBi popOps "pop_operators" 0
  {- query tools -}
  addBi printLocals "print_locals" 0
  addBi promptRetry' "prompt_retry" 0
  addBi (printLocals >> promptRetry) "query" 0
  {- IO -}
  addBi write "write" 1
  addBi writeln "writeln" 1
  addBi nl "nl" 0
  {- debug -}
  addBi (get >>= liftIO . print >> pure Nothing) "interpreter_trace" 0