1 {-# LANGUAGE ConstraintKinds #-} -- For Executable
2 {-# LANGUAGE PatternSynonyms #-} -- For Fmap, App, …
3 {-# LANGUAGE DerivingStrategies #-} -- For Show (Label a)
4 module Symantic.Parser.Automaton.Instructions where
6 import Data.Bool (Bool)
7 import Data.Char (Char)
8 import Data.Either (Either)
10 import Data.Function (($), (.))
11 import Data.Kind (Type)
12 import Text.Show (Show(..), showString)
13 import qualified Data.Functor as Functor
14 import qualified Language.Haskell.TH as TH
15 import qualified Symantic.Parser.Staging as H
17 import Symantic.Parser.Grammar
18 import Symantic.Parser.Automaton.Input
19 import Symantic.Univariant.Trans
22 -- | 'Instr'uctions for the 'Automaton'.
23 data Instr input valueStack (exceptionStack::Peano) returnValue a where
24 -- | @('Push' x k)@ pushes @(x)@ on the value-stack
25 -- and continues with the next 'Instr'uction @(k)@.
28 Instr inp (x ': vs) es ret a ->
30 -- | @('Pop' k)@ pushes @(x)@ on the value-stack.
32 Instr inp vs es ret a ->
33 Instr inp (x ': vs) es ret a
34 -- | @('LiftI2' f k)@ pops two values from the value-stack,
35 -- and pushes the result of @(f)@ applied to them.
37 InstrPure (x -> y -> z) ->
38 Instr inp (z : vs) es ret a ->
39 Instr inp (y : x : vs) es ret a
40 -- | @('Fail')@ raises an error from the exception-stack.
42 Instr inp vs ('Succ es) ret a
43 -- | @('Commit' k)@ removes an exception from the exception-stack
44 -- and continues with the next 'Instr'uction @(k)@.
46 Instr inp vs es ret a ->
47 Instr inp vs ('Succ es) ret a
48 -- | @('Catch' l r)@ tries the @(l)@ 'Instr'uction,
49 -- if it raises an exception, catches it,
50 -- pushes the input on the value-stack
51 -- and continues with the @(r)@ 'Instr'uction.
53 Instr inp vs ('Succ es) ret a ->
54 Instr inp (inp ': vs) es ret a ->
56 -- | @('Seek' k)@ removes the input from the value-stack
57 -- and continues with the next 'Instr'uction @(k)@.
59 Instr inp vs es r a ->
60 Instr inp (inp : vs) es r a
61 -- | @('Tell' k)@ pushes the input @(inp)@ on the value-stack
62 -- and continues with the next 'Instr'uction @(k)@.
64 Instr inp (inp ': vs) es ret a ->
68 Instr inp (x ': vs) es r a ->
69 Instr inp (y ': vs) es r a ->
70 Instr inp (Either x y ': vs) es r a
71 -- | @('Swap' k)@ pops two values on the value-stack,
72 -- pushes the first popped-out, then the second,
73 -- and continues with the next 'Instr'uction @(k)@.
75 Instr inp (x ': y ': vs) es r a ->
76 Instr inp (y ': x ': vs) es r a
77 -- | @('Choices' ps bs d)@.
79 [InstrPure (x -> Bool)] ->
80 [Instr inp vs es ret a] ->
81 Instr inp vs es ret a ->
82 Instr inp (x ': vs) es ret a
83 -- | @('Subroutine' n v k)@ binds the 'Label' @(n)@ to the 'Instr'uction's @(v)@,
84 -- continues with the next 'Instr'uction @(k)@.
87 Instr inp '[] ('Succ es) x a ->
88 Instr inp vs ('Succ es) ret a ->
89 Instr inp vs ('Succ es) ret a
90 -- | @('Jump' n k)@ pass the control-flow to the 'Subroutine' named @(n)@.
93 Instr inp '[] ('Succ es) ret a
94 -- | @('Call' n k)@ pass the control-flow to the 'Subroutine' named @(n)@,
95 -- and when it 'Ret'urns, continues with the next 'Instr'uction @(k)@.
98 Instr inp (x ': vs) ('Succ es) ret a ->
99 Instr inp vs ('Succ es) ret a
100 -- | @('Ret')@ returns the value stored in a singleton value-stack.
102 Instr inp '[ret] es ret a
103 -- | @('Read' p k)@ reads a 'Char' @(c)@ from the 'inp'ut,
104 -- if @(p c)@ is 'True' then continues with the next 'Instr'uction @(k)@ on,
107 InstrPure (Char -> Bool) ->
108 Instr inp (Char ': vs) ('Succ es) ret a ->
109 Instr inp vs ('Succ es) ret a
111 -- ** Type 'InstrPure'
112 data InstrPure a where
113 InstrPureHaskell :: H.Haskell a -> InstrPure a
114 InstrPureSameOffset :: InputPosition inp => InstrPure (inp -> inp -> Bool)
116 instance Show (InstrPure a) where
118 InstrPureHaskell x -> showsPrec p x
119 InstrPureSameOffset -> showString "InstrPureSameOffset"
120 instance Trans InstrPure TH.CodeQ where
122 InstrPureHaskell x -> trans x
123 InstrPureSameOffset -> same
126 newtype Label a = Label { unLabel :: TH.Name }
128 deriving newtype Show
130 -- * Class 'Executable'
131 type Executable repr =
140 -- ** Class 'Stackable'
141 class Stackable (repr :: Type -> [Type] -> Peano -> Type -> Type -> Type) where
144 repr inp (x ': vs) n ret a ->
147 repr inp vs n ret a ->
148 repr inp (x ': vs) n ret a
150 InstrPure (x -> y -> z) ->
151 repr inp (z ': vs) es ret a ->
152 repr inp (y ': x ': vs) es ret a
154 repr inp (x ': y ': vs) n r a ->
155 repr inp (y ': x ': vs) n r a
157 -- ** Class 'Branchable'
158 class Branchable (repr :: Type -> [Type] -> Peano -> Type -> Type -> Type) where
160 repr inp (x ': vs) n r a ->
161 repr inp (y ': vs) n r a ->
162 repr inp (Either x y ': vs) n r a
164 [InstrPure (x -> Bool)] ->
165 [repr inp vs es ret a] ->
166 repr inp vs es ret a ->
167 repr inp (x ': vs) es ret a
169 -- ** Class 'Exceptionable'
170 class Exceptionable (repr :: Type -> [Type] -> Peano -> Type -> Type -> Type) where
171 fail :: repr inp vs ('Succ es) ret a
173 repr inp vs es ret a ->
174 repr inp vs ('Succ es) ret a
176 repr inp vs ('Succ es) ret a ->
177 repr inp (inp ': vs) es ret a ->
180 -- ** Class 'Inputable'
181 class Inputable (repr :: Type -> [Type] -> Peano -> Type -> Type -> Type) where
183 repr inp vs es r a ->
184 repr inp (inp ': vs) es r a
186 repr inp (inp ': vs) es ret a ->
189 -- ** Class 'Routinable'
190 class Routinable (repr :: Type -> [Type] -> Peano -> Type -> Type -> Type) where
193 repr inp '[] ('Succ es) x a ->
194 repr inp vs ('Succ es) ret a ->
195 repr inp vs ('Succ es) ret a
198 repr inp (x ': vs) ('Succ es) ret a ->
199 repr inp vs ('Succ es) ret a
201 repr inp '[ret] es ret a
204 repr inp '[] ('Succ es) ret a
206 -- ** Class 'Readable'
207 class Readable (repr :: Type -> [Type] -> Peano -> Type -> Type -> Type) where
209 InstrPure (Char -> Bool) ->
210 repr inp (Char ': vs) ('Succ es) ret a ->
211 repr inp vs ('Succ es) ret a
215 Trans (Instr inp vs es ret) (repr inp vs es ret) where
217 Push x k -> push x (trans k)
218 Pop k -> pop (trans k)
219 LiftI2 f k -> liftI2 f (trans k)
221 Commit k -> commit (trans k)
222 Catch l r -> catch (trans l) (trans r)
223 Seek k -> seek (trans k)
224 Tell k -> tell (trans k)
225 Case l r -> case_ (trans l) (trans r)
226 Swap k -> swap (trans k)
227 Choices ps bs d -> choices ps (trans Functor.<$> bs) (trans d)
228 Subroutine n v k -> subroutine n (trans v) (trans k)
230 Call n (k::Instr inp (x ': vs) ('Succ es') ret a) ->
231 call n (trans k :: repr inp (x ': vs) ('Succ es') ret a)
233 Read p k -> read p (trans k)
236 -- | Type-level natural numbers, using the Peano recursive encoding.
237 data Peano = Zero | Succ Peano
241 InstrPure (x -> y) ->
242 Instr inp (y ': xs) es ret a ->
243 Instr inp (x ': xs) es ret a
244 pattern Fmap f k = Push f (LiftI2 (InstrPureHaskell (H.Flip H.:@ (H.:$))) k)
246 -- | @('App' k)@ pops @(x)@ and @(x2y)@ from the value-stack,
247 -- pushes @(x2y x)@ and continues with the next 'Instr'uction @(k)@.
248 pattern App :: Instr inp (y : vs) es ret a -> Instr inp (x : (x -> y) : vs) es ret a
249 pattern App k = LiftI2 (InstrPureHaskell (H.:$)) k
251 -- | @('If' ok ko)@ pops a 'Bool' from the value-stack
252 -- and continues either with the 'Instr'uction @(ok)@ if it is 'True'
253 -- or @(ko)@ otherwise.
254 pattern If :: Instr inp vs es ret a -> Instr inp vs es ret a -> Instr inp (Bool ': vs) es ret a
255 pattern If ok ko = Choices [InstrPureHaskell H.Id] [ok] ko
257 parsecHandler :: InputPosition inp => Instr inp vs ('Succ es) ret a -> Instr inp (inp : vs) ('Succ es) ret a
258 parsecHandler k = Tell (LiftI2 InstrPureSameOffset (If k Fail))
260 -- * Type 'Automaton'
261 -- | Making the control-flow explicit.
262 data Automaton inp a x = Automaton { unAutomaton ::
264 {-next-}Instr inp (x ': vs) ('Succ es) ret a ->
265 Instr inp vs ('Succ es) ret a
269 forall inp a es repr.
271 Automaton inp a a -> (repr inp '[] ('Succ es) a) a
272 runAutomaton (Automaton auto) =
273 trans @(Instr inp '[] ('Succ es) a) $
276 instance Applicable (Automaton inp a) where
277 pure x = Automaton $ Push (InstrPureHaskell x)
278 Automaton f <*> Automaton x = Automaton $ f . x . App
279 liftA2 f (Automaton x) (Automaton y) = Automaton $
280 x . y . LiftI2 (InstrPureHaskell f)
281 Automaton x *> Automaton y = Automaton $ x . Pop . y
282 Automaton x <* Automaton y = Automaton $ x . y . Pop
285 Alternable (Automaton inp a) where
286 empty = Automaton $ \_k -> Fail
287 Automaton l <|> Automaton r = Automaton $ \k ->
289 Catch (l (Commit k)) (parsecHandler (r k))
290 try (Automaton x) = Automaton $ \k ->
291 Catch (x (Commit k)) (Seek Fail)
292 instance Charable (Automaton inp a) where
293 satisfy p = Automaton $ Read (InstrPureHaskell p)
294 instance Selectable (Automaton inp a) where
295 branch (Automaton lr) (Automaton l) (Automaton r) = Automaton $ \k ->
297 lr (Case (l (Swap (App k)))
299 instance Matchable (Automaton inp a) where
300 conditional ps bs (Automaton a) (Automaton default_) = Automaton $ \k ->
302 a (Choices (InstrPureHaskell Functor.<$> ps)
303 ((\b -> unAutomaton b k) Functor.<$> bs)
305 instance Lookable (Automaton inp a) where
306 look (Automaton x) = Automaton $ \k ->
307 Tell (x (Swap (Seek k)))
308 negLook (Automaton x) = Automaton $ \k ->
309 Catch (Tell (x (Pop (Seek (Commit Fail)))))
310 (Seek (Push (InstrPureHaskell H.unit) k))
311 instance Letable TH.Name (Automaton inp a) where
312 def n (Automaton v) = Automaton $ \k ->
313 Subroutine (Label n) (v Ret) (Call (Label n) k)
314 ref _isRec n = Automaton $ \case
315 Ret -> Jump (Label n)
316 k -> Call (Label n) k
317 instance InputPosition inp => Foldable (Automaton inp a) where
319 chainPre op p = go <*> p
320 where go = (H..) <$> op <*> go <|> pure H.id
321 chainPost p op = p <**> go
322 where go = (H..) <$> op <*> go <|> pure H.id