Ticket #4005: pL.hs

File pL.hs, 3.0 KB (added by hwloidl, 9 years ago)

cleaned-up sources of the program triggering the bug

Line 
1{-# OPTIONS_GHC -cpp -fglasgow-exts #-}
2{-
3 - Id Example Program
4 - Ensnaffled by SLPJ from MIT via
5 - RPaul <rpaul@juicy-juice.lcs.mit.edu> 93/08/26.
6 - Original author: Steve Heller
7 - Mods: uses lists instead of Arrays
8 - Sequential only!
9 - NB: this gives a slowdown of almost a factor of 2 with ghc-6.12.2rc2 as
10 -}
11
12module Main (main) where
13
14import System
15
16-- Generation of radicals
17
18data Radical = H | C Radical Radical Radical
19 deriving (Eq,Show)
20
21three_partitions :: Int -> [(Int,Int,Int)]
22three_partitions m =
23  [ (i,j,k) | i <- [0..(div m 3)], j <- [i..(div (m-i) 2)], k <- [m - (i+j)]]
24
25remainders [] = []
26remainders (r:rs) = (r:rs) : (remainders rs)
27
28radical_generator :: Int -> [[Radical]]
29radical_generator n =
30  radicals
31 where 
32  radicals =
33    ([H]) : [rads_of_size_n radicals j | j <- [1..n]]
34
35rads_of_size_n :: [[Radical]] -> Int -> [Radical]
36rads_of_size_n radicals n =
37  [ (C ri rj rk)
38  | (i,j,k)  <- (three_partitions (n-1)),
39    (ri:ris) <- (remainders (radicals!!i)),
40    (rj:rjs) <- (remainders (if (i==j) then (ri:ris) else radicals!!j)),
41    rk       <- (if (j==k) then (rj:rjs) else radicals!!k)]
42
43-- Generation of paraffins.
44
45data Paraffin = BCP Radical Radical | CCP Radical Radical Radical Radical
46 deriving (Eq,Show)
47
48bcp_generator ::  [[Radical]] -> Int -> [Paraffin]
49bcp_generator radicals n =
50  if (odd n) then []
51  else
52    [ (BCP r1 r2) | (r1:r1s) <- (remainders (radicals!!(div n 2))),
53                    r2       <- (r1:r1s) ]
54   
55four_partitions :: Int -> [(Int,Int,Int,Int)]
56four_partitions m =
57  [ (i,j,k,l)
58  | i <- [0..(div m 4)],
59    j <- [i..(div (m-i) 3)],
60    k <- [(max j (ceiling ((fromIntegral m)/(fromInteger 2)) - i - j))..(div (m-i-j) 2)],
61    l <- [(m - (i+j+k))]]
62
63ccp_generator :: [[Radical]] -> Int -> [Paraffin]
64ccp_generator radicals n =
65  [ (CCP ri rj rk rl)
66  | (i,j,k,l) <- (four_partitions (n-1)),
67    (ri:ris)  <- (remainders (radicals!!i)),
68    (rj:rjs)  <- (remainders (if (i==j) then (ri:ris) else radicals!!j)),
69    (rk:rks)  <- (remainders (if (j==k) then (rj:rjs) else radicals!!k)),
70    rl        <- (if (k==l) then (rk:rks) else radicals!!l)]
71
72bcp_until :: Int -> [Int]
73bcp_until n =
74  [length(bcp_generator radicals j) | j <- [1..n]]
75 where
76  radicals = radical_generator (div n 2)
77
78ccp_until :: Int -> [Int]
79ccp_until n =
80  [length(ccp_generator radicals j) | j <- [1..n]]
81 where
82  radicals = radical_generator (div n 2)
83
84paraffins_until :: Int -> [Int]
85paraffins_until n =
86 map (\ j -> length (bcp_generator radicals j) + length (ccp_generator radicals j)) [1..n]
87 where
88  radicals = radical_generator (div n 2)
89  res =   [length (bcp_generator radicals j) + length (ccp_generator radicals j)
90          | j <- [1..n]]
91
92main = do
93  [arg] <- getArgs
94  let num = (read arg) :: Int
95  -- just for testing:
96  --print [length (rads!i) | rads <- [(radical_generator num)], i <- [0..num]]
97  --print (bcp_until num)
98  --print (ccp_until num)
99
100  -- main version:
101  print (paraffins_until num)
102  -- predicate version:
103  -- print (length (paraffins_query num (\ _ -> True)))
104
105-- good input is: 28
106