diff --git a/LICENSE b/LICENSE
new file mode 100644
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
diff --git a/layered-state.cabal b/layered-state.cabal
new file mode 100644
--- /dev/null
+++ b/layered-state.cabal
@@ -0,0 +1,65 @@
+-- This file has been generated from package.yaml by hpack version 0.20.0.
+--
+-- see: https://github.com/sol/hpack
+--
+-- hash: 9aa62e9756668f40566d023e7ca6588ef4bf863fb729fa397c4cd36ad4fff77b
+
+name:           layered-state
+version:        1.1.2
+synopsis:       Control structure similar to Control.Monad.State, allowing multiple nested states, distinguishable by provided phantom types.
+category:       Data
+stability:      experimental
+homepage:       https://github.com/luna/layered-state
+bug-reports:    https://github.com/luna/layered-state/issues
+author:         Luna Team
+maintainer:     Wojciech Danilo <wojciech.danilo@luna-lang.org>
+copyright:      Copyright (C) 2018 Luna Team
+license:        Apache-2.0
+license-file:   LICENSE
+build-type:     Simple
+cabal-version:  >= 1.10
+
+library
+  hs-source-dirs:
+      src
+  default-extensions: AllowAmbiguousTypes ApplicativeDo BangPatterns BinaryLiterals ConstraintKinds DataKinds DefaultSignatures DeriveDataTypeable DeriveFoldable DeriveFunctor DeriveGeneric DeriveTraversable DoAndIfThenElse DuplicateRecordFields EmptyDataDecls FlexibleContexts FlexibleInstances FunctionalDependencies GeneralizedNewtypeDeriving InstanceSigs LambdaCase MonadComprehensions MultiWayIf NamedWildCards NegativeLiterals NoImplicitPrelude NumDecimals OverloadedLabels PackageImports QuasiQuotes PatternSynonyms RankNTypes RecursiveDo ScopedTypeVariables StandaloneDeriving TemplateHaskell TupleSections TypeApplications TypeFamilies TypeFamilyDependencies TypeOperators UnicodeSyntax ViewPatterns LiberalTypeSynonyms RelaxedPolyRec
+  ghc-options: -Wall -O2
+  build-depends:
+      base >=4.10 && <4.12
+    , constraints
+    , data-default
+    , exceptions
+    , lens
+    , lens-utils
+    , monad-branch
+    , monoid
+    , mtl
+    , primitive
+    , profunctors
+    , prologue
+    , transformers
+    , typelevel
+  exposed-modules:
+      Control.Monad.State.Layered
+  other-modules:
+      Paths_layered_state
+  default-language: Haskell2010
+
+benchmark layered-state-benchmark
+  type: exitcode-stdio-1.0
+  main-is: Main.hs
+  hs-source-dirs:
+      test/bench
+  default-extensions: AllowAmbiguousTypes ApplicativeDo BangPatterns BinaryLiterals ConstraintKinds DataKinds DefaultSignatures DeriveDataTypeable DeriveFoldable DeriveFunctor DeriveGeneric DeriveTraversable DoAndIfThenElse DuplicateRecordFields EmptyDataDecls FlexibleContexts FlexibleInstances FunctionalDependencies GeneralizedNewtypeDeriving InstanceSigs LambdaCase MonadComprehensions MultiWayIf NamedWildCards NegativeLiterals NoImplicitPrelude NumDecimals OverloadedLabels PackageImports QuasiQuotes PatternSynonyms RankNTypes RecursiveDo ScopedTypeVariables StandaloneDeriving TemplateHaskell TupleSections TypeApplications TypeFamilies TypeFamilyDependencies TypeOperators UnicodeSyntax ViewPatterns LiberalTypeSynonyms RelaxedPolyRec
+  ghc-options: -Wall -O2
+  build-depends:
+      criterion
+    , deepseq
+    , either
+    , kan-extensions
+    , layered-state
+    , mtl-c
+    , timeit
+  other-modules:
+      Paths_layered_state
+  default-language: Haskell2010
diff --git a/src/Control/Monad/State/Layered.hs b/src/Control/Monad/State/Layered.hs
new file mode 100644
--- /dev/null
+++ b/src/Control/Monad/State/Layered.hs
@@ -0,0 +1,237 @@
+{-# LANGUAGE ExplicitForAll         #-}
+{-# LANGUAGE FunctionalDependencies #-}
+{-# LANGUAGE ScopedTypeVariables    #-}
+{-# LANGUAGE TypeApplications       #-}
+{-# LANGUAGE UndecidableInstances   #-}
+{-# LANGUAGE AllowAmbiguousTypes    #-}
+{-# LANGUAGE TypeInType             #-}
+{-# LANGUAGE RankNTypes             #-}
+{-# LANGUAGE BangPatterns #-}
+{-# EXT      InlineAll              #-}
+-- {-# LANGUAGE Strict #-} -- https://ghc.haskell.org/trac/ghc/ticket/14815#ticket
+
+module Control.Monad.State.Layered where
+
+import Prelude hiding (Monad) -- hiding ((.)) -- https://ghc.haskell.org/trac/ghc/ticket/14001
+
+import Control.Applicative
+import Control.Lens.Utils hiding (Getter, Setter, Setter')
+import Control.Monad.Branch
+import Control.Monad.Catch
+import Control.Monad.Fail
+import Control.Monad.Identity hiding (Monad)
+import Control.Monad.IO.Class
+import Control.Monad.Primitive
+import Control.Monad.Trans
+import Control.Monad.Trans.Maybe
+import Data.Constraint
+import Data.Default
+import Data.Kind (Type)
+import Type.Bool
+
+import qualified Prelude as P
+import qualified Control.Monad.State.Strict as S
+
+type M = P.Monad
+
+
+-------------------
+-- === State === --
+-------------------
+
+-- === Definition === --
+
+type    State  s     = StateT s Identity
+newtype StateT s m a = StateT (S.StateT s m a)
+    deriving ( Applicative, Alternative, Functor, M, MonadFail, MonadFix
+             , MonadIO, MonadPlus, MonadTrans, MonadThrow, MonadBranch )
+makeWrapped ''StateT
+
+type        States  ss = StatesT ss Identity
+type family StatesT ss m where
+    StatesT '[]       m = m
+    StatesT (s ': ss) m = StateT s (StatesT ss m)
+
+
+-- === State data discovery === --
+
+type        InferStateData    (l :: k) m = InferStateData' k l m
+type family InferStateData' k (l :: k) m where
+    InferStateData' Type l m = l
+    InferStateData' k    l m = StateData l m
+
+type TransStateData l t m = (InferStateData l (t m) ~ InferStateData l m)
+
+type family StateData l m where
+    StateData l (StateT s m) = If (MatchedBases l s) s (StateData l m)
+    StateData l (t m)        = StateData l m
+
+type family MatchedBases (a :: ka) (b :: kb) :: Bool where
+    MatchedBases (a :: k) (b   :: k) = a == b
+    MatchedBases (a :: k) (b t :: l) = MatchedBases a b
+    MatchedBases (a :: k) (b   :: l) = 'False
+
+
+-- === Monad === --
+
+-- Definitions
+
+type         Monad  l m = (Getter l m, Setter l m)
+class M m => Getter l m where get :: m (InferStateData l m)
+class M m => Setter l m where put :: InferStateData l m -> m ()
+
+-- Instancess
+
+instance                       M m                                                      => Getter (l :: Type) (StateT l m) where get   = wrap   S.get    ; {-# INLINE get #-}
+instance                       M m                                                      => Setter (l :: Type) (StateT l m) where put a = wrap $ S.put a  ; {-# INLINE put #-}
+instance {-# OVERLAPPABLE #-}  Getter l m                                               => Getter (l :: Type) (StateT s m) where get   = lift $ get @l   ; {-# INLINE get #-}
+instance {-# OVERLAPPABLE #-}  Setter l m                                               => Setter (l :: Type) (StateT s m) where put a = lift $ put @l a ; {-# INLINE put #-}
+instance {-# OVERLAPPABLE #-} (M m, Getter__ ok l (StateT s m), ok ~ MatchedBases l s)  => Getter (l :: k)    (StateT s m) where get   = get__  @ok @l   ; {-# INLINE get #-}
+instance {-# OVERLAPPABLE #-} (M m, Setter__ ok l (StateT s m), ok ~ MatchedBases l s)  => Setter (l :: k)    (StateT s m) where put a = put__  @ok @l a ; {-# INLINE put #-}
+instance {-# OVERLAPPABLE #-} (M (t m), MonadTrans t, Getter l m, TransStateData l t m) => Getter (l :: k)    (t m)        where get   = lift $ get @l   ; {-# INLINE get #-}
+instance {-# OVERLAPPABLE #-} (M (t m), MonadTrans t, Setter l m, TransStateData l t m) => Setter (l :: k)    (t m)        where put a = lift $ put @l a ; {-# INLINE put #-}
+
+-- Helpers
+
+class M m => Getter__ (ok :: Bool) l m where get__ :: m (InferStateData l m)
+class M m => Setter__ (ok :: Bool) l m where put__ :: InferStateData l m -> m ()
+
+instance (M m, InferStateData l (StateT s m) ~ s)    => Getter__ 'True  l (StateT s m) where get__   = get @s          ; {-# INLINE get__ #-}
+instance (M m, InferStateData l (StateT s m) ~ s)    => Setter__ 'True  l (StateT s m) where put__ a = put @s a        ; {-# INLINE put__ #-}
+instance (Getter l m, TransStateData l (StateT s) m) => Getter__ 'False l (StateT s m) where get__   = lift $ get @l   ; {-# INLINE get__ #-}
+instance (Setter l m, TransStateData l (StateT s) m) => Setter__ 'False l (StateT s m) where put__ a = lift $ put @l a ; {-# INLINE put__ #-}
+
+-- Replicators
+
+type MonadStates  ss m = (Getters ss m, Setters ss m)
+type Getters ss m = Monads__ Getter ss m
+type Setters ss m = Monads__ Setter ss m
+type family Monads__ p ss m :: Constraint where
+    Monads__ p (s ': ss) m = (p s m, Monads__ p ss m)
+    Monads__ p '[]       m = ()
+
+
+-- === Accessing === --
+
+gets :: ∀ l m s a. (Getter l m, s ~ InferStateData l m)
+     => Lens' s a -> m a
+gets l = view l <$> get @l ; {-# INLINE gets #-}
+
+
+
+-- === Top state accessing === --
+
+type family TopStateData m where
+    TopStateData (StateT s m) = s
+    TopStateData (t m)        = TopStateData m
+
+type Monad'  m = (Getter' m, Setter' m)
+type Getter' m = Getter (TopStateData m) m
+type Setter' m = Setter (TopStateData m) m
+
+get' :: ∀ m. Getter' m => m (TopStateData m)
+put' :: ∀ m. Setter' m => TopStateData m -> m ()
+get' = get @(TopStateData m) ; {-# INLINE get' #-}
+put' = put @(TopStateData m) ; {-# INLINE put' #-}
+
+gets' :: ∀ m s a. (Getter' m, s ~ TopStateData m) => Lens' s a -> m a
+gets' l = view l <$> get' ; {-# INLINE gets' #-}
+
+
+-- === Construction & running === --
+
+stateT :: (s -> m (a,s)) -> StateT s m a
+stateT = StateT . S.StateT ; {-# INLINE stateT #-}
+
+runT  :: ∀ s m a.              StateT s m a -> s -> m (a, s)
+evalT :: ∀ s m a. Functor m => StateT s m a -> s -> m a
+execT :: ∀ s m a. Functor m => StateT s m a -> s -> m s
+runT    = S.runStateT  . unwrap ; {-# INLINE runT  #-}
+evalT m = fmap fst . runT m     ; {-# INLINE evalT #-}
+execT m = fmap snd . runT m     ; {-# INLINE execT #-}
+
+runDefT  :: ∀ s m a.            Default s  => StateT s m a -> m (a, s)
+evalDefT :: ∀ s m a. (Functor m,Default s) => StateT s m a -> m a
+execDefT :: ∀ s m a. (Functor m,Default s) => StateT s m a -> m s
+runDefT  = flip runT  def ; {-# INLINE runDefT  #-}
+evalDefT = flip evalT def ; {-# INLINE evalDefT #-}
+execDefT = flip execT def ; {-# INLINE execDefT #-}
+
+run  :: ∀ s a. State s a -> s -> (a, s)
+eval :: ∀ s a. State s a -> s -> a
+exec :: ∀ s a. State s a -> s -> s
+run  = S.runState  . unwrap ; {-# INLINE run  #-}
+eval = S.evalState . unwrap ; {-# INLINE eval #-}
+exec = S.execState . unwrap ; {-# INLINE exec #-}
+
+runDef  :: ∀ s a. Default s => State s a -> (a, s)
+evalDef :: ∀ s a. Default s => State s a -> a
+execDef :: ∀ s a. Default s => State s a -> s
+runDef  = flip run  def ; {-# INLINE runDef  #-}
+evalDef = flip eval def ; {-# INLINE evalDef #-}
+execDef = flip exec def ; {-# INLINE execDef #-}
+
+
+-- === Generic state modification === --
+
+type InferMonadState s l m = (Monad l m, s ~ InferStateData l m)
+modifyM  :: ∀ l s m a. InferMonadState s l m => (s -> m (a, s)) -> m a
+modifyM_ :: ∀ l s m a. InferMonadState s l m => (s -> m     s)  -> m ()
+modify   :: ∀ l s m a. InferMonadState s l m => (s ->   (a, s)) -> m a
+modify_  :: ∀ l s m a. InferMonadState s l m => (s ->       s)  -> m ()
+modify    = modifyM  @l . fmap return       ; {-# INLINE modify   #-}
+modify_   = modifyM_ @l . fmap return       ; {-# INLINE modify_  #-}
+modifyM_  = modifyM  @l . (fmap.fmap) ((),) ; {-# INLINE modifyM_ #-}
+modifyM f = do (!a,!t) <- f =<< get @l
+               a <$ put @l t
+{-# INLINE modifyM #-}
+
+sub           :: ∀ l s m a. InferMonadState s l m =>               m a -> m a
+with          :: ∀ l s m a. InferMonadState s l m => s          -> m a -> m a
+withModified  :: ∀ l s m a. InferMonadState s l m => (s ->   s) -> m a -> m a
+withModifiedM :: ∀ l s m a. InferMonadState s l m => (s -> m s) -> m a -> m a
+with              = withModified  @l . const       ; {-# INLINE with          #-}
+withModified      = withModifiedM @l . fmap return ; {-# INLINE withModified  #-}
+withModifiedM f m = sub @l $ modifyM_ @l f >> m    ; {-# INLINE withModifiedM #-}
+sub             m = do s <- get @l
+                       m <* put @l s
+{-#INLINE sub #-}
+
+
+-- === Top level state modification === --
+
+type TopMonadState s m = (Monad' m, s ~ TopStateData m)
+modifyM'  :: ∀ s m a. TopMonadState s m => (s -> m (a, s)) -> m a
+modifyM'_ :: ∀ s m a. TopMonadState s m => (s -> m     s)  -> m ()
+modify'   :: ∀ s m a. TopMonadState s m => (s ->   (a, s)) -> m a
+modify'_  :: ∀ s m a. TopMonadState s m => (s ->       s)  -> m ()
+modify'    = modifyM'  . fmap return       ; {-# INLINE modify'   #-}
+modify'_   = modifyM'_ . fmap return       ; {-# INLINE modify'_  #-}
+modifyM'_  = modifyM'  . (fmap.fmap) ((),) ; {-# INLINE modifyM'_ #-}
+modifyM' f = do (!a,!t) <- f =<< get'
+                a <$ put' t
+{-# INLINE modifyM' #-}
+
+sub'           :: ∀ s m a. TopMonadState s m =>               m a -> m a
+with'          :: ∀ s m a. TopMonadState s m => s          -> m a -> m a
+withModified'  :: ∀ s m a. TopMonadState s m => (s ->   s) -> m a -> m a
+withModifiedM' :: ∀ s m a. TopMonadState s m => (s -> m s) -> m a -> m a
+with'              = withModified'  . const       ; {-# INLINE with'          #-}
+withModified'      = withModifiedM' . fmap return ; {-# INLINE withModified'  #-}
+withModifiedM' f m = sub' $ modifyM'_ f >> m      ; {-# INLINE withModifiedM' #-}
+sub'             m = do s <- get'
+                        m <* put' s
+{-# INLINE sub' #-}
+
+
+-- === Other modifications === --
+
+mapT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
+mapT f = _Wrapped %~ S.mapStateT f ; {-# INLINE mapT #-}
+
+
+-- === Instances === --
+
+instance PrimMonad m => PrimMonad (StateT s m) where
+  type PrimState (StateT s m) = PrimState m
+  primitive = lift . primitive ; {-# INLINE primitive #-}
diff --git a/test/bench/Main.hs b/test/bench/Main.hs
new file mode 100644
--- /dev/null
+++ b/test/bench/Main.hs
@@ -0,0 +1,219 @@
+{-# LANGUAGE OverloadedStrings   #-}
+{-# LANGUAGE BangPatterns        #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+-- {-# LANGUAGE Strict #-}
+
+module Main where
+
+import Prelude as P
+import Criterion.Main
+import Control.Monad.State.Layered hiding ((.))
+import qualified Control.Monad.State.Strict as S
+import Control.Monad.Codensity
+import qualified Control.Monad.State.CPS as CPS
+import Data.Word
+import Control.Monad.Identity
+import GHC.IO          as X (evaluate)
+import Control.DeepSeq
+import System.TimeIt
+import System.Environment (getArgs)
+import System.IO (stdout, hSetBuffering, BufferMode(..))
+import Control.Monad.Trans.Either
+import Data.IORef
+import Foreign.ForeignPtr
+import Foreign.Storable (peek, poke)
+
+eval :: NFData a => a -> IO a
+eval = evaluate . force ; {-# INLINE eval #-}
+
+
+
+
+mtlIncState :: S.MonadState Int m => m ()
+mtlIncState = S.modify (1+) ; {-# INLINE mtlIncState #-}
+{-# SPECIALIZE mtlIncState :: S.State Int () #-}
+
+
+-- | incLoop transformation needed because of bug https://ghc.haskell.org/trac/ghc/ticket/14062
+incLoop  :: MonadState  Int          m => Int -> m Int
+incLoop2 :: MonadStates '[Int, Word] m => Int -> m (Int, Word)
+incLoop  n = repeatM incState  n >> get @Int                         ; {-# INLINE incLoop  #-}
+incLoop2 n = repeatM incState2 n >> ((,) <$> get @Int <*> get @Word) ; {-# INLINE incLoop2 #-}
+
+incState  :: MonadState  Int          m => m ()
+incState2 :: MonadStates '[Int, Word] m => m ()
+incState  = modify_ @Int (1+)                       ; {-# INLINE incState  #-}
+incState2 = modify_ @Int (1+) >> modify_ @Word (1+) ; {-# INLINE incState2 #-}
+
+repeatM :: Monad m => m a -> Int -> m ()
+repeatM f = go where
+    go 0 = pure ()
+    go i = f >> go (i - 1)
+{-# INLINE repeatM #-}
+
+pureInc :: Int -> Int -> Int
+pureInc !a !i = case i of
+    0 -> a
+    _ -> pureInc (a + 1) (i - 1)
+
+pureInc2 :: Int -> Word -> Int -> (Int, Word)
+pureInc2 !a !b i = case i of
+    0 -> (a,b)
+    _ -> pureInc2 (a + 1) (b + 1) (i - 1)
+
+iorefInc :: Int -> IO Int
+iorefInc !i = do
+    !ref <- newIORef (0 :: Int)
+    let go = \case
+            0 -> return ()
+            j -> modifyIORef' ref (+1) >> go (j-1)
+    go i
+    readIORef ref
+{-# INLINE iorefInc #-}
+
+fpInc :: Int -> IO ()
+fpInc !i = do
+    !(ptr :: ForeignPtr Int) <- mallocForeignPtr
+    let go = \case
+            0 -> return ()
+            j -> do
+                withForeignPtr ptr $ \ !p -> do
+                    val <- peek p
+                    poke p (val + 1)
+                go (j - 1)
+    go i
+{-# INLINE fpInc #-}
+
+
+sInt :: Functor m => StateT Int    m a -> m a
+sWrd :: Functor m => StateT Word   m a -> m a
+sStr :: Functor m => StateT String m a -> m a
+sChr :: Functor m => StateT Char   m a -> m a
+sTup :: Functor m => StateT ()     m a -> m a
+sInt = flip (evalStateT @Int)    0   ; {-# INLINE sInt #-}
+sWrd = flip (evalStateT @Word)   0   ; {-# INLINE sWrd #-}
+sStr = flip (evalStateT @String) ""  ; {-# INLINE sStr #-}
+sChr = flip (evalStateT @Char)   'x' ; {-# INLINE sChr #-}
+sTup = flip (evalStateT @())     ()  ; {-# INLINE sTup #-}
+
+t_0 :: Int -> ()
+t_0 = runIdentity . sInt . repeatM incState ; {-# INLINE t_0 #-}
+
+t_1R, t_2R, t_3R :: Int -> Int
+t_1R = runIdentity . sInt . sStr               . incLoop ; {-# INLINE t_1R #-}
+t_2R = runIdentity . sInt . sStr . sChr        . incLoop ; {-# INLINE t_2R #-}
+t_3R = runIdentity . sInt . sStr . sChr . sTup . incLoop ; {-# INLINE t_3R #-}
+
+t_1L, t_2L, t_3L :: Int -> Int
+t_1L = runIdentity               . sStr . sInt . incLoop ; {-# INLINE t_1L #-}
+t_2L = runIdentity        . sChr . sStr . sInt . incLoop ; {-# INLINE t_2L #-}
+t_3L = runIdentity . sTup . sChr . sStr . sInt . incLoop ; {-# INLINE t_3L #-}
+
+t_1R2, t_2R2, t_3R2 :: Int -> (Int,Word)
+t_1R2 = runIdentity . sInt . sWrd . sStr               . incLoop2 ; {-# INLINE t_1R2 #-}
+t_2R2 = runIdentity . sInt . sWrd . sStr . sChr        . incLoop2 ; {-# INLINE t_2R2 #-}
+t_3R2 = runIdentity . sInt . sWrd . sStr . sChr . sTup . incLoop2 ; {-# INLINE t_3R2 #-}
+
+t_1L2, t_2L2, t_3L2 :: Int -> (Int, Word)
+t_1L2 = runIdentity               . sStr . sInt . sWrd . incLoop2 ; {-# INLINE t_1L2 #-}
+t_2L2 = runIdentity        . sChr . sStr . sInt . sWrd . incLoop2 ; {-# INLINE t_2L2 #-}
+t_3L2 = runIdentity . sTup . sChr . sStr . sInt . sWrd . incLoop2 ; {-# INLINE t_3L2 #-}
+
+t_1R2E, t_2R2E, t_3R2E :: Int -> Either () (Int,Word)
+t_1R2E = runIdentity . sInt . sWrd . sStr               . runEitherT . incLoop2 ; {-# INLINE t_1R2E #-}
+t_2R2E = runIdentity . sInt . sWrd . sStr . sChr        . runEitherT . incLoop2 ; {-# INLINE t_2R2E #-}
+t_3R2E = runIdentity . sInt . sWrd . sStr . sChr . sTup . runEitherT . incLoop2 ; {-# INLINE t_3R2E #-}
+
+t_1L2E, t_2L2E, t_3L2E :: Int -> Either () (Int, Word)
+t_1L2E = runIdentity . runEitherT               . sStr . sInt . sWrd . incLoop2 ; {-# INLINE t_1L2E #-}
+t_2L2E = runIdentity . runEitherT        . sChr . sStr . sInt . sWrd . incLoop2 ; {-# INLINE t_2L2E #-}
+t_3L2E = runIdentity . runEitherT . sTup . sChr . sStr . sInt . sWrd . incLoop2 ; {-# INLINE t_3L2E #-}
+
+t_1R2Eb, t_2R2Eb, t_3R2Eb :: Int -> Either () (Int,Word)
+t_1R2Eb = runIdentity . sInt . runEitherT . sWrd . sStr               . incLoop2 ; {-# INLINE t_1R2Eb #-}
+t_2R2Eb = runIdentity . sInt . runEitherT . sWrd . sStr . sChr        . incLoop2 ; {-# INLINE t_2R2Eb #-}
+t_3R2Eb = runIdentity . sInt . runEitherT . sWrd . sStr . sChr . sTup . incLoop2 ; {-# INLINE t_3R2Eb #-}
+
+t_1L2Eb, t_2L2Eb, t_3L2Eb :: Int -> Either () (Int, Word)
+t_1L2Eb = runIdentity               . sStr . sInt . runEitherT . sWrd . incLoop2 ; {-# INLINE t_1L2Eb #-}
+t_2L2Eb = runIdentity        . sChr . sStr . sInt . runEitherT . sWrd . incLoop2 ; {-# INLINE t_2L2Eb #-}
+t_3L2Eb = runIdentity . sTup . sChr . sStr . sInt . runEitherT . sWrd . incLoop2 ; {-# INLINE t_3L2Eb #-}
+
+main :: IO ()
+main = do
+    hSetBuffering stdout NoBuffering
+
+    -- let s = "1000000000" :: String
+    --     x = read s :: Int
+    --
+    -- putStrLn "Single counter loop"
+    -- putStr "pure  " >> timeIt (eval (pureInc 0 x))
+    -- putStr "t_0   " >> timeIt (eval (t_0  x))
+    -- putStr "t_1R  " >> timeIt (eval (t_1R x))
+    -- putStr "t_2R  " >> timeIt (eval (t_2R x))
+    -- putStr "t_3R  " >> timeIt (eval (t_3R x))
+    -- putStr "t_1L  " >> timeIt (eval (t_1L x))
+    -- putStr "t_2L  " >> timeIt (eval (t_2L x))
+    -- putStr "t_3L  " >> timeIt (eval (t_3L x))
+    --
+    -- putStrLn "\nDouble counter loop"
+    -- putStr "pure  " >> timeIt (eval (pureInc2 0 0 x))
+    -- putStr "t_1R2 " >> timeIt (eval (t_1R2 x))
+    -- putStr "t_2R2 " >> timeIt (eval (t_2R2 x))
+    -- putStr "t_3R2 " >> timeIt (eval (t_3R2 x))
+    -- putStr "t_1L2 " >> timeIt (eval (t_1L2 x))
+    -- putStr "t_2L2 " >> timeIt (eval (t_2L2 x))
+    -- putStr "t_3L2 " >> timeIt (eval (t_3L2 x))
+    --
+    -- putStrLn "\nDouble counter loop n EitherT"
+    -- putStr "t_1R2E " >> timeIt (eval (t_1R2E x))
+    -- putStr "t_2R2E " >> timeIt (eval (t_2R2E x))
+    -- putStr "t_3R2E " >> timeIt (eval (t_3R2E x))
+    -- putStr "t_1L2E " >> timeIt (eval (t_1L2E x))
+    -- putStr "t_2L2E " >> timeIt (eval (t_2L2E x))
+    -- putStr "t_3L2E " >> timeIt (eval (t_3L2E x))
+
+    let iterCount = 100000000
+
+    putStrLn "\nTesting time overhead of monad transformers"
+    putStrLn "IMPORTANT: These times should be THE SAME. If they are not, you've broken then implementation. Go back and fix it."
+
+    putStrLn "\n\n=== Single counter loop ===\n"
+    defaultMain [ bench "IORef    (10e6)" $ nfIO (iorefInc iterCount)
+                , bench "FPtr     (10e6)" $ nfIO (fpInc iterCount)
+                , bench "pure     (10e6)" $ nf (pureInc 0) iterCount
+                , bench "1R trans (10e6)" $ nf t_1R iterCount
+                , bench "2R trans (10e6)" $ nf t_2R iterCount
+                , bench "3R trans (10e6)" $ nf t_3R iterCount
+                , bench "1L trans (10e6)" $ nf t_1L iterCount
+                , bench "2L trans (10e6)" $ nf t_2L iterCount
+                , bench "3L trans (10e6)" $ nf t_3L iterCount
+                ]
+
+    putStrLn "\n\n=== Double counter loop ===\n"
+    defaultMain [ bench "pure     (10e6)" $ nf (pureInc2 0 0) iterCount
+                , bench "1R trans (10e6)" $ nf t_1R2 iterCount
+                , bench "2R trans (10e6)" $ nf t_2R2 iterCount
+                , bench "3R trans (10e6)" $ nf t_3R2 iterCount
+                , bench "1L trans (10e6)" $ nf t_1L2 iterCount
+                , bench "2L trans (10e6)" $ nf t_2L2 iterCount
+                , bench "3L trans (10e6)" $ nf t_3L2 iterCount
+                ]
+
+    putStrLn "\n\n=== Double counter loop in EitherT ===\n"
+    defaultMain [ bench "1R trans (10e6)" $ nf t_1R2E iterCount
+                , bench "2R trans (10e6)" $ nf t_2R2E iterCount
+                , bench "3R trans (10e6)" $ nf t_3R2E iterCount
+                , bench "1L trans (10e6)" $ nf t_1L2E iterCount
+                , bench "2L trans (10e6)" $ nf t_2L2E iterCount
+                , bench "3L trans (10e6)" $ nf t_3L2E iterCount
+                ]
+
+    putStrLn "\n\n=== Double counter loop with EitherT in-between ===\n"
+    defaultMain [ bench "1R trans (10e6)" $ nf t_1R2Eb iterCount
+                , bench "2R trans (10e6)" $ nf t_2R2Eb iterCount
+                , bench "3R trans (10e6)" $ nf t_3R2Eb iterCount
+                , bench "1L trans (10e6)" $ nf t_1L2Eb iterCount
+                , bench "2L trans (10e6)" $ nf t_2L2Eb iterCount
+                , bench "3L trans (10e6)" $ nf t_3L2Eb iterCount
+                ]
