Ticket #57: vectorNegateShowBugwithUserDefinedDatatypes.hs

File vectorNegateShowBugwithUserDefinedDatatypes.hs, 2.4 kB (added by kaffeepause73, 2 years ago)
Line 
1-- Author: phil - kaffeepause73@yahoo.de
2
3import qualified Data.Vector.Unboxed as V
4
5
6-- A. Originally intended code using signal datatype -- create problem with show on negated signal
7-- Type synonyms to make things easier to read
8type Name = String -- Name
9type Time = (V.Vector Double) -- time Vector
10type Vect = (V.Vector Double) -- data Vector
11type Unit = String -- Unit
12
13-- define datatype nesting vector with other data in a 4-touple
14newtype Signal = Signal (Name, Time, Vect, Unit)
15
16-- functions to access data in 4-touple
17getName :: Signal -> String
18getName (Signal (n,t,v,u)) = n
19
20getTime :: Signal -> Vect
21getTime (Signal (n,t,v,u)) = t
22
23getVect :: Signal -> Vect
24getVect (Signal (n,t,v,u)) = v
25
26getUnit :: Signal -> Unit
27getUnit (Signal (n,t,v,u)) = u
28
29-- function to negate signal using smap         
30snegate s1 = smap negate s1 "negate"
31
32-- show instance for new datatype signal
33instance Show Signal where 
34           show s  = "TimeSignal time: " ++ show (getName s) ++ " val:" ++ show (getVect s) ++ show (getUnit s)   
35
36
37-- smap only acesses signal data an calls V.map
38smap :: (Double -> Double) -> Signal -> String -> Signal
39smap f s1 fstr =  Signal (n,t,v,u)
40               where 
41                 n = getName s1
42                 t = getTime s1
43                 vdata = getVect s1
44                 u = getUnit s1
45                 v = V.map f v -- when time data is same
46
47-- B. Alternative negate function directly on V.vector data types to check
48-- # Direct negate funct
49                 
50vnegate :: V.Vector Double -> V.Vector Double
51vnegate v = V.map negate v
52
53
54-- ### main to test ways A & B: create different vectors and signals ans checks whether show works fine
55
56main = do         
57     -- create a vector
58     let v=V.fromList([0..10])
59
60     -- create a negated vector
61     let vn = vnegate v   
62
63     -- show bothvectors - works fine :-)
64     putStrLn "VectorTests"
65     putStrLn (show v)
66     putStrLn (show vn)     
67
68     -- create signal with vector
69     let s = Signal("name",v,v,"N")
70
71     -- create signal with negated vector
72     let sn = Signal("name",vn,vn,"N")
73
74     -- negate original signal s
75     let sn2 = snegate s
76   
77     -- showing these works fine
78     putStrLn "SignalTests"
79     putStrLn (show s)
80     putStrLn (show sn)
81
82     -- this crashes the program :-(
83     putStrLn "uuups"
84     putStrLn (show sn2) -- shows <<LOOP>>
85
86     -- this line doesn't appear anymore
87     putStrLn (show "test")
88
89