diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 0.4.0.1
+
+* Ported a lens commit that (probably) makes lens generation deterministic. See [this issue](https://github.com/aelve/microlens/issues/83).
+
 # 0.4.0.0
 
 * Added `makeClassy` (and `createClass`).
diff --git a/microlens-th.cabal b/microlens-th.cabal
--- a/microlens-th.cabal
+++ b/microlens-th.cabal
@@ -1,5 +1,5 @@
 name:                microlens-th
-version:             0.4.0.0
+version:             0.4.0.1
 synopsis:            Automatic generation of record lenses for microlens
 description:
   This package lets you automatically generate lenses for data types; code was extracted from the lens package, and therefore generated lenses are fully compatible with ones generated by lens (and can be used both from lens and microlens).
diff --git a/src/Lens/Micro/TH.hs b/src/Lens/Micro/TH.hs
--- a/src/Lens/Micro/TH.hs
+++ b/src/Lens/Micro/TH.hs
@@ -61,7 +61,6 @@
 import           Data.Char
 import           Data.Data
 import           Data.Either
-import           Data.Foldable (toList)
 import qualified Data.Map as Map
 import           Data.Map (Map)
 import           Data.Monoid
@@ -1295,16 +1294,17 @@
 -- Template Haskell wants type variables declared in a forall, so
 -- we find all free type variables in a given type and declare them.
 quantifyType :: Cxt -> Type -> Type
-quantifyType c t = ForallT vs c t
-  where
-  vs = map PlainTV (toList (setOf typeVars t))
+quantifyType = quantifyType' Set.empty
 
 -- This function works like 'quantifyType' except that it takes
 -- a list of variables to exclude from quantification.
 quantifyType' :: Set Name -> Cxt -> Type -> Type
 quantifyType' exclude c t = ForallT vs c t
   where
-  vs = map PlainTV (toList (setOf typeVars t Set.\\ exclude))
+    vs = map PlainTV
+       $ filter (`Set.notMember` exclude)
+       $ nub -- stable order
+       $ toListOf typeVars t
 
 
 ------------------------------------------------------------------------
