-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRect.hs
More file actions
62 lines (44 loc) · 1.26 KB
/
Rect.hs
File metadata and controls
62 lines (44 loc) · 1.26 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# LANGUAGE
DeriveFunctor,
FlexibleContexts,
MultiParamTypeClasses,
TypeOperators
#-}
module Rect where
import Prelude
import AlaCarte
import Prim
import Cond
import Shape
import ShapeArea
--
-- * Syntax
--
-- | Extend Shapes
data Rect t = Rec (Point t) t t
deriving (Eq,Functor,Show)
rect :: (Rect :<: t) => Point (Term t) -> Term t -> Term t -> Term t
rect p h w = inject (Rec p h w)
--
-- * Extend Pretty printing
--
instance Pretty Rect where
prettyAlg (Rec p h w) = concat ["rect: center ", prettyAlg p, " height: ", h, " width: ", w]
--
-- -- ** Extend Evaluation (of Shape)
--
evalRect :: (ShapeDom :<: t,PVal :<: t) => Term t -> Term t -> Term t -> Term t -> Term t
evalRect x y h w =
case (project x, project y,project h,project w ) of
(Just (F x'), Just(F y'), Just(F h'), Just(F w') ) -> shapeDom ( P (float x') (float y')) (float h') (float w')
_ -> error "Type error: non-float values"
instance Eval Rect where
evalAlg (Rec (P x y) h w) = evalRect x y h w
--
-- ** Extend Area
--
instance Area Rect where
areaAlg (Rec (P x y) h w) =
case (h, w, x,y) of
(F h',F w', F _ , F _ ) -> F $ h' * w'
_ -> error "Type error: non-float values"