{-# LANGUAGE OverloadedStrings,
             PatternSynonyms,
             DerivingStrategies #-}
{-|
Module      : Parsley.Internal.Backend.Machine.Instructions
Description : Core instructions that make up a low-level parser.
License     : BSD-3-Clause
Maintainer  : Jamie Willis
Stability   : experimental

This contains the instructions and satelite datatypes for representing
parsers at the lowest CPS-form level. These are indexed by multiple types,
which are documented in the source (if not on Haddock!).

@since 1.0.0.0
-}
module Parsley.Internal.Backend.Machine.Instructions (
    -- * Main Instructions
    Instr(..),
    -- * Auxilliary Types
    Handler(..),
    Access(..),
    MetaInstr(..),
    -- * Smart Instructions
    _App, _Fmap, _Modify, _Make, _Put, _Get, _Jump,
    -- * Smart Meta-Instructions
    addCoins, refundCoins, drainCoins, giveBursary, blockCoins,
    -- * Re-exports
    PosSelector(..)
  ) where

import Data.Kind                                    (Type)
import Data.Void                                    (Void)
import Parsley.Internal.Backend.Machine.Identifiers (MVar, ΦVar, ΣVar)
import Parsley.Internal.Backend.Machine.Types.Coins (Coins(willConsume))
import Parsley.Internal.Common                      (IFunctor4, Fix4(In4), Const4(..), imap4, cata4, Nat(..), One, intercalateDiff)
import Parsley.Internal.Core.CombinatorAST          (PosSelector(..))
import Parsley.Internal.Core.CharPred               (CharPred)

import Parsley.Internal.Backend.Machine.Defunc as Machine (Defunc, user)
import Parsley.Internal.Core.Defunc            as Core    (Defunc(ID), pattern FLIP_H)

import qualified Parsley.Internal.Backend.Machine.Types.Coins as Coins (pattern Zero)

{-|
This represents the instructions of the machine, in CPS form as an indexed functor.

When an instruction has a `Succ` in the type, it indicates that it is capable of failing.

@since 1.4.0.0
-}
data Instr (o :: Type)                                  -- The FIXED input type
           (k :: [Type] -> Nat -> Type -> Type -> Type) -- The FIXED continuation parameter
           (xs :: [Type])                               -- The VARIABLE type defining the required types on the stack on entry
           (n :: Nat)                                   -- The VARIABLE type defining how many handlers are required on entry
           (r :: Type)                                  -- The VARIABLE intermediate type defining what value this parser immediately produces
           (a :: Type)                                  -- The (technically VARIABLE) type specifying the final result of the parser
  where
  {-| This instruction returns from either calls or the entire parser at the top-level.

  @since 1.0.0.0 -}
  Ret       :: Instr o k '[x] n x a {- ^ -}
  {-| Pushes a value onto the stack, which is required by the continuation parameter.

  @since 1.0.0.0 -}
  Push      :: Machine.Defunc x {- ^ Value to push. -} -> k (x : xs) n r a {- ^ Machine requiring value. -} -> Instr o k xs n r a
  {-| Removes a value from the stack, so it is the correct shape for the continuation parameter.

  @since 1.0.0.0 -}
  Pop       :: k xs n r a {- ^ -} -> Instr o k (x : xs) n r a
  {-| Applies a function to the top two elements of the stack, converting them to something else and pushing it back on.

  @since 1.0.0.0 -}
  Lift2     :: Machine.Defunc (x -> y -> z) {- ^ Function to apply. -}
            -> k (z : xs) n r a             {- ^ Machine requiring new value. -}
            -> Instr o k (y : x : xs) n r a
  {-| Reads a character so long as it matches a given predicate. If it does not, or no input is available, this instruction fails.

  @since 2.1.0.0 -}
  Sat       :: CharPred                   {- ^ Predicate to apply. -}
            -> k (Char : xs) (Succ n) r a {- ^ Machine requiring read character. -}
            -> Instr o k xs (Succ n) r a
  {-| Calls another let-bound parser.

  @since 1.0.0.0 -}
  Call      :: MVar x                  {- ^ The binding to invoke. -}
            -> k (x : xs) (Succ n) r a {- ^ Continuation to do after the call. -}
            -> Instr o k xs (Succ n) r a
  {-| Fails unconditionally.

  @since 1.0.0.0 -}
  Empt      :: Instr o k xs (Succ n) r a {- ^ -}
  {-| Discards a failure handler, so that it is no longer in scope.

  @since 1.0.0.0 -}
  Commit    :: k xs n r a {- ^ Next machine, which will /not/ require the discarded handler. -} -> Instr o k xs (Succ n) r a
  {-| Registers a handler to deal with possible failure in the given machine.

  @since 1.4.0.0 -}
  Catch     :: k xs (Succ n) r a          {- ^ Machine where failure is handled by the handler. -}
            -> Handler o k (o : xs) n r a {- ^ The handler to register. -}
            -> Instr o k xs n r a
  {-| Pushes the current input offset onto the stack.

  @since 1.0.0.0 -}
  Tell      :: k (o : xs) n r a {- ^ The machine that accepts the input. -} -> Instr o k xs n r a
  {-| Pops the input offset off of the stack and makes that the current input offset.

  @since 1.0.0.0 -}
  Seek      :: k xs n r a {- ^ Machine to continue with new input. -} -> Instr o k (o : xs) n r a
  {-| Picks one of two continuations based on whether a `Left` or `Right` is on the stack.

  @since 1.0.0.0 -}
  Case      :: k (x : xs) n r a {- ^ Machine to execute if `Left` on stack. -}
            -> k (y : xs) n r a {- ^ Machine to execute if `Right` on stack. -}
            -> Instr o k (Either x y : xs) n r a
  {-| Given a collection of predicates and machines, this instruction will execute the first machine
      for which the corresponding predicate returns true for the value on the top of the stack.

  @since 1.0.0.0 -}
  Choices   :: [Machine.Defunc (x -> Bool)] {- ^ A list of predicates to try. -}
            -> [k xs n r a]                 {- ^ A corresponding list of machines. -}
            -> k xs n r a                   {- ^ A default machine to execute if no predicates match. -}
            -> Instr o k (x : xs) n r a
  {-| Sets up an iteration, where the second argument is executed repeatedly until it fails, which is
      handled by the given handler. The use of `Void` indicates that `Ret` is illegal within the loop.

  @since 1.0.0.0 -}
  Iter      :: MVar Void                  {- ^ The name of the binding. -}
            -> k '[] One Void a           {- ^ The body of the loop: it cannot return "normally". -}
            -> Handler o k (o : xs) n r a {- ^ The handler for the loop's exit. -}
            -> Instr o k xs n r a
  {-| Jumps to a given join point.

  @since 1.0.0.0 -}
  Join      :: ΦVar x {- ^ The join point to jump to. -} -> Instr o k (x : xs) n r a
  {-| Sets up a new join point binding.

  @since 1.0.0.0 -}
  MkJoin    :: ΦVar x           {- ^ The name of the binding that can be referred to later. -}
            -> k (x : xs) n r a {- ^ The body of the join point binding. -}
            -> k xs n r a       {- ^ The scope within which the binding is valid.  -}
            -> Instr o k xs n r a
  {-| Swaps the top two elements on the stack

  @since 1.0.0.0 -}
  Swap      :: k (x : y : xs) n r a {- ^ The machine that requires the reversed stack. -} -> Instr o k (y : x : xs) n r a
  {-| Duplicates the top value on the stack. May produce a let-binding.

  @since 1.0.0.0 -}
  Dup       :: k (x : x : xs) n r a {- ^ Machine that requires doubled element. -} -> Instr o k (x : xs) n r a
  {-| Initialises a new register for use within the continuation. Initial value is on the stack.

  @since 1.0.0.0 -}
  Make      :: ΣVar x     {- ^ The name of the new register. -}
            -> Access     {- ^ Whether or not the register is "concrete". -}
            -> k xs n r a {- ^ The scope within which the register is accessible. -}
            -> Instr o k (x : xs) n r a
  {-| Pushes the value contained within a register onto the stack.

  @since 1.0.0.0 -}
  Get       :: ΣVar x           {- ^ Name of the register to read. -}
            -> Access           {- ^ Whether or not the value is cached. -}
            -> k (x : xs) n r a {- ^ The machine that requires the value. -}
            -> Instr o k xs n r a
  {-| Places the value on the top of the stack into a given register.

  @since 1.0.0.0 -}
  Put       :: ΣVar x     {- ^ Name of the register to update. -}
            -> Access     {- ^ Whether or not the value needs to be stored in a concrete register. -}
            -> k xs n r a
            -> Instr o k (x : xs) n r a
  SelectPos :: PosSelector -> k (Int : xs) n r a -> Instr o k xs n r a
  {-| Begins a debugging scope, the inner scope requires /two/ handlers,
      the first is the log handler itself, and then the second is the
      "real" fail handler for when the log handler is executed.

  @since 1.0.0.0 -}
  LogEnter  :: String                   {- ^ The message to be printed. -}
            -> k xs (Succ (Succ n)) r a {- ^ The machine to be debugged. -}
            -> Instr o k xs (Succ n) r a
  {-| Ends the log scope after a succesful execution.

  @since 1.0.0.0 -}
  LogExit   :: String     {- ^ The message to be printed. -}
            -> k xs n r a {- ^ The machine that follows. -}
            -> Instr o k xs n r a
  {-| Executes a meta-instruction, which is interacting with
      implementation specific static information.

  @since 1.0.0.0 -}
  MetaInstr :: MetaInstr n {- ^ A meta-instruction to perform. -}
            -> k xs n r a  {- ^ The machine that follows. -}
            -> Instr o k xs n r a

{-|
There are two types of organic handlers within parsley, which are
captured by this type, which is also an IFunctor and wraps around
`Instr`.

@since 1.4.0.0
-}
data Handler (o :: Type) (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type]) (n :: Nat) (r :: Type) (a :: Type) where
  {-| These handlers have two distinct behaviours depending on whether the
      captured offset matches the current offset or not.

  @since 1.4.0.0 -}
  Same :: Bool             -- ^ Whether the input matches handler should generate a binding
       -> k xs n r a       -- ^ Execute when the input matches, notice that the captured offset is discarded since it is equal to the current.
       -> Bool             -- ^ Whether the input does not match handler should generate a binding
       -> k (o : xs) n r a -- ^ Execute when the input does not match, the resulting behaviour could use the captured or current input.
       -> Handler o k (o : xs) n r a
  {-| These handlers are unconditional on the input, and will always do the same
      thing regardless of the input provided.

  @since 1.7.0.0 -}
  Always :: Bool             -- ^ Whether the handler should generate a binding
         -> k (o : xs) n r a -- ^ The handler
         -> Handler o k (o : xs) n r a

{-|
This determines whether or not an interaction with an register should be materialised
in the generated code or not.

@since 1.0.0.0
-}
data Access = Hard -- ^ Register exists at runtime and this interaction will use it.
            | Soft -- ^ Register may not exist, and the interaction should be with cache regardless.
            deriving stock Int -> Access -> String -> String
[Access] -> String -> String
Access -> String
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [Access] -> String -> String
$cshowList :: [Access] -> String -> String
show :: Access -> String
$cshow :: Access -> String
showsPrec :: Int -> Access -> String -> String
$cshowsPrec :: Int -> Access -> String -> String
Show

{-|
These are meta-instructions, which interact with static information to direct the
code-generation process. They are not formally part of parsley's semantics and can
be omitted from an implementation without consequence.

@since 1.0.0.0
-}
data MetaInstr (n :: Nat) where
  {-| Adds coins to the piggy-bank system (see "Parsley.Internal.Backend.Machine.Types.Context" for more information).
      If there are coins already available, add a piggy-bank, otherwise generate a length check and add the coins.

      A handler is required, in case the length check fails.

  @since 1.5.0.0 -}
  AddCoins    :: Coins -> MetaInstr (Succ n)
  {-| Refunds to the piggy-bank system (see "Parsley.Internal.Backend.Machine.Types.Context" for more information).
      This always happens for free, and is added straight to the coins.

  @since 1.5.0.0 -}
  RefundCoins :: Int -> MetaInstr n
  {-| Remove coins from piggy-bank system (see "Parsley.Internal.Backend.Machine.Types.Context" for more information)
      This is used to pay for more expensive calls to bindings with known required input.

      A handler is required, as there may not be enough coins to pay the cost and a length check causes a failure.

  @since 1.5.0.0 -}
  DrainCoins  :: Int -> MetaInstr (Succ n)
  {-| Refunds to the piggy-bank system (see "Parsley.Internal.Backend.Machine.Types.Context" for more information).
      This always happens for free, and is added straight to the coins. Unlike `RefundCoins` this cannot reclaim
      input, nor is is subtractive in the analysis.

  @since 1.5.0.0 -}
  GiveBursary :: Int -> MetaInstr n
  {-|
  True meta instruction: does /nothing/ except for reset coin count during coin analysis.

  @since 1.6.0.0
  -}
  BlockCoins :: Bool -> MetaInstr n

mkCoin :: (Coins -> MetaInstr n) -> Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
mkCoin :: forall (n :: Nat) o (xs :: [Type]) r a.
(Coins -> MetaInstr n)
-> Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
mkCoin Coins -> MetaInstr n
_    Coins
Coins.Zero  = forall a. a -> a
id
mkCoin Coins -> MetaInstr n
meta Coins
n           = forall {k} {k1} {k2} {k3}
       (f :: (k -> k1 -> k2 -> k3 -> Type) -> k -> k1 -> k2 -> k3 -> Type)
       (i :: k) (j :: k1) (k4 :: k2) (l :: k3).
f (Fix4 f) i j k4 l -> Fix4 f i j k4 l
In4 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat) (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) r a o.
MetaInstr n -> k xs n r a -> Instr o k xs n r a
MetaInstr (Coins -> MetaInstr n
meta Coins
n)

