(* Time-stamp: Formalisation of the HAM. *) header {* The HAM *} (*<*) theory VM = Basics + Lang: (*>*) subsection {* Values *} (* translations "SVal" == "Ref" *) subsection {* Stack *} text {* Stack values must be references, never literals *} types SVal = Ref (* stuff = nat list *) text {* Frame record, containing return address, the arguments (just above FP), local variables (just below FP), and the expression stack as values. Note that we abstract over the exact location of args, locals and vals, and only model the order within each of these 3 components. We don't need an explicit FP or link field, since this is implicit in the modelling of the stack as a list of frames. *} record Frame = ret :: LabName args :: "SVal list" locals :: "SVal list" vals :: "SVal list" types Stack = "Frame list" syntax emptyStack :: "Stack" translations "emptyStack" == "[]" translations "Stack" <= (type) "\ret :: char list, args :: Ref list, locals :: Ref list, vals :: Ref list\ list" "Frame" <= (type) "\ret :: char list, args :: Ref list, locals :: Ref list, vals :: Ref list\" subsection {* Heap *} text {* The heap is a finite map. Might be worthwhile switching to a more recent version of Isabelle to get a better finite map implementation. *} types Heap = "Locn \\<^sub>f HVal" LabTable = "LabName \ Instr list" FunTable = "FunName \ Instr list" RuleSet = "nat \\<^sub>f Instr list" RuleTable = "BoxName \ RuleSet" Stream = "int \ HVal" ExnTable = "BoxName \ int \ Instr list" text {* An IO record collects all state info, not recorded in stack or heap. These are mainly the wires, but also some pointers into the stack *} types Wire = nat record IO = (* ins :: Stream *) (* outs :: Stream *) (* avb :: "int \ bool" *) bname :: BoxName (* bon :: Wire *) blocked :: bool crp :: nat (* current rule pointer *) rp :: nat (* rule pointer *) inp :: Wire (* input pointer *) mp :: nat (* match pointer *) (* mb :: "SVal list" match buffer; only needed if no mp is present *) (* State = Heap \ Stack \ RuleSet \ IO *) translations "Stack" <= (type) "\ret :: LabName, args :: Ref list, locals :: Ref list, vals :: Ref list\ list" "Heap" <= (type) "nat \\<^sub>f HVal" "IO" <= (type) "\bname :: BoxName, blocked :: bool, crp :: nat, rp :: nat, inp :: nat, mp :: nat\" "LabTable" <= (type) "LabName \ Instr list" "FunTable" <= (type) "FunName \ Instr list" "RuleSet" <= (type) "nat \\<^sub>f Instr list" subsection {* Global Tables *} text {* We use these global tables that should come out of the compilation of Hume code. They never change and therefore don't need to be part of the state. *} consts LabTab :: LabTable FunTab :: FunTable RuleTab :: RuleTable (* base ruleset *) ExnTab :: ExnTable (* table of exception handlers; exceptions tagged by ints *) subsection {* Aux definitions *} (* State = Stack x Env x Heap *) subsection {* Stack operations *} text {* The usual operations on a stack. push and pop work on the vals component of the top frame. *} constdefs pushS :: "Stack \ SVal \ Stack" "pushS s v == let fr = hd s in \ ret=(ret fr), args=(args fr), locals=(locals fr), vals=v#(vals fr) \#(tl s) " (* "push s v == ((hd s)\ ret=ret fr, args=args fr, locals=locals fr vals=(v#(hd s).vals) \)#(tl s)" *) text {* Put N dummy values on top of the stack *} consts pushN :: "nat \ Stack \ Stack" primrec "pushN 0 s = s" "pushN (Suc n) s = pushN n (let fr = hd s in \ ret=(ret fr), args=(args fr), locals=(locals fr), vals=(Nullref#(vals fr)) \#(tl s))" constdefs popS :: "Stack \ Stack" "popS s == let fr = hd s in \ ret=(ret fr), args=(args fr), locals=(locals fr), vals=tl(vals fr) \#(tl s)" consts popN :: "nat \ Stack \ Stack" primrec "popN 0 s = s" "popN (Suc n) s = popN n (popS s)" text {* Remove top N elements, but keeping the top stack element *} consts slideN :: "nat \ Stack \ SVal \ Stack" primrec "slideN 0 s v = pushS s v" "slideN (Suc n) s v = slideN n (((hd s)\ vals:=tl(vals (hd s)) \)#(tl s)) v" constdefs topS :: "Stack \ SVal" "topS s == hd (vals (hd s))" constdefs takeS :: "nat \ Stack \ SVal list" "takeS n s == take n (vals (hd s))" constdefs argN :: "nat \ Stack \ SVal" "argN n s == nth (args (hd s)) n" constdefs varN :: "nat \ Stack \ SVal" "varN n s == nth (vals (hd s)) n" consts setVarN :: "nat \ Stack \ Stack" primrec "setVarN 0 s = ((hd s)\ vals:=(topS s)#(tl (vals (hd s))) \)#(tl s)" "setVarN (Suc n) s = ((hd s)\ vals:=(take n (vals (hd s)))@((topS s)#(drop (Suc n) (vals (hd s)))) \)#(tl s)" consts unpackTuple' :: "Ref list \ nat \ Stack \ Stack" primrec "unpackTuple' [] m s = s" "unpackTuple' (x#xs) m s = (unpackTuple' xs m (pushS s x))" constdefs unpackTuple :: "nat \ Ref list \ Stack \ Stack" "unpackTuple m xs s == unpackTuple' xs m s" (* constdefs unpackCon :: "nat \ nat \ Ref list \ Stack \ Stack" "unpackCon m xs s == unpackCon' m m xs s" constdefs unpackCon' :: "nat \ nat \ Ref list \ Stack \ Stack" primrec "unpackCon' 0 m xs s == s" "unpackCon' n m (x#xs) s == unpackTuple (n-1) m xs (pushS s x)" *) subsection {* Basic heap operations *} constdefs mkBool :: "Heap \ Locn \ bool \ Heap" "mkBool h l b == h(l \\<^sub>f (B b))" constdefs mkInt :: "Heap \ Locn \ int \ Heap" "mkInt h l i == h(l \\<^sub>f (I i))" (* constdefs mkChar :: "Heap \ Locn \ string \ Heap" "mkChar h l c == h(l \\<^sub>f (Y c))" constdefs mkString :: "Heap \ Locn \ string \ Heap" "mkString h l s == h(l \\<^sub>f (S s))" constdefs mkTuple :: "Heap \ Locn \ int \ SVal list \ Heap" "mkTuple h l i xs == h(l \\<^sub>f (T i xs))" *) constdefs mkCon :: "ConName \ Heap \ Locn \ SVal list \ Heap" "mkCon c h l xs == h(l \\<^sub>f (C c (int (length xs)) xs))" (* constdefs mkFun :: "Heap \ Locn \ LabName \ int \ int \ SVal list \ Heap" "mkFun h l f m n xs == h(l \\<^sub>f (F f m n xs))" constdefs mkExn :: "Heap \ Locn \ LabName \ int \ Heap" "mkExn h l f i == h(l \\<^sub>f (X f i))" constdefs mkNone :: "Heap \ Locn \ Heap" "mkNone h l == h(l \\<^sub>f N)" *) consts mkHVal :: "HVal \ Heap \ Locn \ Heap" primrec "mkHVal (I x) h l = h(l \\<^sub>f (I x))" "mkHVal (B x) h l = h(l \\<^sub>f (B x))" "mkHVal (Y x) h l = h(l \\<^sub>f (Y x))" "mkHVal (S x) h l = h(l \\<^sub>f (S x))" "mkHVal (R x) h l = h(l \\<^sub>f (R x))" "mkHVal (T i xs) h l = h(l \\<^sub>f (T i xs))" "mkHVal (C i j xs) h l = h(l \\<^sub>f (C i j xs))" (* "mkHVal (F f m n xs) h l = h(l \\<^sub>f (F f m n xs))" "mkHVal (X f i) h l = h(l \\<^sub>f (X f i))" "mkHVal (N) h l = h(l \\<^sub>f (N))" *) constdefs newFrame :: "Stack \ LabName \ LabName \ Stack" "newFrame s l f == \ ret=l, args=[], locals=[], vals=[] \#s" constdefs oldFrame :: "Stack \ Stack" "oldFrame s == pushS (tl s) (topS s)" (* TODO: check this with draft paper *) constdefs modifyFrame :: "Stack \ IO \ LabName \ LabName \ nat \ nat \ nat \ (Stack \ IO)" "modifyFrame s io c f n d z == (let s' = (take ((length s) - d) s) in ( ((hd s')\ vals := vals (hd s) \)#(tl s') , io\ rp := 0 \))" subsection {* Manipulating rule-pointer and other stuff in the IO record *} (* constdefs writeW :: "IO \ int \ HVal \ IO" "writeW io n x == io\ ins := (ins io)(n := x) \" constdefs readW :: "IO \ int \ HVal" "readW io n == ((ins io) n)" *) constdefs nextRule :: "RuleSet \ IO \ Instr list" "nextRule rs io == case (fmap_lookup rs (rp io)) of None \ [] | (Some cs) \ cs" constdefs currentRuleIdx :: "IO \ nat" "currentRuleIdx io == crp io" constdefs incRP :: "RuleSet \ IO \ IO" "incRP rs io == if ((rp io)+1 \ fmap_dom rs) then io\ crp := (rp io), rp := (rp io)+1 \ else io\ crp := (rp io), rp := 0 \" (* TODO: model reordering as 2 queues in the IO record *) consts reorderRules :: "IO \ IO" constdefs currArg :: "Stack \ IO \ SVal" "currArg s io == nth (args (hd s)) (mp io)" subsection {* System *} text {* Here is the design rationale for the components in the HAM on system level. The entire system is modelled as a triple \[ (bs,bm,wm) \] where \begin{itemize} \item $bs$ is a list of box names, which still need to be processed in the current superstep (one round of executing, if possible, every box); \item $bm$ a mapping (as total function for ease of use) of boxnames to box states \item $wm$ a mapping of wirenames to wire states. \end{itemize} Wire names are uniquely generated out of box names and wire indices, using the function $\mbox{wname}$. \paragraph{BoxState}: Since we will use a small-step semantics, the state of a box contains the code that still has to be executed. The other components are of course, stack and heap, and the IO record. The latter only contains a few pointers, mainly those neeeded for fair matching and for doing parameter matching. \paragraph{Wiring}: The wiring matches the $n$-th output wire of the current box, to a pair $(b,w)$ for box name and input wire, to which it is connected. \paragraph{Box}: The static information of a Box consists of its RuleSet and the Wiring. Note that the static components, $\mbox{BoxTab}$, $\mbox{Box}$, $\mbox{Wiring}$ can be generated automatically during compilation. *} types Wiring = "Wire => (BName \ Wire)" Box = "(RuleSet \ Wiring)" WState = "HVal option" BState = "Stack \ Heap \ IO \ Instr list" System = "BName list \ (BName \ BState) \ (WName \ WState)" consts BoxTab :: "BName \\<^sub>f Box" translations "BState" <= (type) "(Stack \ Heap \ IO \ Instr list)" "System" <= (type) "(BName list \ (BName \ Stack \ Heap \ IO \ Instr list) \ (WName \ HVal option))" text {* NeededIn maps each rule in the ruleset to the set of needed inputs, ie. a set of wires on which input must be available for the rule to fire. NeededOut maps each rule in the ruleset to the set of needed inputs, ie. a set of wires to which this rule will write. *} consts NeededIn :: "RuleSet \ Wire \ Wire set" consts NeededOut :: "RuleSet \ Wire \ Wire set" consts theBx :: "Box option \ Box" primrec "theBx (Some b) = b" syntax link :: "BName \ Wire \ (BName \ Wire)" translations "link b w" == "(snd (theBx (fmap_lookup BoxTab b))) w" text {* Generates a unique name for a wire based on the box names and wire indices of both ends. *} (* needs to be commutative *) constdefs wname :: "BName \ Wire \ WName" "wname b w == case b of BN str \ case (link b w) of (b',w') \ case b' of BN str' \ WN (str@str')" text {* Check availability of input data on an input wire. *} constdefs avb :: "(WName \ WState) \ WName \ bool" "avb wm w == case (wm w) of None \ False | (Some x) \ True" constdefs avb' :: "IO \ Wire \ System \ bool" "avb' io w sys == (case ((thd3 sys) (wname (bname io) w)) of None \ False | (Some x) \ True)" constdefs wval :: "IO \ Wire \ System \ HVal" "wval io w sys == (case ((thd3 sys) (wname (bname io) w)) of None \ R Nullref | (Some x) \ x)" text {* This predicate says that all output wires, needed by the current rule, are free. This could be merged with restartable! Note that this predicate specifies what needs to be checked in CheckOutputs. *} constdefs outputable :: "IO \ (WName \ WState) \ bool" "outputable io wm == (\ w. w \ (NeededOut (RuleTab (bname io)) (currentRuleIdx io)) \ avb wm (wname (bname io) w))" constdefs blockBox :: "IO \ IO" "blockBox io == io\ blocked := True \" text {* A box is runnable, if it is not marked as blocked, is not finished already ie. still has some code in its state, and if it has at least 1 rule for which inputs are available on all needed input wires. Note that this predicate specifies what needs to be checked in the main scheduling loop. *} constdefs runnable :: "System \ BState \ bool" "runnable sy b == (case b of (s,h,io,cs) => (\(blocked io) \ \(null cs) \ (\ r. r \ fmap_dom (RuleTab (bname io)) \ (\ w. w \ (NeededIn (RuleTab (bname io)) r) \ avb' io w sy))))" (* \ (\ w. w \ (NeededOut (RuleTab (bname io)) r) \ avb' io w sy))))"*) text {* A box is restartable if it is blocked now and all output wires are free. *} constdefs restartable :: "System \ BState \ bool" "restartable sy b == (case b of (s,h,io,cs) => ((blocked io) \ (\ w. w \ (NeededOut (RuleTab (bname io)) (currentRuleIdx io)) \ (avb' io w sy))))" (*<*) (* subsection {* Resources *} types Wiring = "Wire => (BName \ Wire)" Box = "(Instr list \ Wiring)" BoxState = "(BName \ Stack \ Heap \ RuleSet \ IO)" System = "BoxState set" *) (* This version is used only for the small-step semantics *) (* types SSWiring = "Wire => (BName \ Wire)" SSBox = "(Instr list \ Wiring)" SSBoxState = "(BName \ Stack \ Heap \ RuleSet \ IO \ Instr list)" SSSystem = "BoxState set" *) end (*>*)