-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtypeclasses.ail
More file actions
126 lines (96 loc) · 2.12 KB
/
Copy pathtypeclasses.ail
File metadata and controls
126 lines (96 loc) · 2.12 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- typeclasses.ail - Type class usage examples
-- Demonstrates Num, Eq, Ord, and Show type classes
-- =======================
-- NUM TYPE CLASS
-- =======================
-- Numeric operations with defaulting to Int
1 + 2 * 3
-- Output: 7
-- Floating point operations
3.14 * 2.0
-- Output: 6.28
-- Negative numbers
-42 + 100
-- Output: 58
-- Division
100 / 4
-- Output: 25
-- =======================
-- EQ TYPE CLASS
-- =======================
-- Integer equality
42 == 42
-- Output: true
-- Inequality
42 != 43
-- Output: true
-- String equality
"hello" == "hello"
-- Output: true
-- Boolean equality
true == true
-- Output: true
-- =======================
-- ORD TYPE CLASS
-- =======================
-- Less than
5 < 10
-- Output: true
-- Less than or equal
10 <= 10
-- Output: true
-- Greater than
20 > 15
-- Output: true
-- Greater than or equal
30 >= 30
-- Output: true
-- String comparison
"abc" < "xyz"
-- Output: true
-- =======================
-- SHOW TYPE CLASS
-- =======================
-- Showing integers (using implicit dictionary passing)
show(42)
-- Output: "42"
-- Showing floats
show(3.14159)
-- Output: "3.14159"
-- Showing booleans
show(true)
-- Output: "true"
-- Showing strings (identity)
show("Hello, AILANG!")
-- Output: "Hello, AILANG!"
-- =======================
-- STRING CONCATENATION
-- =======================
-- Concat operator
"Hello, World!"
-- Output: "Hello, World!"
-- Building messages
"The answer is ${show(42)}"
-- Output: "The answer is 42"
-- =======================
-- POLYMORPHIC OPERATIONS
-- =======================
-- Identity function is polymorphic
let id = \x. x in
"${show(id(42))}, ${show(id("hello"))}"
-- Output: "42, hello"
-- Generic comparison
let equals = \x. \y. x == y in
"${show(equals(1)(1))}, ${show(equals("a")("b"))}"
-- Output: "true, false"
-- =======================
-- COMPLEX EXPRESSIONS
-- =======================
-- Combining multiple type classes
let result = if 10 > 5 then 42 else 0 in
"${show(result)} is the answer"
-- Output: "42 is the answer"
-- Numeric polymorphism
let double = \x. x * 2 in
"${show(double(21))}, ${show(double(3.14))}"
-- Output: "42, 6.28"