File: generate-nfdata-tuple.hs

package info (click to toggle)
ghc 9.6.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 158,216 kB
  • sloc: haskell: 648,228; ansic: 81,656; cpp: 11,808; javascript: 8,444; sh: 5,831; fortran: 3,527; python: 3,277; asm: 2,523; makefile: 2,298; yacc: 1,570; lisp: 532; xml: 196; perl: 145; csh: 2
file content (33 lines) | stat: -rw-r--r-- 1,164 bytes parent folder | download | duplicates (5)
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
import Data.List

genNFData :: Int -> [String]
genNFData n = 
    [ ""
    , "instance (" ++ ctx0 ++ ") =>"
    , "         NFData (" ++ intercalate ", " as0  ++ ") where rnf = rnf2"
    , "instance (" ++ ctx1 ++ ") =>"
    , "         NFData1 ((" ++ replicate (n - 1) ',' ++ ") " ++ intercalate " " as1  ++ ") where liftRnf = liftRnf2 rnf"
    , "instance (" ++ ctx2 ++ ") =>"
    , "         NFData2 ((" ++ replicate (n - 1) ',' ++ ") " ++ intercalate " " as2  ++ ") where"
    , "  liftRnf2 r r' (" ++ intercalate "," xs ++ ") = " ++ implemantation
    ]
  where
    as0 = take (n - 0) $ ('a' :) . show <$> [1..]
    as1 = take (n - 1) $ ('a' :) . show <$> [1..]
    as2 = take (n - 2) $ ('a' :) . show <$> [1..]

    xs = take n $ ('x' : ) . show <$> [1..]

    ctx0 = intercalate ", " $ ("NFData " ++) <$> as0
    ctx1 = intercalate ", " $ ("NFData " ++) <$> as1
    ctx2 = intercalate ", " $ ("NFData " ++) <$> as2

    implemantation
        = intercalate " `seq` "
        $ zipWith (\l r -> l ++ " " ++ r)
            (replicate (n-2) "rnf" ++ ["r", "r'"])
            xs
    

main :: IO ()
main = putStr $ unlines $ concatMap (\x -> genNFData x) [3..9]