OpenSCAD
Check-in [4d1c535b67]
Not logged in
Public Repositories
mwm's Repositories

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Remove unneeded Semigroup instance (Monoid does it all)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | 0.3
Files: files | file ages | folders
SHA1: 4d1c535b67414d43f5f1c0c44db53c515c7d3da5
User & Date: mwm 2015-07-21 07:56:47.588
Context
2015-07-21
07:56
Remove unneeded Semigroup instance (Monoid does it all) Leaf check-in: 4d1c535b67 user: mwm tags: 0.3
07:41
Remove extra containers requirement, convert to stack. check-in: 7fa0002390 user: mwm tags: 0.3
Changes
Unified Diff Ignore Whitespace Patch
Changes to Graphics/OpenSCAD.hs.
1
2
3
4
5
6
7
8
{-# LANGUAGE FlexibleInstances #-}

{- |
Module      : Graphics.OpenSCAD
Description : Type-checked wrappers for the OpenSCAD primitives.
Copyright   : © Mike Meyer, 2014
License     : BSD4
Maintainer  : mwm@mired.org
|







1
2
3
4
5
6
7
8
 {-# LANGUAGE FlexibleInstances #-}

{- |
Module      : Graphics.OpenSCAD
Description : Type-checked wrappers for the OpenSCAD primitives.
Copyright   : © Mike Meyer, 2014
License     : BSD4
Maintainer  : mwm@mired.org
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
where

import Data.Colour (Colour, AlphaColour, alphaChannel, darken, over, black)
import Data.Colour.Names as Colours
import Data.Colour.SRGB (channelRed, channelBlue, channelGreen, toSRGB)
import Data.List (elemIndices, nub, intercalate)
import qualified Data.List.NonEmpty as NE
import Data.Semigroup (Semigroup((<>), sconcat), Monoid(mconcat, mempty, mappend))
import qualified Data.Set as Set
import System.FilePath (FilePath)

-- A vector in 2 or 3-space. They are used in transformations of
-- 'Model's of their type.
class Eq a => Vector a where
  rVector :: a -> String







|







132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
where

import Data.Colour (Colour, AlphaColour, alphaChannel, darken, over, black)
import Data.Colour.Names as Colours
import Data.Colour.SRGB (channelRed, channelBlue, channelGreen, toSRGB)
import Data.List (elemIndices, nub, intercalate)
import qualified Data.List.NonEmpty as NE
import Data.Monoid ((<>), Monoid, mconcat, mempty, mappend)
import qualified Data.Set as Set
import System.FilePath (FilePath)

-- A vector in 2 or 3-space. They are used in transformations of
-- 'Model's of their type.
class Eq a => Vector a where
  rVector :: a -> String
613
614
615
616
617
618
619
620
621
622
623
624

625
626
627
628
629
630
631
632
633
634
635
636
637
def :: Facet
def = Def

-- And one last convenience function.
-- | Use 'diam' to turn a diameter into a radius for circles, spheres, etc.
diam :: Double -> Double
diam = (/ 2)
-- Now, let Haskell work it's magic
instance Vector v => Semigroup (Model v) where
  a <> b = union [a, b]
  sconcat = union . NE.toList


instance Vector v => Monoid (Model v) where
  mempty = Solid $ Box 0 0 0
  mappend (Solid (Box 0 0 0)) b = b
  mappend a (Solid (Box 0 0 0)) = a
  mappend a b = union [a, b]
  mconcat [a] = a
  mconcat as = union as


-- | You can use '(#)' to write transformations in a more readable postfix form,
--   cube 3 # color red # translate (-3, -3, -3)
infixl 8 #
(#) = flip ($)







<
<
<
<

>













613
614
615
616
617
618
619




620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
def :: Facet
def = Def

-- And one last convenience function.
-- | Use 'diam' to turn a diameter into a radius for circles, spheres, etc.
diam :: Double -> Double
diam = (/ 2)





-- Now, let Haskell work it's magic
instance Vector v => Monoid (Model v) where
  mempty = Solid $ Box 0 0 0
  mappend (Solid (Box 0 0 0)) b = b
  mappend a (Solid (Box 0 0 0)) = a
  mappend a b = union [a, b]
  mconcat [a] = a
  mconcat as = union as


-- | You can use '(#)' to write transformations in a more readable postfix form,
--   cube 3 # color red # translate (-3, -3, -3)
infixl 8 #
(#) = flip ($)
Changes to Graphics/OpenSCAD/Unicode.hs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{-# LANGUAGE UnicodeSyntax #-}

{-
Module      : Graphics.OpenSCAD.Unicode
Description : Unicode operators so you can write 'Model' expressions.
Copyright   : &#xa9; Mike Meyer, 2014
License     : BSD4
Maintainer  : mwm@mired.org
Stability   : experimental
-}

module Graphics.OpenSCAD.Unicode where

import Data.Semigroup ((<>))
import Graphics.OpenSCAD

infixl 6 ∪
infixr 6 ∩
infixl 9 ∖
infixl 9 ⊖
infixl 9 ⊕













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{-# LANGUAGE UnicodeSyntax #-}

{-
Module      : Graphics.OpenSCAD.Unicode
Description : Unicode operators so you can write 'Model' expressions.
Copyright   : &#xa9; Mike Meyer, 2014
License     : BSD4
Maintainer  : mwm@mired.org
Stability   : experimental
-}

module Graphics.OpenSCAD.Unicode where

import Data.Monoid ((<>))
import Graphics.OpenSCAD

infixl 6 ∪
infixr 6 ∩
infixl 9 ∖
infixl 9 ⊖
infixl 9 ⊕
Changes to UnitTest.hs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env runghc

module Main where

import Control.DeepSeq
import Control.Exception
import Test.Tasty
import Test.Tasty.HUnit
import Test.HUnit.Tools
import Graphics.OpenSCAD
import Data.Colour (withOpacity)
import Data.List.NonEmpty (fromList)
import Data.Semigroup (Semigroup((<>), sconcat), Monoid(mconcat, mempty, mappend))



assertError err code =
  assertRaises "Check error"  (ErrorCall err) . evaluate $ deepseq (show code) ()

sw = concat . words












|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env runghc

module Main where

import Control.DeepSeq
import Control.Exception
import Test.Tasty
import Test.Tasty.HUnit
import Test.HUnit.Tools
import Graphics.OpenSCAD
import Data.Colour (withOpacity)
import Data.List.NonEmpty (fromList)
import Data.Monoid ((<>), Monoid,mconcat, mempty, mappend)



assertError err code =
  assertRaises "Check error"  (ErrorCall err) . evaluate $ deepseq (show code) ()

sw = concat . words
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
       (square 1 <> circle 1.1 (fs 0.1)),
    st "Monoid 2 3d" "union(){cube([1.0,1.0,1.0]);sphere(1.1,$fs=0.1);}"
       (mconcat [cube 1, sphere 1.1 $ fs 0.1]),
    st "Monoid 2 2d" "union(){square([1.0,1.0]);circle(1.1,$fs=0.1);}"
       (mconcat [square 1, circle 1.1 $ fs 0.1]),
    st "Monoid 3 3d" "sphere(1.1,$fs=0.1);" (mconcat [sphere 1.1 $ fs 0.1]),
    st "Monoid 3 2d" "square([1.0,1.0]);" (mconcat [square 1]),
    st "Semigroup 1 3d" "cube([0.0,0.0,0.0]);" (solid mempty),
    -- should we export a "shape" function?
    st "Semigroup 1 2d" "cube([0.0,0.0,0.0]);" (mempty :: Model2d),
    st "Semigroup 2 3d" "union(){cube([1.0,1.0,1.0]);sphere(1.1,$fs=0.1);}"
       (mappend (cube 1) $ sphere 1.1 (fs 0.1)),
    st "Semigroup 2 2d" "union(){square([1.0,1.0]);circle(1.1,$fs=0.1);}"
       (mappend (square 1) $ circle 1.1 (fs 0.1)),
    st "Semigroup 3 3d" "union(){cube([1.0,1.0,1.0]);sphere(1.1,$fs=0.1);}"
       (sconcat $ fromList [cube 1, sphere 1.1 $ fs 0.1]),
    st "Semigroup 3 2d" "union(){square([1.0,1.0]);circle(1.1,$fs=0.1);}"
       (sconcat $ fromList [square 1, circle 1.1 $ fs 0.1])
    ]
  ]

main = defaultMain tests







|
<
|
|

|
|
<
<
<
<




290
291
292
293
294
295
296
297

298
299
300
301
302




303
304
305
306
       (square 1 <> circle 1.1 (fs 0.1)),
    st "Monoid 2 3d" "union(){cube([1.0,1.0,1.0]);sphere(1.1,$fs=0.1);}"
       (mconcat [cube 1, sphere 1.1 $ fs 0.1]),
    st "Monoid 2 2d" "union(){square([1.0,1.0]);circle(1.1,$fs=0.1);}"
       (mconcat [square 1, circle 1.1 $ fs 0.1]),
    st "Monoid 3 3d" "sphere(1.1,$fs=0.1);" (mconcat [sphere 1.1 $ fs 0.1]),
    st "Monoid 3 2d" "square([1.0,1.0]);" (mconcat [square 1]),
    st "Monoid 4 3d" "cube([0.0,0.0,0.0]);" (solid mempty),

    st "Monoid 4 2d" "cube([0.0,0.0,0.0]);" (mempty :: Model2d),
    st "Monoid 5 3d" "union(){cube([1.0,1.0,1.0]);sphere(1.1,$fs=0.1);}"
       (mappend (cube 1) $ sphere 1.1 (fs 0.1)),
    st "Monoid 5 2d" "union(){square([1.0,1.0]);circle(1.1,$fs=0.1);}"
       (mappend (square 1) $ circle 1.1 (fs 0.1))




    ]
  ]

main = defaultMain tests