-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx04.elm
More file actions
93 lines (64 loc) · 2.01 KB
/
Ex04.elm
File metadata and controls
93 lines (64 loc) · 2.01 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---- Phantom Types ----
module Ex04 exposing (..)
import Email exposing (Email, EmailError, create)
import Email
-- Define a type; this is a simple wrapper over number. BUT, it's a bit weird:
type Temperature a = Temperature Float
-- And some types. Only used for the type;
-- the "a" param on the left side of
-- Temperature definition is only for differentiating
-- by type.
-- All the same thing at run-time (wrapper over Float),
-- after type-checking.
type Celsius = Celsius
type Fahrenheit = Fahrenheit
celsiusToFahrenheit :
Temperature Celsius ->
Temperature Fahrenheit
celsiusToFahrenheit (Temperature num) =
Temperature ((num - 32) / 1.8)
createFahr : Float -> Temperature Fahrenheit
createFahr num =
Temperature num
createCels : Float -> Temperature Celsius
createCels num =
Temperature num
boilingFahr : Temperature Fahrenheit
boilingFahr =
celsiusToFahrenheit (createCels 212)
-- Can't do:
-- > celsiusToFahrenheit boilingFahr
-- (Kaboom! Type error. You need to explicitly
-- convert Fahrenheit to Celsius before being
-- able to use it with that function.)
-- Same idea:
type FormData value tag = FormData value
type Validated = Validated
type Unvalidated = Unvalidated
-- Export only the type and these functions; can't
-- create a FormData value that is Validated without
-- using them.
create : a -> FormData a Unvalidated
create a = FormData a
validateEmail :
FormData String Unvalidated ->
Result EmailError (FormData Email Validated)
validateEmail (FormData rawEmail) =
Result.map
(\email -> FormData email)
(Email.create rawEmail)
---- A state machine, with types ----
type Plane state = Plane { flightCode : String }
type OnGround = OnGround
type Taxiing = Taxiing
type InAir = InAir
type Landing = Landing
taxi : Plane OnGround -> Plane Taxiing
taxi (Plane x) = Plane x
takeOff : Plane Taxiing -> Plane InAir
takeOff (Plane x) = Plane x
approach : Plane InAir -> Plane Landing
approach (Plane x) = Plane x
-- a little morbid
crash : Plane a -> Plane OnGround
crash (Plane x) = Plane x