summaryrefslogtreecommitdiff
path: root/app/Interpreter.hs
diff options
context:
space:
mode:
authorMirek Kratochvil <exa.exa@gmail.com>2022-11-12 18:45:13 +0100
committerMirek Kratochvil <exa.exa@gmail.com>2022-11-12 18:45:13 +0100
commita736c1e7b727876b0b05f0b413e2c914437df13a (patch)
treef625bc8f0b5f25b5c88057f8681b495aaabc0f46 /app/Interpreter.hs
parentb9633a33182f5b381e912366273709e59f469bb9 (diff)
downloadprlg-a736c1e7b727876b0b05f0b413e2c914437df13a.tar.gz
prlg-a736c1e7b727876b0b05f0b413e2c914437df13a.tar.bz2
compiled vars carry ids
Diffstat (limited to 'app/Interpreter.hs')
-rw-r--r--app/Interpreter.hs71
1 files changed, 44 insertions, 27 deletions
diff --git a/app/Interpreter.hs b/app/Interpreter.hs
index c29b701..da00301 100644
--- a/app/Interpreter.hs
+++ b/app/Interpreter.hs
@@ -63,7 +63,7 @@ proveStep c f i = go i
{- heap tools -}
deref x =
case hmap M.!? x of
- Just (HeapRef x') ->
+ Just (HeapRef x' _) ->
if x == x'
then FreeRef x'
else deref x'
@@ -74,7 +74,7 @@ proveStep c f i = go i
newHeapVars n (Heap nxt m) =
let addrs = [nxt + i - 1 | i <- [1 .. n]]
in ( Heap (nxt + n) $
- foldr (uncurry M.insert) m $ zip addrs (map HeapRef addrs)
+ foldr (uncurry M.insert) m [(a, HeapRef a Nothing) | a <- addrs]
, addrs)
allocLocal reg scope cont
| Just addr <- scope M.!? reg = cont scope heap addr
@@ -83,47 +83,62 @@ proveStep c f i = go i
newHeapStruct addr s@(Struct Id {arity = arity}) cont =
let (Heap nxt' m', addrs) = newHeapVars (arity + 1) heap
m'' =
- M.insert addr (HeapRef $ head addrs) . M.insert (head addrs) s $
+ M.insert addr (HeapRef (head addrs) Nothing) .
+ M.insert (head addrs) s $
m'
- in cont (map HeapRef $ tail addrs) (Heap nxt' m'')
+ in cont [HeapRef a Nothing | a <- tail addrs] (Heap nxt' m'')
{- simple cases first -}
- unify VoidRef VoidRef = uok
+ unify (VoidRef _) (VoidRef _) = uok
unify (Atom a) (Atom b)
| a == b = uok
- unify VoidRef (Atom _) = uok
- unify (Atom _) VoidRef = uok
+ unify (VoidRef _) (Atom _) = uok
+ unify (Atom _) (VoidRef _) = uok
unify (Struct a) (Struct b)
| a == b = uok
{- unifying a struct with void must cause us to skip the void -}
- unify VoidRef (Struct Id {arity = a}) =
- c i {cur = cur {hed = replicate a (U VoidRef) ++ hs, gol = gs}}
- unify (Struct Id {arity = a}) VoidRef =
- c i {cur = cur {hed = hs, gol = replicate a (U VoidRef) ++ gs}}
+ unify (VoidRef _) (Struct Id {arity = a}) =
+ c
+ i
+ { cur =
+ cur {hed = replicate a (U $ VoidRef Nothing) ++ hs, gol = gs}
+ }
+ unify (Struct Id {arity = a}) (VoidRef _) =
+ c
+ i
+ { cur =
+ cur {hed = hs, gol = replicate a (U $ VoidRef Nothing) ++ gs}
+ }
{- handle local refs; first ignore their combination with voids to save memory -}
- unify (LocalRef _) VoidRef = uok
- unify VoidRef (LocalRef _) = uok
+ unify (LocalRef _ _) (VoidRef _) = uok
+ unify (VoidRef _) (LocalRef _ _) = uok
{- allocate heap for LocalRefs and retry with HeapRefs -}
- unify (LocalRef hv) _ =
+ unify (LocalRef hv ident) _ =
allocLocal hv (hvar cur) $ \hvar' heap' addr ->
c
i
{ cur =
cur
- {hed = U (HeapRef addr) : hs, hvar = hvar', heap = heap'}
+ { hed = U (HeapRef addr ident) : hs
+ , hvar = hvar'
+ , heap = heap'
+ }
}
- unify _ (LocalRef gv) =
+ unify _ (LocalRef gv ident) =
allocLocal gv (gvar cur) $ \gvar' heap' addr ->
c
i
{ cur =
cur
- {gol = U (HeapRef addr) : gs, gvar = gvar', heap = heap'}
+ { gol = U (HeapRef addr ident) : gs
+ , gvar = gvar'
+ , heap = heap'
+ }
}
{- handle heap refs; first ignore their combination with voids again -}
- unify (HeapRef _) VoidRef = uok
- unify VoidRef (HeapRef _) = uok
- {- actual HeapRefs, these are dereferenced and then unified; decide between copying and linking -}
- unify (HeapRef hr') g =
+ unify (HeapRef _ _) (VoidRef _) = uok
+ unify (VoidRef _) (HeapRef _ _) = uok
+ {- actual HeapRefs, these are dereferenced and then unified (sometimes with copying) -}
+ unify (HeapRef hr' hident) g =
case deref hr' of
FreeRef hr ->
case g of
@@ -139,10 +154,10 @@ proveStep c f i = go i
cur
{hed = map U nhs ++ hs, gol = gs, heap = nheap}
})
- HeapRef gr' ->
+ HeapRef gr' _ ->
case deref gr' of
- FreeRef gr -> setHeap hr (HeapRef gr)
- BoundRef addr _ -> setHeap hr (HeapRef addr)
+ FreeRef gr -> setHeap hr (HeapRef gr hident)
+ BoundRef addr _ -> setHeap hr (HeapRef addr hident)
_ -> ifail "dangling goal ref (from head ref)"
BoundRef _ atom@(Atom a) -> unify atom g
BoundRef addr struct@(Struct Id {arity = arity}) ->
@@ -152,12 +167,13 @@ proveStep c f i = go i
cur
{ hed =
U struct :
- [U (HeapRef $ addr + i) | i <- [1 .. arity]] ++ hs
+ [U (HeapRef (addr + i) Nothing) | i <- [1 .. arity]] ++
+ hs
, gol = U g : gs
}
}
_ -> ifail "dangling head ref"
- unify h (HeapRef gr') =
+ unify h (HeapRef gr' gident) =
case deref gr' of
FreeRef gr ->
case h of
@@ -182,7 +198,8 @@ proveStep c f i = go i
{ hed = U h : hs
, gol =
U struct :
- [U (HeapRef $ addr + i) | i <- [1 .. arity]] ++ gs
+ [U (HeapRef (addr + i) Nothing) | i <- [1 .. arity]] ++
+ gs
}
}
_ -> ifail "dangling goal ref"