shapes
examples/shapes.omg
examples/shapes.omg38 lines
1{- Records, unions and `merge`.
2
3 omegyu normalize examples/shapes.omg
4
5 A union type lists alternatives; `merge` takes a value of a union type
6 apart with one handler per alternative, and the type checker makes sure
7 that none is forgotten.
8-}
9
10let Shape =
11 < Circle : { radius : Natural } | Rectangle : { width : Natural, height : Natural } | Point >
12
13-- Areas, with pi rounded to 3.
14let area
15 : Shape -> Natural
16 = \shape ->
17 merge
18 { Circle = \(c : { radius : Natural }) -> 3 * c.radius * c.radius
19 , Rectangle = \(r : { width : Natural, height : Natural }) -> r.width * r.height
20 , Point = 0
21 }
22 shape
23
24let describe
25 : Shape -> Text
26 = \shape ->
27 merge
28 { Circle = \(c : { radius : Natural }) -> "a circle of radius ${Natural/show c.radius}"
29 , Rectangle =
30 \(r : { width : Natural, height : Natural }) ->
31 "a ${Natural/show r.width} by ${Natural/show r.height} rectangle"
32 , Point = "a point"
33 }
34 shape
35
36let shapes = [ Shape.Circle { radius = 2 }, Shape.Rectangle { width = 3, height = 4 }, Shape.Point ]
37
38in std:List.map (\(s : Shape) -> { shape = describe s, area = area s }) shapes