summaryrefslogtreecommitdiff
path: root/app/Builtins.hs
blob: 0cdd7cdd343705484c4a416fcf225aa5d6ccdd8a (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
module Builtins where

import Code
  ( Builtin(..)
  , Cho(..)
  , Datum(..)
  , Dereferenced(..)
  , Heap(..)
  , Instr(..)
  , Interp(..)
  , InterpFn
  , InterpFn
  , derefHeap
  , heapStruct
  )
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 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

showTerm itos heap = runIdentity . heapStruct atom struct hrec heap
  where
    atom (Atom a) = pure $ itos M.! a
    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.elems scope) $ \(ref, name) ->
    lift . outputStrLn $
    (maybe "_" id $ itos M.!? name) ++ " = " ++ showTerm itos heap ref
  return Nothing

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

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

write :: InterpFn
write
  --TODO: prlgError on write(Unbound)
 = do
  scope <- gets (hvar . cur)
  heap <- gets (heap . cur)
  IR.StrTable _ _ itos <- gets strtable
  lift . outputStr . showTerm itos heap . fst $ scope M.! 0
  return Nothing

nl :: InterpFn
nl = do
  lift $ outputStrLn ""
  return Nothing

writeln :: InterpFn
writeln = write >> nl

assertFact :: InterpFn
assertFact = do
  scope <- gets (hvar . cur)
  heap <- gets (heap . cur)
  case Co.compileGoal . Co.squashVars <$>
       Co.heapStructPrlgInt Nothing heap (fst $ scope M.! 0) of
    Just (U (Struct s):head) -> do
      addClause s $ head ++ [NoGoal]
      return Nothing
    _ -> prlgError "assert fact failure"

assertClause :: InterpFn
assertClause = do
  scope <- gets (hvar . cur)
  heap <- gets (heap . cur)
  commaId <- findStruct "," 2
  cut <- findAtom "!"
  case Co.squashVars . IR.CallI (IR.Id 0 0) <$>
       traverse (Co.heapStructPrlgInt Nothing heap . fst . (M.!) scope) [0, 1] of
    Just (IR.CallI (IR.Id 0 0) [hs, gs]) ->
      let (U (Struct s):cs) =
            Co.compileGoal hs ++ Co.seqGoals (Co.compileGoals commaId cut gs)
       in do addClause s cs
             return Nothing
    _ -> prlgError "assert clause failure"

retractall :: InterpFn
retractall = prlgError "no retractall yet"

call :: InterpFn
call = do
  ref <- gets (fst . (M.! 0) . hvar . cur)
  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
                  }
            }
        return Nothing
  case derefHeap heap ref 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"

{- adding the builtins -}
addOp op = modify $ \s -> s {ops = op : ops s}

addClause struct code =
  modify $ \s ->
    s {defs = M.alter (Just . maybe [code] (\hs -> code : hs)) struct $ defs s}

addProcedure struct heads =
  modify $ \s -> s {defs = M.insert struct heads $ defs s}

addProc n a c = do
  sym <- findStruct n a
  addProcedure sym c

addBi0 n b = addProc n 0 [[Invoke $ bi b]]

addPrelude :: PrlgEnv ()
addPrelude = do
  pure undefined
  {- primitives -}
  addBi0 "true" (pure Nothing)
  addBi0 "fail" backtrack
  addOp $ O.xfx "=" 700
  addProc "=" 2 [[U (LocalRef 0 0), U (LocalRef 0 0), NoGoal]]
  {- clauses -}
  addOp $ O.xfy "," 1000
  addOp $ O.xfx ":-" 1200
  horn2 <- findStruct ":-" 2
  --addOp $ O.fx ":-" 1200
  addProc
    "assert"
    1
    [ [ U (Struct horn2)
      , U (LocalRef 0 0)
      , U (LocalRef 1 0)
      , Cut
      , Invoke (bi assertClause)
      ]
    , [U (LocalRef 0 0), Invoke (bi assertFact)]
    ]
  addProc "retractall" 1 [[U (LocalRef 0 0), Invoke (bi retractall)]]
  addProc "call" 1 [[U (LocalRef 0 0), Invoke (bi call)]]
  {- query tools -}
  addBi0 "print_locals" printLocals
  addBi0 "prompt_retry" promptRetry'
  addBi0 "query" (printLocals >> promptRetry)
  {- IO -}
  addProc "write" 1 [[U (LocalRef 0 0), Invoke (bi write)]]
  addProc "writeln" 1 [[U (LocalRef 0 0), Invoke (bi writeln)]]
  addBi0 "nl" nl
  {- debug -}
  addBi0 "interpreter_trace" (get >>= liftIO . print >> pure Nothing)