on 445d5fe help sign in
generic-lens/examples/Examples.hs
112 lines 3.0 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 PartialTypeSignatures #-}
9{-# LANGUAGE Rank2Types #-}
10{-# LANGUAGE ScopedTypeVariables #-}
11{-# LANGUAGE TypeApplications #-}
12{-# LANGUAGE UndecidableInstances #-}
13{-# OPTIONS_GHC -Wno-missing-signatures #-}
14{-# OPTIONS_GHC -fno-warn-unused-imports #-}
16module Examples where
18import Data.Function ((&))
19import Data.Generics.Internal.Lens
20import Data.Generics.Product
21import Data.Generics.Sum
22import GHC.Generics
23import Data.Generics.Labels
25data Animal = Animal
26 { name :: String
27 , age :: Int
28 , eats :: String
29 } deriving (Show, Generic)
31data Human = Human
32 { name :: String
33 , age :: Int
34 , address :: String
35 , eats :: String
36 } deriving (Show, Generic)
38data Living
39 = Animal' { name :: String, eats :: String, age :: Int }
40 | Human' { name :: String, age :: Int, address :: String, eats :: String }
41 deriving (Show, Generic)
43toby :: Human
44toby = Human { name = "Toby", age = 10, address = "London", eats = "Bread" }
46growUp :: Animal -> Animal
47growUp (Animal n a _) = Animal n (a + 10) "raw meat"
49data MyRecord = MyRecord { field1 :: Int, field2 :: String } deriving Generic
51--g :: Subtype s MyRecord => s -> String
52--g s = s ^. super @MyRecord . label @"field2"
54data Test a b = Test { fieldInt :: Int, fieldA :: a, fieldB :: b } deriving (Generic, Show)
56-- changedA :: Test Int String
57-- >>> changedA
58-- Test {fieldInt = 10, fieldA = 10, fieldB = "world"}
59changedA = Test 10 "hello" "world" & field @"fieldA" .~ (10 :: Int)
61-- changedB :: Test String Int
62-- >>> changedB
63-- Test {fieldInt = 10, fieldA = "hello", fieldB = 10}
64changedB = (Test 10 "hello" "world") & field @"fieldB" .~ (10 :: Int)
66--changedInt = set (field @"fieldInt") ("hello") (Test 10 "hello" "world")
67-- type error
69data Animal2 a
70 = Dog (Dog a)
71 | Cat Name Age
72 | Duck Age
73 deriving (Generic, Show)
75data Dog a
76 = MkDog
77 { name :: Name
78 , age :: Age
79 , fieldA :: a
80 }
81 deriving (Generic, Show)
82type Name = String
83type Age = Int
84dog :: Animal2 Int
85dog = Dog (MkDog "Shep" 3 30)
87-- TODO: the error message for this case is ugly
88-- data Dog a
89-- = MkDog
90-- { name :: Name
91-- , age :: Age
92-- , fieldA :: a
93-- , fieldA' :: a
94-- }
95-- deriving (Generic, Show)
97--dog' :: Animal2 String
98dog' = dog & _Ctor @"Dog" . field @"fieldA" .~ "now it's a String"
100stuff ::
101 ( HasPosition 15 s t a String
102 , HasField "test" s' t' a' b'
103 , HasField "bar" a' b' s t
104 ) => s' -> t'
105stuff r = r & field @"test" . field @"bar" . position @15 .~ "hello"
107stuff' ::
108 ( HasPosition 15 s t a String
109 , HasField "test" s' t' a' b'
110 , HasField "bar" a' b' s t
111 ) => s' -> t'
112stuff' r = r & #test . #bar . position @15 .~ "hello"