-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathLanguage.hs
259 lines (209 loc) · 5.93 KB
/
Language.hs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
module Juvix.Compiler.Nockma.Language where
import Data.HashMap.Strict qualified as HashMap
import GHC.Base (Type)
import Juvix.Prelude hiding (Atom, Path)
import Juvix.Prelude.Pretty
data ReplStatement a
= ReplStatementExpression (ReplExpression a)
| ReplStatementAssignment (Assignment a)
data ReplExpression a
= ReplExpressionTerm (ReplTerm a)
| ReplExpressionWithStack (WithStack a)
data WithStack a = WithStack
{ _withStackStack :: ReplTerm a,
_withStackTerm :: ReplTerm a
}
data ReplTerm a
= ReplName Text
| ReplTerm (Term a)
newtype Program a = Program
{ _programStatements :: [Statement a]
}
data Statement a
= StatementAssignment (Assignment a)
| StatementStandalone (Term a)
data Assignment a = Assignment
{ _assignmentName :: Text,
_assignmentBody :: Term a
}
data Term a
= TermAtom (Atom a)
| TermCell (Cell a)
deriving stock (Show, Eq, Lift)
data Cell a = Cell
{ _cellLeft :: Term a,
_cellRight :: Term a
}
deriving stock (Show, Eq, Lift)
data Atom a = Atom
{ _atom :: a,
_atomInfo :: Irrelevant (Maybe AtomHint)
}
deriving stock (Show, Eq, Lift)
data AtomHint
= AtomHintOp
| AtomHintPath
| AtomHintBool
| AtomHintNil
deriving stock (Show, Eq, Lift)
data NockOp
= OpAddress
| OpQuote
| OpApply
| OpIsCell
| OpInc
| OpEq
| OpIf
| OpSequence
| OpPush
| OpCall
| OpReplace
| OpHint
deriving stock (Bounded, Enum, Eq, Generic)
instance Hashable NockOp
instance Pretty NockOp where
pretty = \case
OpAddress -> "@"
OpQuote -> "quote"
OpApply -> "apply"
OpIsCell -> "isCell"
OpInc -> "suc"
OpEq -> "="
OpIf -> "if"
OpSequence -> "seq"
OpPush -> "push"
OpCall -> "call"
OpReplace -> "replace"
OpHint -> "hint"
atomOps :: HashMap Text NockOp
atomOps = HashMap.fromList [(prettyText op, op) | op <- allElements]
data OperatorCell a = OperatorCell
{ _operatorCellOp :: NockOp,
_operatorCellTerm :: Term a
}
data AutoConsCell a = AutoConsCell
{ _autoConsCellLeft :: Cell a,
_autoConsCellRight :: Term a
}
data ParsedCell a
= ParsedOperatorCell (OperatorCell a)
| ParsedAutoConsCell (AutoConsCell a)
newtype EncodedPath = EncodedPath
{ _encodedPath :: Natural
}
data Direction
= L
| R
deriving stock (Show)
type Path = [Direction]
emptyPath :: Path
emptyPath = []
makeLenses ''Cell
makeLenses ''Atom
makeLenses ''OperatorCell
makeLenses ''AutoConsCell
makeLenses ''Program
makeLenses ''Assignment
makeLenses ''WithStack
makeLenses ''EncodedPath
naturalNockOps :: HashMap Natural NockOp
naturalNockOps = HashMap.fromList [(serializeOp op, op) | op <- allElements]
nockOpsNatural :: HashMap NockOp Natural
nockOpsNatural = HashMap.fromList (swap <$> HashMap.toList naturalNockOps)
parseOp :: (Member Fail r) => Natural -> Sem r NockOp
parseOp n = failMaybe (naturalNockOps ^. at n)
serializeOp :: NockOp -> Natural
serializeOp = \case
OpAddress -> 0
OpQuote -> 1
OpApply -> 2
OpIsCell -> 3
OpInc -> 4
OpEq -> 5
OpIf -> 6
OpSequence -> 7
OpPush -> 8
OpCall -> 9
OpReplace -> 10
OpHint -> 11
decodePath :: forall r. (Member Fail r) => EncodedPath -> Sem r Path
decodePath ep = execOutputList (go (ep ^. encodedPath))
where
go :: Natural -> Sem (Output Direction ': r) ()
go = \case
0 -> fail
1 -> return ()
x ->
if
| even x -> do
go (x `div` 2)
output L
| otherwise -> do
go ((x - 1) `div` 2)
output R
decodePath' :: EncodedPath -> Path
decodePath' = run . runFailDefault (error "encoded path cannot be 0") . decodePath
encodePath :: Path -> EncodedPath
encodePath = EncodedPath . foldl' step 1
where
step :: Natural -> Direction -> Natural
step n = \case
R -> 2 * n + 1
L -> 2 * n
class (Eq a) => NockNatural a where
type ErrNockNatural a :: Type
nockNatural :: (Member (Error (ErrNockNatural a)) r) => Atom a -> Sem r Natural
serializeNockOp :: NockOp -> a
serializePath :: Path -> a
errInvalidOp :: Atom a -> ErrNockNatural a
errInvalidPath :: Atom a -> ErrNockNatural a
nockOp :: (Member (Error (ErrNockNatural a)) r) => Atom a -> Sem r NockOp
nockOp atm = do
n <- nockNatural atm
failWithError (errInvalidOp atm) (parseOp n)
nockPath :: (Member (Error (ErrNockNatural a)) r) => Atom a -> Sem r Path
nockPath atm = do
n <- nockNatural atm
failWithError (errInvalidPath atm) (decodePath (EncodedPath n))
nockTrue :: Atom a
nockFalse :: Atom a
nockSucc :: Atom a -> Atom a
nockNil :: Atom a
data NockNaturalNaturalError
= NaturalInvalidPath (Atom Natural)
| NaturalInvalidOp (Atom Natural)
deriving stock (Show)
instance NockNatural Natural where
type ErrNockNatural Natural = NockNaturalNaturalError
nockNatural a = return (a ^. atom)
nockTrue = Atom 0 (Irrelevant (Just AtomHintBool))
nockFalse = Atom 1 (Irrelevant (Just AtomHintBool))
nockNil = Atom 0 (Irrelevant (Just AtomHintNil))
nockSucc = over atom succ
errInvalidOp atm = NaturalInvalidOp atm
errInvalidPath atm = NaturalInvalidPath atm
serializeNockOp = serializeOp
serializePath = (^. encodedPath) . encodePath
class IsNock nock where
toNock :: nock -> Term Natural
instance IsNock (Term Natural) where
toNock = id
instance IsNock (Atom Natural) where
toNock = TermAtom
instance IsNock (Cell Natural) where
toNock = TermCell
instance IsNock Natural where
toNock n = toNock (Atom n (Irrelevant Nothing))
instance IsNock NockOp where
toNock op = toNock (Atom (serializeOp op) (Irrelevant (Just AtomHintOp)))
instance IsNock Bool where
toNock = \case
False -> toNock (nockFalse @Natural)
True -> toNock (nockTrue @Natural)
instance IsNock Path where
toNock pos = TermAtom (Atom (encodePath pos ^. encodedPath) (Irrelevant (Just AtomHintPath)))
instance IsNock EncodedPath where
toNock = toNock . decodePath'
infixr 5 #
(#) :: (IsNock x, IsNock y) => x -> y -> Term Natural
a # b = TermCell (Cell (toNock a) (toNock b))