67 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Haskell
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Haskell
		
	
	
	
	
	
| module Code where
 | |
| 
 | |
| import qualified Data.Map as M
 | |
| import IR (Id(..), StrTable)
 | |
| import Operators (Ops)
 | |
| 
 | |
| data Datum
 | |
|   = Atom Int -- unifies a constant
 | |
|   | Struct Id -- unifies a structure with arity
 | |
|   | VoidRef (Maybe Int) -- unifies with anything (references may refer to variable names)
 | |
|   | LocalRef Int (Maybe Int) -- code-local variable idx (should not occur on heap)
 | |
|   | HeapRef Int (Maybe Int) -- heap structure idx
 | |
|   deriving (Show, Eq, Ord)
 | |
| 
 | |
| data BuiltinFunc =
 | |
|   BuiltinFunc (Interp -> Interp)
 | |
| 
 | |
| instance Show BuiltinFunc where
 | |
|   show _ = "BuiltinFunc _"
 | |
| 
 | |
| data Instr
 | |
|   = U Datum -- something unifiable
 | |
|   | NoGoal -- trivial goal (directly after head)
 | |
|   | Builtin BuiltinFunc -- trivial goal (directly after head)
 | |
|   | Goal -- a new goal (set head)
 | |
|   | Call -- all seems okay, call the head's hoal
 | |
|   | LastCall -- tail call the head's 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
 | |
|     , cur :: Cho -- the choice that is being evaluated right now
 | |
|     , cho :: [Cho] -- remaining choice points
 | |
|     , ops :: Ops -- currently defined operators
 | |
|     , strtable :: StrTable -- string table
 | |
|     }
 | |
|   deriving (Show)
 |