This repository was archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Haskell Generics tutorial #40
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright © 2017 Stack Builders | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to | ||
| deal in the Software without restriction, including without limitation the | ||
| rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| sell copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| IN THE SOFTWARE. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Haskel Generics | ||
|
|
||
| Code for the above-mentioned tutorial. | ||
|
|
||
| ## License | ||
|
|
||
| Copyright © 2017 Stack Builders | ||
|
|
||
| Distributed under MIT. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import Distribution.Simple | ||
| main = defaultMain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| name: generics | ||
| version: 0.1.0 | ||
| cabal-version: >= 1.10 | ||
| license: MIT | ||
| license-file: LICENSE.md | ||
| author: Mark Karpov <mkarpov@stackbuilders.com> | ||
| maintainer: Mark Karpov <mkarpov@stackbuilders.com> | ||
| homepage: https://github.com/stackbuilders/tutorials | ||
| bug-reports: https://github.com/stackbuilders/tutorials/issues | ||
| category: Generics | ||
| synopsis: Code for the Stack Builders Generics tutorial | ||
| build-type: Simple | ||
| description: Code for the Stack Builders Generics tutorial. | ||
| extra-doc-files: CHANGELOG.md | ||
| , README.md | ||
|
|
||
| source-repository head | ||
| type: git | ||
| location: https://github.com/stackbuilders/tutorials.git | ||
|
|
||
| library | ||
| hs-source-dirs: src | ||
| build-depends: base >= 4.7 && < 5.0 | ||
| exposed-modules: GenericsTutorial | ||
| ghc-options: -O2 -Wall | ||
| default-language: Haskell2010 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| {-# LANGUAGE DefaultSignatures #-} | ||
| {-# LANGUAGE DeriveGeneric #-} | ||
| {-# LANGUAGE FlexibleContexts #-} | ||
| {-# LANGUAGE TypeFamilies #-} | ||
| {-# LANGUAGE TypeOperators #-} | ||
|
|
||
| module GenericsTutorial | ||
| ( CountFields (..) ) | ||
| where | ||
|
|
||
| import GHC.Generics | ||
| import Numeric.Natural | ||
| import Prelude | ||
|
|
||
| class CountFields a where | ||
| -- | Return number of constructor fields for a value. | ||
| countFields :: a -> Natural | ||
| default countFields :: (Generic a, CountFields1 (Rep a)) => a -> Natural | ||
| countFields = defaultCountFields | ||
|
|
||
| defaultCountFields :: (Generic a, CountFields1 (Rep a)) => a -> Natural | ||
| defaultCountFields = countFields1 . from | ||
|
|
||
| class CountFields1 f where | ||
| countFields1 :: f p -> Natural | ||
|
|
||
| instance CountFields1 V1 where | ||
| countFields1 _ = 0 | ||
|
|
||
| instance CountFields1 U1 where | ||
| countFields1 _ = 0 | ||
|
|
||
| instance CountFields1 (K1 i c) where | ||
| countFields1 _ = 1 | ||
|
|
||
| instance CountFields1 f => CountFields1 (M1 i c f) where | ||
| countFields1 (M1 x) = countFields1 x | ||
|
|
||
| instance (CountFields1 a, CountFields1 b) => CountFields1 (a :+: b) where | ||
| countFields1 (L1 x) = countFields1 x | ||
| countFields1 (R1 x) = countFields1 x | ||
|
|
||
| instance (CountFields1 a, CountFields1 b) => CountFields1 (a :*: b) where | ||
| countFields1 (a :*: b) = countFields1 a + countFields1 b | ||
|
|
||
| ---------------------------------------------------------------------------- | ||
| -- Tests | ||
|
|
||
| instance CountFields (Maybe a) | ||
|
|
||
| data Foo = Foo Int Int Int | Bar Bool | ||
| deriving (Show, Generic) | ||
|
|
||
| instance CountFields Foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| resolver: lts-7.16 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though not that important, lts-7.17 is out |
||
| packages: | ||
| - '.' | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mark,
Thanks for writing this post! It is really well written and structured. I learned a lot editing it. I have tried to understand Generics before and found myself with a headache trying to take in too much at once. Saving the MetaData info for the end was a wise choice.
Most of my comments are small grammar fixes about missing articles, subjects an objects, e.g. a, the, we and us. My biggest question is about this tricky
pparameter. It seems like that is a hard thing to explain, but I hope my questions in the comments below give you some inspiration for how to explain it just a little more clearly.Cheers!
Nathan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!