From b9633a33182f5b381e912366273709e59f469bb9 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sat, 12 Nov 2022 17:47:51 +0100 Subject: reorg. --- app/Code.hs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/Code.hs (limited to 'app/Code.hs') diff --git a/app/Code.hs b/app/Code.hs new file mode 100644 index 0000000..3488f0b --- /dev/null +++ b/app/Code.hs @@ -0,0 +1,56 @@ +module Code where + +import qualified Data.Map as M +import IR (Id(..)) + +data Datum + = Atom Int -- unifies a constant + | Struct Id -- unifies a structure with arity + | VoidRef -- unifies with anything + | LocalRef Int -- code-local variable idx (should not occur on heap) + | HeapRef Int -- heap structure idx + deriving (Show, Eq, Ord) + +data Instr + = U Datum -- something unifiable + | NoGoal -- trivial goal + | Goal -- we start a new goal, set up backtracking etc + | Call -- all seems okay, call the goal + | LastCall -- tail call the goal + | Cut -- remove all alternative clauses of the current goal + deriving (Show) + +type Code = [Instr] + +type Defs = M.Map Id [Code] + +data Heap = + Heap Int (M.Map Int Datum) + deriving (Show) + +emptyHeap = Heap 0 M.empty + +type Scope = M.Map Int Int + +emptyScope :: Scope +emptyScope = M.empty + +data Cho = + Cho + { hed :: Code -- head pointer + , hvar :: Scope -- variables unified in head (so far) + , gol :: Code -- goal pointer + , gvar :: Scope -- variables unified in the goal + , heap :: Heap -- a snapshot of the heap (there's no trail; we rely on CoW copies in other choicepoints) + , stk :: [(Code, Scope, [Cho])] -- remaining goals together with their vars and cuts + , cut :: [Cho] -- snapshot of choicepoints before entering + } + deriving (Show) + +data Interp = + Interp + { defs :: Defs -- global definitions for lookup (TODO can we externalize?) + , cur :: Cho -- the choice that is being evaluated right now + , cho :: [Cho] -- remaining choice points + } + deriving (Show) -- cgit v1.2.3