File: Class.hs

package info (click to toggle)
haskell-flexible-defaults 0.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 72 kB
  • sloc: haskell: 297; makefile: 2
file content (69 lines) | stat: -rwxr-xr-x 1,927 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-type-defaults #-}
module Class
    ( DefaultsTest(..)
    , defaultsTest
    ) where

import Language.Haskell.TH.FlexibleDefaults
import Data.Char
import Data.Monoid
import qualified Data.Map as M
import qualified Data.Set as S

-- A very silly example.  For a real-world example, see the random-source package:
-- https://github.com/mokus0/random-fu/blob/master/random-source/src/Data/Random/Internal/TH.hs
class DefaultsTest a where
    foo :: a -> String
    foo = error "foo not implemented"

    bar :: a -> Int
    bar = error "bar not implemented"

    baz :: a -> a -> a
    baz = error "baz not implemented"

    qux :: a -> Integer
    qux = error "qux not implemented"

    quux :: a -> Bool
    quux = error "quux not implemented"

defaults :: Defaults (Sum Int) ()
defaults = scoreBy Sum $ do
    function "foo" $ do
        implementation $ do
            cost 1
            return [d| foo = filter isDigit . show |]

    function "bar" $ do
        implementation $ do
            dependsOn "qux"
            return [d| bar = fromInteger . qux |]

    function "baz" $ do
        implementation $ do
            score 1
            dependsOn "quux"
            return [d| baz x | quux x = const x
                             | otherwise = id
                    |]
        implementation $ do
            return [d| baz = const |]

    function "qux" $ do
        implementation $ do
            dependsOn "foo"
            return [d| qux = read . foo |]

    function "quux" $ do
        implementation $ do
            cost 1
            dependsOn "foo"
            return [d| quux x = toInteger (read (foo x) :: Int) == read (foo x) |]
        implementation $ do
            dependsOn "bar"
            dependsOn "qux"
            return [d| quux x = toInteger (bar x) == qux x |]

defaultsTest = withDefaults defaults