1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
module Heap where
import Data.FiniteMap
import FlatCurry.Annotated.Pretty (ppExp, ppVarIndex)
import FlatCurry.Annotated.Types
import Text.Pretty
import FlatCurryGoodies (TypeAnn)
import Substitution
data Binding = BlackHole
| BoundVar (AExpr TypeAnn)
| LazyBound (AExpr TypeAnn)
| FreeVar
| LazyFree
| SymArg
deriving (Eq, Show)
type Heap = FM VarIndex Binding
emptyH :: Heap
emptyH = emptyFM (<)
fromListH :: [(VarIndex, Binding)] -> Heap
fromListH = listToFM (<)
isEmptyH :: Heap -> Bool
isEmptyH = isEmptyFM
elemH :: VarIndex -> Heap -> Bool
elemH = elemFM
lookupH :: VarIndex -> Heap -> Maybe Binding
lookupH = flip lookupFM
bindH :: VarIndex -> Binding -> Heap -> Heap
bindH v b h = addToFM h v b
bindHole :: VarIndex -> Heap -> Heap
bindHole v = bindH v BlackHole
bindExpr :: VarIndex -> AExpr TypeAnn -> Heap -> Heap
bindExpr v e = bindH v (BoundVar e)
bindLazyExpr :: VarIndex -> AExpr TypeAnn -> Heap -> Heap
bindLazyExpr v e = bindH v (LazyBound e)
bindFree :: VarIndex -> Heap -> Heap
bindFree v = bindH v FreeVar
bindLazyFree :: VarIndex -> Heap -> Heap
bindLazyFree v = bindH v LazyFree
bindSym :: VarIndex -> Heap -> Heap
bindSym v = bindH v SymArg
unbind :: VarIndex -> Heap -> Heap
unbind = flip delFromFM
instance Pretty Binding where
pretty BlackHole = text "\9632"
pretty (BoundVar e) = ppExp e
pretty (LazyBound e) = text "~" <> ppExp e
pretty FreeVar = text "free"
pretty LazyFree = text "~free"
pretty SymArg = text "sym"
ppHeap :: Heap -> Doc
ppHeap h = ppHeap' $ fmToList h
where
ppHeap' [] = text "[]"
ppHeap' heap@(_:_) = listSpaced $ map ppBinding heap
where ppBinding (i, b) = ppVarIndex i <+> char '\8614' <+> pretty b
|