Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tutorials/haskell/generics/code/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copy link
Copy Markdown

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 p parameter. 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


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.
9 changes: 9 additions & 0 deletions tutorials/haskell/generics/code/README.md
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.
2 changes: 2 additions & 0 deletions tutorials/haskell/generics/code/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
26 changes: 26 additions & 0 deletions tutorials/haskell/generics/code/generics.cabal
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
54 changes: 54 additions & 0 deletions tutorials/haskell/generics/code/src/GenericsTutorial.hs
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
3 changes: 3 additions & 0 deletions tutorials/haskell/generics/code/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resolver: lts-7.16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though not that important, lts-7.17 is out

packages:
- '.'
Loading