# AsmGofer Prelude

The prelude description is not yet complete. ### Lists

First element of the list
`  head :: [a] -> a`
Last element of the list
`  last :: [a] -> a`
All elements except the first one
` tail :: [a] -> [a]`
All elements except the last one
`  init :: [a] -> [a]`
Length of a list
`  length :: [a] -> Int`

### Sets

```instance Eq [a] => Eq {a} where
Set xs == Set ys = (xs == ys)

instance Ord [a] => Ord {a} ...
```
Conversion from list to set
`  mkSet :: Ord a => [a] -> {a}`
Map function for sets
```  setmap :: Ord b => (a -> b) -> {a} -> {b}
setmap f xs = { f(x) | x <-: xs }
```
Combining sets
```  union      :: Ord a => {a} -> {a} -> {a}
bigUnion   :: Ord a => {{a}} -> {a}
intersect  :: Ord a => {a} -> {a} -> {a}
difference :: Ord a => {a} -> {a} -> {a}
```
Test for empty set
`  empty :: {a} -> Bool`
Determine cardinality of a set
`  card :: {a} -> Int`
Get one arbitrary element of a set. The function can only be applied to non empty sets.
`  one :: {a} -> a`
Fold on non empty sets
`  fold1 :: (a -> a -> a) -> {a} -> a`
Convertion from set to list
`  expr2list :: {a} -> [a]`

### Gofer extensions for ASMs

Fixity declaration
`  infixl 0 :=`
Skip rule
`  skip :: Rule ()`
Firing rules
```  fire1     :: Rule () -> IO ()
fire      :: Int -> Rule () -> IO ()
fireWhile :: Bool -> Rule () -> IO ()
fireUntil :: Bool -> Rule () -> IO ()
fixpoint  :: Rule () -> IO ()
```
Putting trace information in a rule. The first parameter is the trace action.
```  trace :: IO () -> Rule () -> Rule ()
```
Creating dynamic functions
```  initVal       :: (Eq a, AsmTerm a) => String -> a -> Dynamic a
initValStdin  :: String -> Dynamic String
initValStdout :: Bool -> String -> Dynamic String
initAssocs    :: (AsmTerm key, AsmTerm val, AsmOrd key, Eq val) =>
String -> [(key,val)] -> Dynamic (key -> val)
```
Predefined dynamic functions
```  stdout :: Dynamic String
stdout = initValStdout False "stdout"

stdin  :: Dynamic String
stdin  = initValStdin "stdin"
```
Accessing information stored in dynamic functions
```  assocs :: Dynamic (key -> val) -> [(key,val)]
assocs = primAsmAssocs

dom :: Dynamic (key -> val) -> {key}
dom = mkSet . map fst . assocs

ran :: Dynamic (key -> val) -> {val}
ran = mkSet . map snd . assocs

inDom :: key -> Dynamic (key -> val) -> Bool
inDom = primAsmInDom

notInDom :: key -> Dynamic (key -> val) -> Bool
notInDom key fun = not(key `inDom` fun)
```
Datatypes and instances
```  data Rule a = Skip | Rule ...

instance Functor Rule where
map f (Rule m) = Rule (map f m)
map f Skip     = Skip

result = Rule . result
Rule m `bind` f = Rule (m `bind` (\x -> let Rule r = f x in r))
Skip `bind` f   = f (error "skip does not return a value")

zero = Skip

a ++ b = do a; b

class AsmOrd a where
asmCompare :: a -> a -> Int

class AsmTerm a where
(:=) :: Dynamic a -> a -> Rule ()
asmDefault :: a
asmDefault = primAsmDefault ()

instance AsmTerm Bool where
asmDefault = False

instance AsmTerm Int
instance AsmTerm Float
instance AsmTerm Char
instance AsmTerm String
instance AsmTerm a => AsmTerm [a]
instance (AsmTerm a, AsmTerm b) => AsmTerm (a,b)
instance (AsmTerm a, AsmTerm b, AsmTerm c) => AsmTerm (a,b, c)
instance (AsmTerm a, AsmTerm b, AsmTerm c, AsmTerm d) => AsmTerm (a,b, c, d)
instance AsmTerm a => Eq a where
(==) = genericEq
instance AsmTerm a => AsmOrd a where
asmCompare = genericCmp
```

### Combining rules (only version 1.1)

Sequential execution of rules
```  seq :: Rule () -> Rule () -> Rule ()
```
The rule x := 1 `seq` x := 2 is equivalent to x := 2, because the second rule overwrites the value of x assigned by the first rule. On the other side, the rule x := 1 `seq` x := x + 1 is equivalent to x := 2, because the second rule uses the value of x assigned by the first rule.

Iteration of a rule until a fixpoint is reached (no further updates)
```  iterate :: Rule () -> Rule ()
```
There are some nice features
• iterate skip is equivalent to skip
• iterate (if cond then r else skip behaves like the normal while loop.
• iterate (x := 1) is a never ending computation (update set is always non-empty).

### Multi agent ASMs (only version 1.1)

Predefined dynamic function holding active agents
```  agents :: Dynamic (Agent -> Bool)
```
Randomly choosing one rule to fire.
```  multi :: Dynamic (a -> (a -> Rule ())) -> Rule ()
```
Let acts be a dynamic function from type a to type a -> Rule (). The type a could be an agent. If x is an element of type a in the domain of acts then acts(x) is a function from type a to type Rule ().
The expression multi acts chooses one x in the domain of acts and returns rule (acts x) x.