{-# LANGUAGE DataKinds #-} {-# LANGUAGE NoPolyKinds #-} {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE ViewPatterns #-} module Symantic.Parser.Automaton.Instructions where import Data.Bool (Bool) import Data.Either (Either) import Data.Function (($), (.)) import Symantic.Parser.Grammar import qualified Data.Functor as Functor import qualified Symantic.Parser.Staging as Hask {- class Automatable repr where ret :: repr inp '[ret] n ret a push :: x -> repr inp (x ': vs) n ret a -> repr inp vs n ret a pop :: repr inp vs n ret a -> repr inp (x ': vs) n ret a -} class InputPosition inp where -- * Type 'Instr' -- | 'Instr'uctions for the 'Automaton'. data Instr input valueStack (exceptionStack::Peano) returnValue a where -- | @('Ret')@ returns the value in a singleton value-stack. Ret :: Instr inp '[ret] es ret a -- | @('Push' x k)@ pushes @(x)@ on the value-stack and continues with the next 'Instr'uction @(k)@. Push :: InstrPure x -> Instr inp (x ': vs) es ret a -> Instr inp vs es ret a -- | @('Pop' k)@ pushes @(x)@ on the value-stack. Pop :: Instr inp vs es ret a -> Instr inp (x ': vs) es ret a -- | @('Lift2' f k)@ pops two values from the value-stack, and pushes the result of @(f)@ applied to them. Lift2 :: InstrPure (x -> y -> z) -> Instr inp (z : vs) es ret a -> Instr inp (y : x : vs) es ret a -- | @('Fail')@ raises an error from the exception-stack. Fail :: Instr inp vs ('Succ es) ret a -- | @('Commit' k)@ removes an exception from the exception-stack and continues with the next 'Instr'uction @(k)@. Commit :: Instr inp vs es ret a -> Instr inp vs ('Succ es) ret a -- | @('Catch' l r)@ tries the @(l)@ 'Instr'uction, if it raises an exception, catches it, pushes the input on the value-stack and continues with the @(r)@ 'Instr'uction. Catch :: Instr inp vs ('Succ es) ret a -> Instr inp (inp ': vs) es ret a -> Instr inp vs es ret a -- | @('Seek' k)@ removes the input from the value-stack and continues with the next 'Instr'uction @(k)@. Seek :: Instr inp vs es r a -> Instr inp (inp : vs) es r a -- | @('Tell' k)@ pushes the input @(inp)@ on the value-stack and continues with the next 'Instr'uction @(k)@. Tell :: Instr inp (inp ': vs) es ret a -> Instr inp vs es ret a Case :: Instr inp (x : vs) n r a -> Instr inp (y : vs) n r a -> Instr inp (Either x y : vs) n r a -- | @('Swap' k)@ pops two values on the value-stack, pushes the first popped-out, then the second, and continues with the next 'Instr'uction @(k)@. Swap :: Instr inp (x : y : vs) n r a -> Instr inp (y : x : vs) n r a Choices :: [InstrPure (x -> Bool)] -> [Instr inp vs es ret a] -> Instr inp vs es ret a -> Instr inp (x ': vs) es ret a -- ** Type 'InstrPure' data InstrPure a = InstrPureHaskell (Hask.Haskell a) | InstrPureSameOffset -- ** Type 'Peano' -- | Type-level natural numbers, using the Peano recursive encoding. data Peano = Zero | Succ Peano -- | @('App' k)@ pops @(x)@ and @(x2y)@ from the value-stack, pushes @(x2y x)@ and continues with the next 'Instr'uction @(k)@. pattern App :: Instr inp (y : vs) es ret a -> Instr inp (x : (x -> y) : vs) es ret a pattern App k = Lift2 (InstrPureHaskell (Hask.:$)) k -- | @('If' ok ko)@ pops a 'Bool' from the value-stack and continues either with the 'Instr'uction @(ok)@ if it is 'True' or @(ko)@ otherwise. pattern If :: Instr inp vs es ret a -> Instr inp vs es ret a -> Instr inp (Bool ': vs) es ret a pattern If ok ko = Choices [InstrPureHaskell Hask.Id] [ok] ko parsecHandler :: InputPosition inp => Instr inp vs ('Succ es) ret a -> Instr inp (inp : vs) ('Succ es) ret a parsecHandler k = Tell (Lift2 InstrPureSameOffset (If k Fail)) -- * Type 'Automaton' -- | Making the control-flow explicit. data Automaton inp a x = Automaton { unAutomaton :: forall vs es ret. {-next-}Instr inp (x ': vs) ('Succ es) ret a -> Instr inp vs ('Succ es) ret a } instance Applicable (Automaton inp a) where pure x = Automaton $ Push (InstrPureHaskell x) Automaton f <*> Automaton x = Automaton $ f . x . App liftA2 f (Automaton x) (Automaton y) = Automaton $ x . y . Lift2 (InstrPureHaskell f) Automaton x *> Automaton y = Automaton $ x . Pop . y Automaton x <* Automaton y = Automaton $ x . y . Pop instance InputPosition inp => Alternable (Automaton inp a) where empty = Automaton $ \_k -> Fail Automaton l <|> Automaton r = Automaton $ \k -> -- TODO: join points Catch (l (Commit k)) (parsecHandler (r k)) try (Automaton x) = Automaton $ \k -> Catch (x (Commit k)) (Seek Fail) instance Selectable (Automaton inp a) where branch (Automaton lr) (Automaton l) (Automaton r) = Automaton $ \k -> -- TODO: join points lr (Case (l (Swap (App k))) (r (Swap (App k)))) instance Matchable (Automaton inp a) where conditional ps bs (Automaton a) (Automaton def) = Automaton $ \k -> -- TODO: join points a (Choices (InstrPureHaskell Functor.<$> ps) ((\b -> unAutomaton b k) Functor.<$> bs) (def k))