on 7bceff2 help sign in
generic-lens/examples/Examples.hs
132 lines 3.6 kB history raw
1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE DuplicateRecordFields #-}
5{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE GADTs #-}
7{-# LANGUAGE NoMonomorphismRestriction #-}
8{-# LANGUAGE OverloadedLabels #-}
9{-# LANGUAGE PartialTypeSignatures #-}
10{-# LANGUAGE Rank2Types #-}
11{-# LANGUAGE ScopedTypeVariables #-}
12{-# LANGUAGE TypeApplications #-}
13{-# LANGUAGE UndecidableInstances #-}
14{-# OPTIONS_GHC -Wno-missing-signatures #-}
15{-# OPTIONS_GHC -fno-warn-unused-imports #-}
17module Examples where
19import Data.Function ((&))
20import Data.Generics.Internal.VL.Lens
21import Data.Generics.Product
22import Data.Generics.Sum
23import GHC.Generics
24import Data.Generics.Labels
25import Data.Generics.Internal.VL.Iso
26import Data.Generics.Internal.VL.Prism
27import Data.Generics.Internal.Profunctor.Lens
28import Data.Generics.Internal.Profunctor.Iso
29import Data.Generics.Internal.Profunctor.Prism
31data Animal = Animal
32 { name :: String
33 , age :: Int
34 , eats :: String
35 } deriving (Show, Generic)
37data Human = Human
38 { name :: String
39 , age :: Int
40 , address :: String
41 , eats :: String
42 } deriving (Show, Generic)
44data Living
45 = Animal' { name :: String, eats :: String, age :: Int }
46 | Human' { name :: String, age :: Int, address :: String, eats :: String }
47 deriving (Show, Generic)
49toby :: Human
50toby = Human { name = "Toby", age = 10, address = "London", eats = "Bread" }
52growUp :: Animal -> Animal
53growUp (Animal n a _) = Animal n (a + 10) "raw meat"
55data MyRecord = MyRecord { field1 :: Int, field2 :: String } deriving Generic
57--g :: Subtype s MyRecord => s -> String
58--g s = s ^. super @MyRecord . label @"field2"
60data Test a b = Test { fieldInt :: Int, fieldA :: a, fieldB :: b } deriving (Generic, Show)
62-- changedA :: Test Int String
63-- >>> changedA
64-- Test {fieldInt = 10, fieldA = 10, fieldB = "world"}
65changedA = Test 10 "hello" "world" & field @"fieldA" .~ (10 :: Int)
67-- changedB :: Test String Int
68-- >>> changedB
69-- Test {fieldInt = 10, fieldA = "hello", fieldB = 10}
70changedB = (Test 10 "hello" "world") & field @"fieldB" .~ (10 :: Int)
72--changedInt = set (field @"fieldInt") ("hello") (Test 10 "hello" "world")
73-- type error
75data Animal2 a
76 = Dog (Dog a)
77 | Cat Name Age
78 | Duck Age
79 deriving (Generic, Show)
81data Dog a
82 = MkDog
83 { name :: Name
84 , age :: Age
85 , fieldA :: a
86 }
87 deriving (Generic, Show)
88type Name = String
89type Age = Int
90dog :: Animal2 Int
91dog = Dog (MkDog "Shep" 3 30)
93-- TODO: the error message for this case is ugly
94-- data Dog a
95-- = MkDog
96-- { name :: Name
97-- , age :: Age
98-- , fieldA :: a
99-- , fieldA' :: a
100-- }
101-- deriving (Generic, Show)
103--dog' :: Animal2 String
104dog' = dog & _Ctor @"Dog" . field @"fieldA" .~ "now it's a String"
106stuff ::
107 ( HasPosition 15 s t a String
108 , HasField "test" s' t' a' b'
109 , HasField "bar" a' b' s t
110 ) => s' -> t'
111stuff r = r & field @"test" . field @"bar" . position @15 .~ "hello"
113stuff' ::
114 ( HasPosition 15 s t a String
115 , HasField "test" s' t' a' b'
116 , HasField "bar" a' b' s t
117 ) => s' -> t'
118stuff' r = r & #test . #bar . position @15 .~ "hello"
120data Foo m s = Foo
121 { foo1 :: m s
122 , foo2 :: [s]
123 } deriving Generic
125modifyFoo2 :: Foo (Either String) Int -> Foo Maybe Int
126modifyFoo2 x = x & field @"foo1" .~ pure (1 :: Int)
128data Bar a b = Bar
129 { barField :: (a, b)
130 } deriving Generic
132modifiedBar = (Bar ("hello", "world")) & field @"barField" .~ ('c', 1 :: Int)