This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlateExamples.hs
144 lines (122 loc) · 3.17 KB
/
PlateExamples.hs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
-- | The first Plate library.
module PlateExamples where
import Plate
import Plate.Prelude hiding (bool, either, maybe, sequence)
import qualified Data.HashMap.Strict as HM
unit :: Expression
unit = Builtin (ProductType mempty)
string :: Schema
string =
SumType (HM.singleton "string" (Builtin (SSequence (Builtin SInteger))))
bool :: Schema
bool =
SumType (HM.fromList
[ ("true", unit)
, ("false", unit)
])
maybe :: Expression
maybe =
Abstraction "a"
$ Builtin (SumType (HM.fromList
[ ("just", Variable "a")
, ("nothing", unit)
]))
either :: Expression
either =
Abstraction "a"
$ Abstraction "b"
$ Builtin (SumType (HM.fromList
[ ("left", Variable "a")
, ("right", Variable "b")
]))
sequence :: Expression
sequence =
Abstraction "a"
$ Builtin (SumType (HM.fromList
[ ("nil", unit)
, ("cons", Variable "b")
]))
dictionary :: Expression
dictionary =
Abstraction "k"
$ Abstraction "v"
$ Application (Variable sequenceRef) tup
where
tup :: Expression
tup =
Application
(Application (Variable tuple2Ref) (Variable "k"))
(Variable "v")
tuple2 :: Expression
tuple2 =
Abstraction "a"
$ Abstraction "b"
$ Builtin (ProductType (HM.fromList
[ ("1", Variable "a")
, ("2", Variable "b")
]))
expression :: Schema
expression =
SumType (HM.fromList
[ ("variable", Builtin SString)
, ("abstraction",
Builtin (ProductType (HM.fromList
[ ("parameter", Builtin SString)
, ("body", Variable expressionRef)
])))
, ("application",
Builtin (ProductType (HM.fromList
[ ("function", Variable expressionRef)
, ("argument", Variable expressionRef)
])))
, ("type", Variable schemaRef)
])
schema :: Schema
schema =
SumType (HM.fromList
[ ("schema.sum", sumOrProduct)
, ("schema.product", sumOrProduct)
, ("schema.integer", Builtin (ProductType mempty))
, ("schema.set", Variable expressionRef)
, ("schema.dictionary",
Builtin (ProductType (HM.fromList
[ ("keys", Variable expressionRef)
, ("values", Variable expressionRef)
])))
, ("schema.sequence", Variable expressionRef)
, ("schema.string", Builtin (ProductType mempty))
, ("schema.primitive", Variable expressionRef)
])
where
sumOrProduct :: Expression
sumOrProduct = Builtin (SPrimitive (Variable expressionRef))
exampleLibrary :: [(Text, Expression)]
exampleLibrary =
[ (stringRef , Builtin string)
, (boolRef , Builtin bool)
, (maybeRef , maybe)
, (eitherRef , either)
, (sequenceRef , sequence)
, (dictionaryRef, dictionary)
, (tuple2Ref , tuple2)
, (expressionRef, Builtin expression)
, (schemaRef , Builtin schema)
]
stringRef :: Text
stringRef = "string"
boolRef :: Text
boolRef = "bool"
maybeRef :: Text
maybeRef = "maybe"
eitherRef :: Text
eitherRef = "either"
sequenceRef :: Text
sequenceRef = "sequence"
dictionaryRef :: Text
dictionaryRef = "dictionary"
tuple2Ref :: Text
tuple2Ref = "tuple2"
expressionRef :: Text
expressionRef = "expression"
schemaRef :: Text
schemaRef = "schema"