Skip to content

Instantly share code, notes, and snippets.

@igrep
Created June 6, 2019 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrep/19139bb4e3bc7fe7988597559b9ab2dd to your computer and use it in GitHub Desktop.
Save igrep/19139bb4e3bc7fe7988597559b9ab2dd to your computer and use it in GitHub Desktop.
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
import Control.Arrow (first)
import Data.Generics.Product.Fields (field')
import Lens.Micro (Lens', (^.), (.~), (&))
import GHC.Generics (Generic)
data Sample = Sample
{ sample1 :: Int
, sample2 :: Int
} deriving (Generic, Show)
viewThenSet :: Sample -> Lens' Sample Int -> (Int, Sample)
viewThenSet sample lensInt = (sample ^. lensInt, sample & lensInt .~ 10)
main :: IO ()
main = print . viewThenSet (Sample 1 2) $ field' @"sample1"
-- ^ Replacing this (.) with ($) fixes the type error:
{-
> stack exec ghc -- --version
The Glorious Glasgow Haskell Compilation System, version 8.6.4
> stack exec runghc .\lens-composition.hs
lens-composition.hs:27:16: error:
• Couldn't match type ‘f’ with ‘f0’
‘f’ is a rigid type variable bound by
a type expected by the context:
Lens' Sample Int
at lens-composition.hs:27:16-39
Expected type: ((Int -> f0 Int) -> Sample -> f0 Sample)
-> (Int, Sample)
Actual type: Lens' Sample Int -> (Int, Sample)
• In the second argument of ‘(.)’, namely
‘viewThenSet (Sample 1 2)’
In the expression: print . viewThenSet (Sample 1 2)
In the expression:
print . viewThenSet (Sample 1 2) $ field' @"sample1"
|
27 | main = print . viewThenSet (Sample 1 2) $ field' @"sample1"
| ^^^^^^^^^^^^^^^^^^^^^^^^
lens-composition.hs:27:43: error:
• Ambiguous type variable ‘f0’ arising from a use of ‘field'’
prevents the constraint ‘(Functor f0)’ from being solved.
Probable fix: use a type annotation to specify what ‘f0’ should be.
These potential instances exist:
instance Functor (Either a) -- Defined in ‘Data.Either’
instance Functor IO -- Defined in ‘GHC.Base’
instance Functor Maybe -- Defined in ‘GHC.Base’
...plus two others
...plus 57 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the second argument of ‘($)’, namely ‘field' @"sample1"’
In the expression:
print . viewThenSet (Sample 1 2) $ field' @"sample1"
In an equation for ‘main’:
main = print . viewThenSet (Sample 1 2) $ field' @"sample1"
|
27 | main = print . viewThenSet (Sample 1 2) $ field' @"sample1"
| ^^^^^^^^^^^^^^^^^
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment