-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtypes.p
More file actions
77 lines (64 loc) · 1.84 KB
/
types.p
File metadata and controls
77 lines (64 loc) · 1.84 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
// vim: ft=plasma
// This is free and unencumbered software released into the public domain.
// See ../LICENSE.unlicense
module Types
/*
* This example doesn't yet compile. The uncommented code requires a type
* alias for Number and the module system and a Set module.
*/
// We can define our own types.
// A simple enum
type Suit = Hearts | Diamonds | Spades | Clubs
type Number = Ace
| One
| Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
| Jack
| Queen
| King
// A structure: a single constructor with fields.
type PlayingCard = Card ( suit : Suit, number : Number )
// A combination of the above, a PlayingCard is either an ordinary card or a
// joker. An orderinary card has fields.
type PlayingCardOrJoker = OrdinaryCard ( suit : Suit, number : Number )
| Joker
// Types are polymorphic, they may take type parameters.
type Tree('k, 'v) = EmptyTree
| Node (
key : 'k,
value : 'v,
left : Tree('k, 'v),
right : Tree('k, 'v)
)
// Test that module qualifiers work on type expressions.
import Set
type MyType = MyConstr (
field : Set.Set(Int)
)
//
// Type Aliases
//
//
//# A type alias, ID is now another word for Int.
//type_alias ID = Int
//
//# It's often more useful to alias something more complex.
//type_alias Name = String
//type_alias NameMap = Map(ID, Name)
//
//# Type aliases can take parameters:
//type_alias IDMap(x) = Map(ID, x)
//
// Empty main function.
entrypoint
func main() uses IO -> Int {
print!("Types example doesn't actually do anything with the types\n")
return 0
}