Why does floor not consider its argument an Integral?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does floor not consider its argument an Integral?

Jeffrey Brown
Tidal [1] defines these data types:
  
    type Time = Rational
    type Arc = (Time, Time)
  
I want to write a function "splitMultiCycArc" which divides an Arc into mostly-integer segments, so that, for instance,
  
    splitMultiCycArc (0,1)   = [(0,1)]
    splitMultiCycArc (0,2)   = [(0,1),(1,2)]
    splitMultiCycArc (0,3)   = [(0,1),(1,2),(2,3)]
    splitMultiCycArc (1%2,2) = [(1%2,1),(1,2)]
    splitMultiCycArc (1,5%2) = [(1,2),(2,5%2)]

I thought I had solved the problem with this code:

    splitMultiCycArc:: Arc -> [Arc]
    splitMultiCycArc (a,b) = let ceiling_ish = floor a + 1 in
      if      b <= a           then []    
      else if b <= ceiling_ish then [(a,b)]
      else (a,ceiling_ish) : splitMultiCycArc (ceiling_ish,b)
  
When I try to load that, I get this single error:

    > :reload
    [12 of 13] Compiling Sound.Tidal.JBB  ( Sound/Tidal/JBB.hs, interpreted )
    
    Sound/Tidal/JBB.hs:16:44:
        No instance for (Integral Time) arising from a use of ‘floor’
        In the first argument of ‘(+)’, namely ‘floor a’
        In the expression: floor a + 1
        In an equation for ‘ceiling_ish’: ceiling_ish = floor a + 1
    Failed, modules loaded: Sound.Tidal.Strategies, Sound.Tidal.Dirt, Sound.Tidal.Pattern, Sound.Tidal.Stream, Sound.Tidal.Parse, Sound.Tidal.Tempo, Sound.Tidal.Time, Sound.Tidal.Utils, Sound.Tidal.SuperCollider, Sound.Tidal.Params, Sound.Tidal.Transition.
    > 
  
And yet under other conditions, "floor" is perfectly happy operating on a Time value:

    > let a = (1%2,1) :: Arc
    > floor (fst a) + 1
    1
    > 



--
Jeffrey Benjamin Brown

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Why does floor not consider its argument an Integral?

Jeffrey Brown
I solved it. I was misreading the error report as being about the input to floor, when the problem was the output of it. This code works:

splitArcAtIntegers:: Arc -> [Arc]
splitArcAtIntegers (a,b) = let
    ceiling_ish = fromInteger $ (floor a) + 1 
  in if      b <= a           then []    
     else if b <= ceiling_ish then [(a,b)]
     else (a,ceiling_ish) : splitArcAtIntegers (ceiling_ish,b)


On Sat, Nov 7, 2015 at 10:14 PM, Jeffrey Brown <[hidden email]> wrote:
Tidal [1] defines these data types:
  
    type Time = Rational
    type Arc = (Time, Time)
  
I want to write a function "splitMultiCycArc" which divides an Arc into mostly-integer segments, so that, for instance,
  
    splitMultiCycArc (0,1)   = [(0,1)]
    splitMultiCycArc (0,2)   = [(0,1),(1,2)]
    splitMultiCycArc (0,3)   = [(0,1),(1,2),(2,3)]
    splitMultiCycArc (1%2,2) = [(1%2,1),(1,2)]
    splitMultiCycArc (1,5%2) = [(1,2),(2,5%2)]

I thought I had solved the problem with this code:

    splitMultiCycArc:: Arc -> [Arc]
    splitMultiCycArc (a,b) = let ceiling_ish = floor a + 1 in
      if      b <= a           then []    
      else if b <= ceiling_ish then [(a,b)]
      else (a,ceiling_ish) : splitMultiCycArc (ceiling_ish,b)
  
When I try to load that, I get this single error:

    > :reload
    [12 of 13] Compiling Sound.Tidal.JBB  ( Sound/Tidal/JBB.hs, interpreted )
    
    Sound/Tidal/JBB.hs:16:44:
        No instance for (Integral Time) arising from a use of ‘floor’
        In the first argument of ‘(+)’, namely ‘floor a’
        In the expression: floor a + 1
        In an equation for ‘ceiling_ish’: ceiling_ish = floor a + 1
    Failed, modules loaded: Sound.Tidal.Strategies, Sound.Tidal.Dirt, Sound.Tidal.Pattern, Sound.Tidal.Stream, Sound.Tidal.Parse, Sound.Tidal.Tempo, Sound.Tidal.Time, Sound.Tidal.Utils, Sound.Tidal.SuperCollider, Sound.Tidal.Params, Sound.Tidal.Transition.
    > 
  
And yet under other conditions, "floor" is perfectly happy operating on a Time value:

    > let a = (1%2,1) :: Arc
    > floor (fst a) + 1
    1
    > 



--
Jeffrey Benjamin Brown



--
Jeffrey Benjamin Brown

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners