on 2279d08 help sign in
generic-lens/examples/Examples.hs
127 lines 3.4 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.Lens
21import Data.Generics.Product
22import Data.Generics.Sum
23import GHC.Generics
24import Data.Generics.Labels
26data Animal = Animal
27 { name :: String
28 , age :: Int
29 , eats :: String
30 } deriving (Show, Generic)
32data Human = Human
33 { name :: String
34 , age :: Int
35 , address :: String
36 , eats :: String
37 } deriving (Show, Generic)
39data Living
40 = Animal' { name :: String, eats :: String, age :: Int }
41 | Human' { name :: String, age :: Int, address :: String, eats :: String }
42 deriving (Show, Generic)
44toby :: Human
45toby = Human { name = "Toby", age = 10, address = "London", eats = "Bread" }
47growUp :: Animal -> Animal
48growUp (Animal n a _) = Animal n (a + 10) "raw meat"
50data MyRecord = MyRecord { field1 :: Int, field2 :: String } deriving Generic
52--g :: Subtype s MyRecord => s -> String
53--g s = s ^. super @MyRecord . label @"field2"
55data Test a b = Test { fieldInt :: Int, fieldA :: a, fieldB :: b } deriving (Generic, Show)
57-- changedA :: Test Int String
58-- >>> changedA
59-- Test {fieldInt = 10, fieldA = 10, fieldB = "world"}
60changedA = Test 10 "hello" "world" & field @"fieldA" .~ (10 :: Int)
62-- changedB :: Test String Int
63-- >>> changedB
64-- Test {fieldInt = 10, fieldA = "hello", fieldB = 10}
65changedB = (Test 10 "hello" "world") & field @"fieldB" .~ (10 :: Int)
67--changedInt = set (field @"fieldInt") ("hello") (Test 10 "hello" "world")
68-- type error
70data Animal2 a
71 = Dog (Dog a)
72 | Cat Name Age
73 | Duck Age
74 deriving (Generic, Show)
76data Dog a
77 = MkDog
78 { name :: Name
79 , age :: Age
80 , fieldA :: a
81 }
82 deriving (Generic, Show)
83type Name = String
84type Age = Int
85dog :: Animal2 Int
86dog = Dog (MkDog "Shep" 3 30)
88-- TODO: the error message for this case is ugly
89-- data Dog a
90-- = MkDog
91-- { name :: Name
92-- , age :: Age
93-- , fieldA :: a
94-- , fieldA' :: a
95-- }
96-- deriving (Generic, Show)
98--dog' :: Animal2 String
99dog' = dog & _Ctor @"Dog" . field @"fieldA" .~ "now it's a String"
101stuff ::
102 ( HasPosition 15 s t a String
103 , HasField "test" s' t' a' b'
104 , HasField "bar" a' b' s t
105 ) => s' -> t'
106stuff r = r & field @"test" . field @"bar" . position @15 .~ "hello"
108stuff' ::
109 ( HasPosition 15 s t a String
110 , HasField "test" s' t' a' b'
111 , HasField "bar" a' b' s t
112 ) => s' -> t'
113stuff' r = r & #test . #bar . position @15 .~ "hello"
115data Foo m s = Foo
116 { foo1 :: m s
117 , foo2 :: [s]
118 } deriving Generic
120modifyFoo2 :: Foo (Either String) Int -> Foo Maybe Int
121modifyFoo2 x = x & field @"foo1" .~ pure (1 :: Int)
123data Bar a b = Bar
124 { barField :: (a, b)
125 } deriving Generic
127modifiedBar = (Bar ("hello", "world")) & field @"barField" .~ ('c', 1 :: Int)