1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE DuplicateRecordFields #-}
5{-# LANGUAGE FlexibleContexts #-}
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 #-}
19import Data.Function ((&))
20import Data.Generics.Internal.Lens
21import Data.Generics.Product
22import Data.Generics.Sum
24import Data.Generics.Labels
30 } deriving (Show, Generic)
37 } deriving (Show, Generic)
40 = Animal' { name :: String, eats :: String, age :: Int }
41 | Human' { name :: String, age :: Int, address :: String, eats :: String }
42 deriving (Show, Generic)
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
59-- Test {fieldInt = 10, fieldA = 10, fieldB = "world"}
60changedA = Test 10 "hello" "world" & field @"fieldA" .~ (10 :: Int)
62-- changedB :: Test String Int
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")
74 deriving (Generic, Show)
82 deriving (Generic, Show)
86dog = Dog (MkDog "Shep" 3 30)
88-- TODO: the error message for this case is ugly
96-- deriving (Generic, Show)
98--dog' :: Animal2 String
99dog' = dog & _Ctor @"Dog" . field @"fieldA" .~ "now it's a String"
102 ( HasPosition 15 s t a String
103 , HasField "test" s' t' a' b'
104 , HasField "bar" a' b' s t
106stuff r = r & field @"test" . field @"bar" . position @15 .~ "hello"
109 ( HasPosition 15 s t a String
110 , HasField "test" s' t' a' b'
111 , HasField "bar" a' b' s t
113stuff' r = r & #test . #bar . position @15 .~ "hello"
120modifyFoo2 :: Foo (Either String) Int -> Foo Maybe Int
121modifyFoo2 x = x & field @"foo1" .~ pure (1 :: Int)
127modifiedBar = (Bar ("hello", "world")) & field @"barField" .~ ('c', 1 :: Int)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list