|
1 |
| -module Circuit.Language.Expr where |
| 1 | +module Circuit.Language.Expr |
| 2 | + ( Expr(..), |
| 3 | + UVar(..), |
| 4 | + BinOp(..), |
| 5 | + UnOp(..), |
| 6 | + unType, |
| 7 | + Hash(..), |
| 8 | + hashCons, |
| 9 | + getAnnotation, |
| 10 | + ) where |
2 | 11 |
|
3 | 12 | import Circuit.Language.TExpr qualified as TExpr
|
4 | 13 | import Data.Vector qualified as V
|
@@ -77,52 +86,56 @@ getAnnotation = \case
|
77 | 86 | EUpdateIndex a _ _ _ -> a
|
78 | 87 | EBundle a _ -> a
|
79 | 88 |
|
80 |
| -hashCons :: (Hashable i, Hashable f) => Expr () i f -> Expr Int i f |
| 89 | +newtype Hash = Hash Int |
| 90 | + deriving (Show, Eq, Ord) |
| 91 | + deriving (Hashable) via Int |
| 92 | + |
| 93 | +hashCons :: (Hashable i, Hashable f) => Expr () i f -> Expr Hash i f |
81 | 94 | hashCons = \case
|
82 | 95 | EVal _ f ->
|
83 |
| - let i = hash (hash @Text "EVal", f) |
| 96 | + let i = Hash $ hash (hash @Text "EVal", f) |
84 | 97 | in EVal i f
|
85 | 98 | EVar _ v ->
|
86 |
| - let i = hash (hash @Text "EVar", v) |
| 99 | + let i = Hash $ hash (hash @Text "EVar", v) |
87 | 100 | in EVar i v
|
88 | 101 | EUnOp _ op e ->
|
89 | 102 | let e' = hashCons e
|
90 |
| - i = hash (hash @Text "EUnOp", op, getAnnotation e') |
| 103 | + i = Hash $ hash (hash @Text "EUnOp", op, getAnnotation e') |
91 | 104 | in EUnOp i op e'
|
92 | 105 | EBinOp _ op e1 e2 ->
|
93 | 106 | let e1' = hashCons e1
|
94 | 107 | e2' = hashCons e2
|
95 |
| - i = hash (hash @Text "EBinOp", op, getAnnotation e1', getAnnotation e2') |
| 108 | + i = Hash $ hash (hash @Text "EBinOp", op, getAnnotation e1', getAnnotation e2') |
96 | 109 | in EBinOp i op e1' e2'
|
97 | 110 | EIf _ b t e ->
|
98 | 111 | let b' = hashCons b
|
99 | 112 | t' = hashCons t
|
100 | 113 | e' = hashCons e
|
101 |
| - i = hash (hash @Text "EIf", getAnnotation b', getAnnotation t', getAnnotation e') |
| 114 | + i = Hash $ hash (hash @Text "EIf", getAnnotation b', getAnnotation t', getAnnotation e') |
102 | 115 | in EIf i b' t' e'
|
103 | 116 | EEq _ l r ->
|
104 | 117 | let l' = hashCons l
|
105 | 118 | r' = hashCons r
|
106 |
| - i = hash (hash @Text "EEq", getAnnotation l', getAnnotation r') |
| 119 | + i = Hash $ hash (hash @Text "EEq", getAnnotation l', getAnnotation r') |
107 | 120 | in EEq i l' r'
|
108 | 121 | ESplit _ n e ->
|
109 | 122 | let e' = hashCons e
|
110 |
| - i = hash (hash @Text "ESplit", n, getAnnotation e') |
| 123 | + i = Hash $ hash (hash @Text "ESplit", n, getAnnotation e') |
111 | 124 | in ESplit i n e'
|
112 | 125 | EJoin _ e ->
|
113 | 126 | let e' = hashCons e
|
114 |
| - i = hash (hash @Text "EJoin", getAnnotation e') |
| 127 | + i = Hash $ hash (hash @Text "EJoin", getAnnotation e') |
115 | 128 | in EJoin i e'
|
116 | 129 | EAtIndex _ v ix ->
|
117 | 130 | let v' = hashCons v
|
118 |
| - i = hash (hash @Text "AtIndex", getAnnotation v', ix) |
| 131 | + i = Hash $ hash (hash @Text "AtIndex", getAnnotation v', ix) |
119 | 132 | in EAtIndex i v' ix
|
120 | 133 | EUpdateIndex _ p b v ->
|
121 | 134 | let b' = hashCons b
|
122 | 135 | v' = hashCons v
|
123 |
| - i = hash (hash @Text "UpdateIndex", p, getAnnotation b', getAnnotation v') |
| 136 | + i = Hash $ hash (hash @Text "UpdateIndex", p, getAnnotation b', getAnnotation v') |
124 | 137 | in EUpdateIndex i p b' v'
|
125 | 138 | EBundle _ b ->
|
126 | 139 | let b' = V.map hashCons b
|
127 |
| - i = hash (hash @Text "Bundle", toList $ fmap getAnnotation b') |
| 140 | + i = Hash $ hash (hash @Text "Bundle", toList $ fmap getAnnotation b') |
128 | 141 | in EBundle i b'
|
0 commit comments