{-|
Smart-constuctor around `AddCoins`.

@since 1.5.0.0
-}
addCoins :: Coins -> Fix4 (Instr o) xs (Succ n) r a -> Fix4 (Instr o) xs (Succ n) r a
addCoins :: forall o (xs :: [Type]) (n :: Nat) r a.
Coins
-> Fix4 (Instr o) xs ('Succ n) r a
-> Fix4 (Instr o) xs ('Succ n) r a
addCoins = forall (n :: Nat) o (xs :: [Type]) r a.
(Coins -> MetaInstr n)
-> Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
mkCoin forall (xs :: Nat). Coins -> MetaInstr ('Succ xs)
AddCoins

{-|
Smart-constuctor around `RefundCoins`.

@since 1.5.0.0
-}
refundCoins :: Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
refundCoins :: forall o (xs :: [Type]) (n :: Nat) r a.
Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
refundCoins = forall (n :: Nat) o (xs :: [Type]) r a.
(Coins -> MetaInstr n)
-> Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
mkCoin (forall (n :: Nat). Int -> MetaInstr n
RefundCoins forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coins -> Int
willConsume)

{-|
Smart-constuctor around `DrainCoins`.

@since 1.5.0.0
-}
drainCoins :: Coins -> Fix4 (Instr o) xs (Succ n) r a -> Fix4 (Instr o) xs (Succ n) r a
drainCoins :: forall o (xs :: [Type]) (n :: Nat) r a.
Coins
-> Fix4 (Instr o) xs ('Succ n) r a
-> Fix4 (Instr o) xs ('Succ n) r a
drainCoins = forall (n :: Nat) o (xs :: [Type]) r a.
(Coins -> MetaInstr n)
-> Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
mkCoin (forall (xs :: Nat). Int -> MetaInstr ('Succ xs)
DrainCoins forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coins -> Int
willConsume)

{-|
Smart-constuctor around `RefundCoins`.

@since 1.5.0.0
-}
giveBursary :: Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
giveBursary :: forall o (xs :: [Type]) (n :: Nat) r a.
Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
giveBursary = forall (n :: Nat) o (xs :: [Type]) r a.
(Coins -> MetaInstr n)
-> Coins -> Fix4 (Instr o) xs n r a -> Fix4 (Instr o) xs n r a
mkCoin (forall (n :: Nat). Int -> MetaInstr n
GiveBursary forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coins -> Int
willConsume)

{-|
Smart-constructor around `BlockCoins`.

@since 1.6.0.0
-}
blockCoins :: Bool -> Fix4 (Instr o) xs (Succ n) r a -> Fix4 (Instr o) xs (Succ n) r a
blockCoins :: forall o (xs :: [Type]) (n :: Nat) r a.
Bool
-> Fix4 (Instr o) xs ('Succ n) r a
-> Fix4 (Instr o) xs ('Succ n) r a
blockCoins Bool
strong = forall {k} {k1} {k2} {k3}
       (f :: (k -> k1 -> k2 -> k3 -> Type) -> k -> k1 -> k2 -> k3 -> Type)
       (i :: k) (j :: k1) (k4 :: k2) (l :: k3).
f (Fix4 f) i j k4 l -> Fix4 f i j k4 l
In4 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat) (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) r a o.
MetaInstr n -> k xs n r a -> Instr o k xs n r a
MetaInstr (forall (n :: Nat). Bool -> MetaInstr n
BlockCoins Bool
strong)

{-|
Applies a value on the top of the stack to a function on the second-most top of the stack.

@since 1.0.0.0
-}
_App :: Fix4 (Instr o) (y : xs) n r a -> Instr o (Fix4 (Instr o)) (x : (x -> y) : xs) n r a
_App :: forall o y (xs :: [Type]) (n :: Nat) r a x.
Fix4 (Instr o) (y : xs) n r a
-> Instr o (Fix4 (Instr o)) (x : (x -> y) : xs) n r a
_App = forall xs xs xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
Defunc (xs -> xs -> xs)
-> k (xs : xs) n r a -> Instr o k (xs : xs : xs) n r a
Lift2 (forall a. Defunc a -> Defunc a
user forall a1. Defunc (a1 -> a1)
ID)

{-|
Adjusts the value on the top of the stack with the given function.

@since 1.0.0.0
-}
_Fmap :: Machine.Defunc (x -> y) -> Fix4 (Instr o) (y : xs) n r a -> Instr o (Fix4 (Instr o)) (x : xs) n r a
_Fmap :: forall x y o (xs :: [Type]) (n :: Nat) r a.
Defunc (x -> y)
-> Fix4 (Instr o) (y : xs) n r a
-> Instr o (Fix4 (Instr o)) (x : xs) n r a
_Fmap Defunc (x -> y)
f Fix4 (Instr o) (y : xs) n r a
k = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
Defunc xs -> k (xs : xs) n r a -> Instr o k xs n r a
Push Defunc (x -> y)
f (forall {k} {k1} {k2} {k3}
       (f :: (k -> k1 -> k2 -> k3 -> Type) -> k -> k1 -> k2 -> k3 -> Type)
       (i :: k) (j :: k1) (k4 :: k2) (l :: k3).
f (Fix4 f) i j k4 l -> Fix4 f i j k4 l
In4 (forall xs xs xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
Defunc (xs -> xs -> xs)
-> k (xs : xs) n r a -> Instr o k (xs : xs : xs) n r a
Lift2 (forall a. Defunc a -> Defunc a
user (forall y x a b c.
((x -> y) ~ ((a -> b -> c) -> b -> a -> c)) =>
Defunc x -> Defunc y
FLIP_H forall a1. Defunc (a1 -> a1)
ID)) Fix4 (Instr o) (y : xs) n r a
k))

{-|
Updates the value in a given register using the function on the top of the stack.

@since 1.0.0.0
-}
_Modify :: ΣVar x -> Fix4 (Instr o) xs n r a -> Instr o (Fix4 (Instr o)) ((x -> x) : xs) n r a
_Modify :: forall x o (xs :: [Type]) (n :: Nat) r a.
ΣVar x
-> Fix4 (Instr o) xs n r a
-> Instr o (Fix4 (Instr o)) ((x -> x) : xs) n r a
_Modify ΣVar x
σ  = forall x (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar x -> k (x : xs) n r a -> Instr o k xs n r a
_Get ΣVar x
σ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k1} {k2} {k3}
       (f :: (k -> k1 -> k2 -> k3 -> Type) -> k -> k1 -> k2 -> k3 -> Type)
       (i :: k) (j :: k1) (k4 :: k2) (l :: k3).
f (Fix4 f) i j k4 l -> Fix4 f i j k4 l
In4 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall o y (xs :: [Type]) (n :: Nat) r a x.
Fix4 (Instr o) (y : xs) n r a
-> Instr o (Fix4 (Instr o)) (x : (x -> y) : xs) n r a
_App forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k1} {k2} {k3}
       (f :: (k -> k1 -> k2 -> k3 -> Type) -> k -> k1 -> k2 -> k3 -> Type)
       (i :: k) (j :: k1) (k4 :: k2) (l :: k3).
f (Fix4 f) i j k4 l -> Fix4 f i j k4 l
In4 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar x -> k xs n r a -> Instr o k (x : xs) n r a
_Put ΣVar x
σ

{-|
Smart-instruction for `Make` that uses a `Hard` access.

@since 1.0.0.0
-}
_Make :: ΣVar x -> k xs n r a -> Instr o k (x : xs) n r a
_Make :: forall x (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar x -> k xs n r a -> Instr o k (x : xs) n r a
_Make ΣVar x
σ = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar xs -> Access -> k xs n r a -> Instr o k (xs : xs) n r a
Make ΣVar x
σ Access
Hard

{-|
Smart-instruction for `Put` that uses a `Hard` access.

@since 1.0.0.0
-}
_Put :: ΣVar x -> k xs n r a -> Instr o k (x : xs) n r a
_Put :: forall x (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar x -> k xs n r a -> Instr o k (x : xs) n r a
_Put ΣVar x
σ = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar xs -> Access -> k xs n r a -> Instr o k (xs : xs) n r a
Put ΣVar x
σ Access
Hard

{-|
Smart-instruction for `Get` that uses a `Hard` access.

@since 1.0.0.0
-}
_Get :: ΣVar x -> k (x : xs) n r a -> Instr o k xs n r a
_Get :: forall x (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar x -> k (x : xs) n r a -> Instr o k xs n r a
_Get ΣVar x
σ = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar xs -> Access -> k (xs : xs) n r a -> Instr o k xs n r a
Get ΣVar x
σ Access
Hard

_Jump :: MVar x -> Instr o (Fix4 (Instr o)) '[] (Succ n) x a
_Jump :: forall x o (n :: Nat) a.
MVar x -> Instr o (Fix4 (Instr o)) '[] ('Succ n) x a
_Jump = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (xs :: Nat) r a o.
MVar xs
-> k (xs : xs) ('Succ xs) r a -> Instr o k xs ('Succ xs) r a
Call (forall {k} {k1} {k2} {k3}
       (f :: (k -> k1 -> k2 -> k3 -> Type) -> k -> k1 -> k2 -> k3 -> Type)
       (i :: k) (j :: k1) (k4 :: k2) (l :: k3).
f (Fix4 f) i j k4 l -> Fix4 f i j k4 l
In4 forall o (k :: [Type] -> Nat -> Type -> Type -> Type) x (n :: Nat)
       a.
Instr o k '[x] n x a
Ret)

-- Instances
instance IFunctor4 (Instr o) where
  imap4 :: forall (a :: [Type] -> Nat -> Type -> Type -> Type) x
       (b :: [Type] -> Nat -> Type -> Type -> Type) (i :: [Type])
       (j :: Nat) k.
(forall (i' :: [Type]) (j' :: Nat) k'.
 a i' j' k' x -> b i' j' k' x)
-> Instr o a i j k x -> Instr o b i j k x
imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
_ Instr o a i j k x
Ret                 = forall o (k :: [Type] -> Nat -> Type -> Type -> Type) x (n :: Nat)
       a.
Instr o k '[x] n x a
Ret
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Push Defunc x
x a (x : i) j k x
k)          = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
Defunc xs -> k (xs : xs) n r a -> Instr o k xs n r a
Push Defunc x
x (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (x : i) j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Pop a xs j k x
k)             = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (n :: Nat) r a o xs.
k xs n r a -> Instr o k (xs : xs) n r a
Pop (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a xs j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Lift2 Defunc (x -> y -> z)
g a (z : xs) j k x
k)         = forall xs xs xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
Defunc (xs -> xs -> xs)
-> k (xs : xs) n r a -> Instr o k (xs : xs : xs) n r a
Lift2 Defunc (x -> y -> z)
g (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (z : xs) j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Sat CharPred
g a (Char : i) ('Succ n) k x
k)           = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (xs :: Nat) r a o.
CharPred
-> k (Char : xs) ('Succ xs) r a -> Instr o k xs ('Succ xs) r a
Sat CharPred
g (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (Char : i) ('Succ n) k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Call MVar x
μ a (x : i) ('Succ n) k x
k)          = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (xs :: Nat) r a o.
MVar xs
-> k (xs : xs) ('Succ xs) r a -> Instr o k xs ('Succ xs) r a
Call MVar x
μ (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (x : i) ('Succ n) k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
_ Instr o a i j k x
Empt                = forall o (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (xs :: Nat) r a.
Instr o k xs ('Succ xs) r a
Empt
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Commit a i n k x
k)          = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (xs :: Nat) r a o.
k xs xs r a -> Instr o k xs ('Succ xs) r a
Commit (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a i n k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Catch a i ('Succ j) k x
p Handler o a (o : i) j k x
h)         = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (n :: Nat) r a o.
k xs ('Succ n) r a
-> Handler o k (o : xs) n r a -> Instr o k xs n r a
Catch (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a i ('Succ j) k x
p) (forall (f :: ([Type] -> Nat -> Type -> Type -> Type)
             -> [Type] -> Nat -> Type -> Type -> Type)
       (a :: [Type] -> Nat -> Type -> Type -> Type) x
       (b :: [Type] -> Nat -> Type -> Type -> Type) (i :: [Type])
       (j :: Nat) k.
IFunctor4 f =>
(forall (i' :: [Type]) (j' :: Nat) k'.
 a i' j' k' x -> b i' j' k' x)
-> f a i j k x -> f b i j k x
imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f Handler o a (o : i) j k x
h)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Tell a (o : i) j k x
k)            = forall (k :: [Type] -> Nat -> Type -> Type -> Type) o
       (xs :: [Type]) (n :: Nat) r a.
k (o : xs) n r a -> Instr o k xs n r a
Tell (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (o : i) j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Seek a xs j k x
k)            = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (n :: Nat) r a o.
k xs n r a -> Instr o k (o : xs) n r a
Seek (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a xs j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Case a (x : xs) j k x
p a (y : xs) j k x
q)          = forall (k :: [Type] -> Nat -> Type -> Type -> Type) xs
       (xs :: [Type]) (n :: Nat) r a xs o.
k (xs : xs) n r a
-> k (xs : xs) n r a -> Instr o k (Either xs xs : xs) n r a
Case (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (x : xs) j k x
p) (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (y : xs) j k x
q)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Choices [Defunc (x -> Bool)]
fs [a xs j k x]
ks a xs j k x
def) = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
[Defunc (xs -> Bool)]
-> [k xs n r a] -> k xs n r a -> Instr o k (xs : xs) n r a
Choices [Defunc (x -> Bool)]
fs (forall a b. (a -> b) -> [a] -> [b]
map forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f [a xs j k x]
ks) (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a xs j k x
def)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Iter MVar Void
μ a '[] One Void x
l Handler o a (o : i) j k x
h)        = forall (k :: [Type] -> Nat -> Type -> Type -> Type) a o
       (xs :: [Type]) (n :: Nat) r.
MVar Void
-> k '[] One Void a
-> Handler o k (o : xs) n r a
-> Instr o k xs n r a
Iter MVar Void
μ (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a '[] One Void x
l) (forall (f :: ([Type] -> Nat -> Type -> Type -> Type)
             -> [Type] -> Nat -> Type -> Type -> Type)
       (a :: [Type] -> Nat -> Type -> Type -> Type) x
       (b :: [Type] -> Nat -> Type -> Type -> Type) (i :: [Type])
       (j :: Nat) k.
IFunctor4 f =>
(forall (i' :: [Type]) (j' :: Nat) k'.
 a i' j' k' x -> b i' j' k' x)
-> f a i j k x -> f b i j k x
imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f Handler o a (o : i) j k x
h)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
_ (Join ΦVar x
φ)            = forall xs o (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a.
ΦVar xs -> Instr o k (xs : xs) n r a
Join ΦVar x
φ
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (MkJoin ΦVar x
φ a (x : i) j k x
p a i j k x
k)      = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΦVar xs -> k (xs : xs) n r a -> k xs n r a -> Instr o k xs n r a
MkJoin ΦVar x
φ (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (x : i) j k x
p) (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a i j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Swap a (x : y : xs) j k x
k)            = forall (k :: [Type] -> Nat -> Type -> Type -> Type) xs xs
       (xs :: [Type]) (n :: Nat) r a o.
k (xs : xs : xs) n r a -> Instr o k (xs : xs : xs) n r a
Swap (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (x : y : xs) j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Dup a (x : x : xs) j k x
k)             = forall (k :: [Type] -> Nat -> Type -> Type -> Type) xs
       (xs :: [Type]) (n :: Nat) r a o.
k (xs : xs : xs) n r a -> Instr o k (xs : xs) n r a
Dup (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (x : x : xs) j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Make ΣVar x
σ Access
a a xs j k x
k)        = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar xs -> Access -> k xs n r a -> Instr o k (xs : xs) n r a
Make ΣVar x
σ Access
a (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a xs j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Get ΣVar x
σ Access
a a (x : i) j k x
k)         = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar xs -> Access -> k (xs : xs) n r a -> Instr o k xs n r a
Get ΣVar x
σ Access
a (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (x : i) j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Put ΣVar x
σ Access
a a xs j k x
k)         = forall xs (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) (n :: Nat) r a o.
ΣVar xs -> Access -> k xs n r a -> Instr o k (xs : xs) n r a
Put ΣVar x
σ Access
a (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a xs j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (SelectPos PosSelector
sel a (Int : i) j k x
k)   = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (n :: Nat) r a o.
PosSelector -> k (Int : xs) n r a -> Instr o k xs n r a
SelectPos PosSelector
sel (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (Int : i) j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (LogEnter String
name a i ('Succ ('Succ n)) k x
k)   = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (xs :: Nat) r a o.
String
-> k xs ('Succ ('Succ xs)) r a -> Instr o k xs ('Succ xs) r a
LogEnter String
name (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a i ('Succ ('Succ n)) k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (LogExit String
name a i j k x
k)    = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (n :: Nat) r a o.
String -> k xs n r a -> Instr o k xs n r a
LogExit String
name (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a i j k x
k)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (MetaInstr MetaInstr j
m a i j k x
k)     = forall (n :: Nat) (k :: [Type] -> Nat -> Type -> Type -> Type)
       (xs :: [Type]) r a o.
MetaInstr n -> k xs n r a -> Instr o k xs n r a
MetaInstr MetaInstr j
m (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a i j k x
k)

instance IFunctor4 (Handler o) where
  imap4 :: forall (a :: [Type] -> Nat -> Type -> Type -> Type) x
       (b :: [Type] -> Nat -> Type -> Type -> Type) (i :: [Type])
       (j :: Nat) k.
(forall (i' :: [Type]) (j' :: Nat) k'.
 a i' j' k' x -> b i' j' k' x)
-> Handler o a i j k x -> Handler o b i j k x
imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Same Bool
gyes a xs j k x
yes Bool
gno a (o : xs) j k x
no) = forall (k :: [Type] -> Nat -> Type -> Type -> Type) (xs :: [Type])
       (n :: Nat) r a o.
Bool
-> k xs n r a
-> Bool
-> k (o : xs) n r a
-> Handler o k (o : xs) n r a
Same Bool
gyes (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a xs j k x
yes) Bool
gno (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (o : xs) j k x
no)
  imap4 forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f (Always Bool
gk a (o : xs) j k x
k)          = forall (k :: [Type] -> Nat -> Type -> Type -> Type) o
       (xs :: [Type]) (n :: Nat) r a.
Bool -> k (o : xs) n r a -> Handler o k (o : xs) n r a
Always Bool
gk (forall (i' :: [Type]) (j' :: Nat) k'. a i' j' k' x -> b i' j' k' x
f a (o : xs) j k x
k)

instance Show (Fix4 (Instr o) xs n r a) where
  show :: Fix4 (Instr o) xs n r a -> String
show = (forall a b. (a -> b) -> a -> b
$ String
"") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: ([Type] -> Nat -> Type -> Type -> Type)
             -> [Type] -> Nat -> Type -> Type -> Type)
       (a :: [Type] -> Nat -> Type -> Type -> Type) (i :: [Type])
       (j :: Nat) k x.
IFunctor4 f =>
(forall (i' :: [Type]) (j' :: Nat) k'.
 f a i' j' k' x -> a i' j' k' x)
-> Fix4 f i j k x -> a i j k x
cata4 (forall {k} {k1} {k2} {k3} a (i :: k) (j :: k1) (k4 :: k2)
       (l :: k3).
a -> Const4 a i j k4 l
Const4 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (xs :: [Type]) (n :: Nat) r a.
Instr o (Const4 (String -> String)) xs n r a -> String -> String
alg)
    where
      alg :: forall xs n r a. Instr o (Const4 (String -> String)) xs n r a -> String -> String
      alg :: forall (xs :: [Type]) (n :: Nat) r a.
Instr o (Const4 (String -> String)) xs n r a -> String -> String
alg Instr o (Const4 (String -> String)) xs n r a
Ret                        = String -> String
"Ret"
      alg (Call MVar x
μ Const4 (String -> String) (x : xs) ('Succ n) r a
k)                 = String -> String
"(Call " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows MVar x
μ forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (x : xs) ('Succ n) r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Push Defunc x
x Const4 (String -> String) (x : xs) n r a
k)                 = String -> String
"(Push " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Defunc x
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (x : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Pop Const4 (String -> String) xs n r a
k)                    = String -> String
"(Pop " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Lift2 Defunc (x -> y -> z)
f Const4 (String -> String) (z : xs) n r a
k)                = String -> String
"(Lift2 " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Defunc (x -> y -> z)
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (z : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Sat CharPred
f Const4 (String -> String) (Char : xs) ('Succ n) r a
k)                  = String -> String
"(Sat " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows CharPred
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (Char : xs) ('Succ n) r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg Instr o (Const4 (String -> String)) xs n r a
Empt                       = String -> String
"Empt"
      alg (Commit Const4 (String -> String) xs n r a
k)                 = String -> String
"(Commit " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Catch Const4 (String -> String) xs ('Succ n) r a
p Handler o (Const4 (String -> String)) (o : xs) n r a
h)                = String -> String
"(Catch " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs ('Succ n) r a
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Handler o (Const4 (String -> String)) (o : xs) n r a
h forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Tell Const4 (String -> String) (o : xs) n r a
k)                   = String -> String
"(Tell " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (o : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Seek Const4 (String -> String) xs n r a
k)                   = String -> String
"(Seek " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Case Const4 (String -> String) (x : xs) n r a
p Const4 (String -> String) (y : xs) n r a
q)                 = String -> String
"(Case " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (x : xs) n r a
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (y : xs) n r a
q forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Choices [Defunc (x -> Bool)]
fs [Const4 (String -> String) xs n r a]
ks Const4 (String -> String) xs n r a
def)        = String -> String
"(Choices " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows [Defunc (x -> Bool)]
fs forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" [" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> [a -> a] -> a -> a
intercalateDiff String -> String
", " (forall a b. (a -> b) -> [a] -> [b]
map forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 [Const4 (String -> String) xs n r a]
ks) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
"] " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
def forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Iter MVar Void
μ Const4 (String -> String) '[] One Void a
l Handler o (Const4 (String -> String)) (o : xs) n r a
h)               = String -> String
"{Iter " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows MVar Void
μ forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) '[] One Void a
l forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Handler o (Const4 (String -> String)) (o : xs) n r a
h forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
"}"
      alg (Join ΦVar x
φ)                   = forall a. Show a => a -> String -> String
shows ΦVar x
φ
      alg (MkJoin ΦVar x
φ Const4 (String -> String) (x : xs) n r a
p Const4 (String -> String) xs n r a
k)             = String -> String
"(let " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows ΦVar x
φ forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" = " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (x : xs) n r a
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" in " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Swap Const4 (String -> String) (x : y : xs) n r a
k)                   = String -> String
"(Swap " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (x : y : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Dup Const4 (String -> String) (x : x : xs) n r a
k)                    = String -> String
"(Dup " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (x : x : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Make ΣVar x
σ Access
a Const4 (String -> String) xs n r a
k)               = String -> String
"(Make " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows ΣVar x
σ forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Access
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Get ΣVar x
σ Access
a Const4 (String -> String) (x : xs) n r a
k)                = String -> String
"(Get " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows ΣVar x
σ forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Access
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (x : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (Put ΣVar x
σ Access
a Const4 (String -> String) xs n r a
k)                = String -> String
"(Put " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows ΣVar x
σ forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Access
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (SelectPos PosSelector
Line Const4 (String -> String) (Int : xs) n r a
k)         = String -> String
"(Line " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (Int : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (SelectPos PosSelector
Col Const4 (String -> String) (Int : xs) n r a
k)          = String -> String
"(Col " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (Int : xs) n r a
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
")"
      alg (LogEnter String
_ Const4 (String -> String) xs ('Succ ('Succ n)) r a
k)             = forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs ('Succ ('Succ n)) r a
k
      alg (LogExit String
_ Const4 (String -> String) xs n r a
k)              = forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k
      alg (MetaInstr BlockCoins{} Const4 (String -> String) xs n r a
k) = forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k
      alg (MetaInstr MetaInstr n
m Const4 (String -> String) xs n r a
k)            = String -> String
"[" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows MetaInstr n
m forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
"] " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
k

instance Show (Handler o (Const4 (String -> String)) (o : xs) n r a) where
  show :: Handler o (Const4 (String -> String)) (o : xs) n r a -> String
show (Same Bool
_ Const4 (String -> String) xs n r a
yes Bool
_ Const4 (String -> String) (o : xs) n r a
no) = String -> String
"(Dup (Tell (Lift2 same (If " (forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) xs n r a
yes (String -> String
" " (forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (o : xs) n r a
no String
"))))")))
  show (Always Bool
_ Const4 (String -> String) (o : xs) n r a
k)      = forall {k1} {k2} {k3} {k4} a (i :: k1) (j :: k2) (k5 :: k3)
       (l :: k4).
Const4 a i j k5 l -> a
getConst4 Const4 (String -> String) (o : xs) n r a
k String
""

instance Show (MetaInstr n) where
  show :: MetaInstr n -> String
show (AddCoins Coins
n)    = String
"Add " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Coins
n forall a. [a] -> [a] -> [a]
++ String
" coins"
  show (RefundCoins Int
n) = String
"Refund " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
" coins"
  show (DrainCoins Int
n)  = String
"Using " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
" coins"
  show (GiveBursary Int
n) = String
"Bursary of " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
" coins"
  show BlockCoins{}    = String
""