dhall 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -72,9 +72,9 @@ module Dhall
72
72
  end
73
73
 
74
74
  KINDS = [
75
- Dhall::Variable["Type"],
76
- Dhall::Variable["Kind"],
77
- Dhall::Variable["Sort"]
75
+ Builtins[:Type],
76
+ Builtins[:Kind],
77
+ Builtins[:Sort]
78
78
  ].freeze
79
79
 
80
80
  class Variable
@@ -84,38 +84,12 @@ module Dhall
84
84
  @var = var
85
85
  end
86
86
 
87
- BUILTIN = {
88
- "Type" => Dhall::Variable["Kind"],
89
- "Kind" => Dhall::Variable["Sort"],
90
- "Bool" => Dhall::Variable["Type"],
91
- "Natural" => Dhall::Variable["Type"],
92
- "Integer" => Dhall::Variable["Type"],
93
- "Double" => Dhall::Variable["Type"],
94
- "Text" => Dhall::Variable["Type"],
95
- "List" => Dhall::Forall.of_arguments(
96
- Dhall::Variable["Type"],
97
- body: Dhall::Variable["Type"]
98
- ),
99
- "Optional" => Dhall::Forall.of_arguments(
100
- Dhall::Variable["Type"],
101
- body: Dhall::Variable["Type"]
102
- ),
103
- "None" => Dhall::Forall.new(
104
- var: "A",
105
- type: Dhall::Variable["Type"],
106
- body: Dhall::Application.new(
107
- function: Dhall::Variable["Optional"],
108
- argument: Dhall::Variable["A"]
109
- )
110
- )
111
- }.freeze
112
-
113
87
  def annotate(context)
114
88
  raise TypeError, "Sort has no Type, Kind, or Sort" if @var.name == "Sort"
115
89
 
116
90
  Dhall::TypeAnnotation.new(
117
91
  value: @var,
118
- type: BUILTIN.fetch(@var.name) { context.fetch(@var) }
92
+ type: context.fetch(@var)
119
93
  )
120
94
  end
121
95
  end
@@ -130,6 +104,7 @@ module Dhall
130
104
  def initialize(lit)
131
105
  @lit = lit
132
106
  @type = Dhall::Variable[lit.class.name.split(/::/).last]
107
+ @type = Builtins[@type.name.to_sym] || @type
133
108
  end
134
109
 
135
110
  def annotate(*)
@@ -170,14 +145,14 @@ module Dhall
170
145
  def annotate(context)
171
146
  chunks = Chunks.new(@lit.chunks).map { |c|
172
147
  TypeChecker.for(c).annotate(context).tap do |annotated|
173
- TypeChecker.assert annotated.type, Dhall::Variable["Text"],
148
+ TypeChecker.assert annotated.type, Builtins[:Text],
174
149
  "Cannot interpolate #{annotated.type}"
175
150
  end
176
151
  }.to_a
177
152
 
178
153
  Dhall::TypeAnnotation.new(
179
154
  value: @lit.with(chunks: chunks),
180
- type: Dhall::Variable["Text"]
155
+ type: Builtins[:Text]
181
156
  )
182
157
  end
183
158
  end
@@ -194,9 +169,9 @@ module Dhall
194
169
 
195
170
  class AnnotatedIf
196
171
  def initialize(expr, apred, athen, aelse, context:)
197
- TypeChecker.assert apred.type, Dhall::Variable["Bool"],
172
+ TypeChecker.assert apred.type, Builtins[:Bool],
198
173
  "If must have a predicate of type Bool"
199
- TypeChecker.assert_type athen.type, Dhall::Variable["Type"],
174
+ TypeChecker.assert_type athen.type, Builtins[:Type],
200
175
  "If branches must have types of type Type",
201
176
  context: context
202
177
  TypeChecker.assert aelse.type, athen.type,
@@ -225,13 +200,13 @@ module Dhall
225
200
 
226
201
  class Operator
227
202
  {
228
- Dhall::Operator::And => Dhall::Variable["Bool"],
229
- Dhall::Operator::Or => Dhall::Variable["Bool"],
230
- Dhall::Operator::Equal => Dhall::Variable["Bool"],
231
- Dhall::Operator::NotEqual => Dhall::Variable["Bool"],
232
- Dhall::Operator::Plus => Dhall::Variable["Natural"],
233
- Dhall::Operator::Times => Dhall::Variable["Natural"],
234
- Dhall::Operator::TextConcatenate => Dhall::Variable["Text"]
203
+ Dhall::Operator::And => Builtins[:Bool],
204
+ Dhall::Operator::Or => Builtins[:Bool],
205
+ Dhall::Operator::Equal => Builtins[:Bool],
206
+ Dhall::Operator::NotEqual => Builtins[:Bool],
207
+ Dhall::Operator::Plus => Builtins[:Natural],
208
+ Dhall::Operator::Times => Builtins[:Natural],
209
+ Dhall::Operator::TextConcatenate => Builtins[:Text]
235
210
  }.each do |node_type, type|
236
211
  TypeChecker.register self, node_type, type
237
212
  end
@@ -270,7 +245,7 @@ module Dhall
270
245
  module IsList
271
246
  def self.===(other)
272
247
  other.is_a?(Dhall::Application) &&
273
- other.function == Dhall::Variable["List"]
248
+ other.function == Builtins[:List]
274
249
  end
275
250
  end
276
251
 
@@ -387,7 +362,7 @@ module Dhall
387
362
  end
388
363
 
389
364
  def annotate(context)
390
- TypeChecker.assert_type @expr.element_type, Dhall::Variable["Type"],
365
+ TypeChecker.assert_type @expr.element_type, Builtins[:Type],
391
366
  "EmptyList element type not of type Type",
392
367
  context: context
393
368
 
@@ -430,7 +405,7 @@ module Dhall
430
405
  Util::ArrayOf.new(alist.element_type),
431
406
  "Non-homogenous List"
432
407
 
433
- TypeChecker.assert_type alist.element_type, Dhall::Variable["Type"],
408
+ TypeChecker.assert_type alist.element_type, Builtins[:Type],
434
409
  "List type not of type Type", context: context
435
410
 
436
411
  alist.annotation
@@ -447,7 +422,7 @@ module Dhall
447
422
  def annotate(context)
448
423
  TypeChecker.assert(
449
424
  TypeChecker.for(@expr.value_type).annotate(context).type,
450
- Dhall::Variable["Type"],
425
+ Builtins[:Type],
451
426
  "OptionalNone element type not of type Type"
452
427
  )
453
428
 
@@ -469,7 +444,7 @@ module Dhall
469
444
  some = asome.with(value_type: asome.value.type)
470
445
 
471
446
  type_type = TypeChecker.for(some.value_type).annotate(context).type
472
- TypeChecker.assert type_type, Dhall::Variable["Type"],
447
+ TypeChecker.assert type_type, Builtins[:Type],
473
448
  "Some type not of type Type, was: #{type_type}"
474
449
 
475
450
  Dhall::TypeAnnotation.new(type: some.type, value: some)
@@ -486,7 +461,7 @@ module Dhall
486
461
  def annotate(*)
487
462
  Dhall::TypeAnnotation.new(
488
463
  value: @expr,
489
- type: Dhall::Variable["Type"]
464
+ type: Builtins[:Type]
490
465
  )
491
466
  end
492
467
  end
@@ -562,7 +537,7 @@ module Dhall
562
537
 
563
538
  class Selector
564
539
  def self.for(annotated_record)
565
- if annotated_record.type == Dhall::Variable["Type"]
540
+ if annotated_record.type == Builtins[:Type]
566
541
  TypeSelector.new(annotated_record.value)
567
542
  elsif annotated_record.type.class == Dhall::RecordType
568
543
  new(annotated_record.type)
@@ -727,7 +702,7 @@ module Dhall
727
702
 
728
703
  TypeChecker.assert(
729
704
  kind,
730
- Dhall::Variable["Type"],
705
+ Builtins[:Type],
731
706
  "Merge must have kind Type"
732
707
  )
733
708
 
@@ -934,10 +909,33 @@ module Dhall
934
909
  end
935
910
 
936
911
  BUILTIN_TYPES = {
912
+ "Bool" => Builtins[:Type],
913
+ "Type" => Builtins[:Kind],
914
+ "Kind" => Builtins[:Sort],
915
+ "Natural" => Builtins[:Type],
916
+ "Integer" => Builtins[:Type],
917
+ "Double" => Builtins[:Type],
918
+ "Text" => Builtins[:Type],
919
+ "List" => Dhall::Forall.of_arguments(
920
+ Builtins[:Type],
921
+ body: Builtins[:Type]
922
+ ),
923
+ "Optional" => Dhall::Forall.of_arguments(
924
+ Builtins[:Type],
925
+ body: Builtins[:Type]
926
+ ),
927
+ "None" => Dhall::Forall.new(
928
+ var: "A",
929
+ type: Builtins[:Type],
930
+ body: Dhall::Application.new(
931
+ function: Builtins[:Optional],
932
+ argument: Dhall::Variable["A"]
933
+ )
934
+ ),
937
935
  "Natural/build" => Dhall::Forall.of_arguments(
938
936
  Dhall::Forall.new(
939
937
  var: "natural",
940
- type: Dhall::Variable["Type"],
938
+ type: Builtins[:Type],
941
939
  body: Dhall::Forall.new(
942
940
  var: "succ",
943
941
  type: Dhall::Forall.of_arguments(
@@ -951,13 +949,13 @@ module Dhall
951
949
  )
952
950
  )
953
951
  ),
954
- body: Dhall::Variable["Natural"]
952
+ body: Builtins[:Natural]
955
953
  ),
956
954
  "Natural/fold" => Dhall::Forall.of_arguments(
957
- Dhall::Variable["Natural"],
955
+ Builtins[:Natural],
958
956
  body: Dhall::Forall.new(
959
957
  var: "natural",
960
- type: Dhall::Variable["Type"],
958
+ type: Builtins[:Type],
961
959
  body: Dhall::Forall.new(
962
960
  var: "succ",
963
961
  type: Dhall::Forall.of_arguments(
@@ -973,36 +971,36 @@ module Dhall
973
971
  )
974
972
  ),
975
973
  "Natural/isZero" => Dhall::Forall.of_arguments(
976
- Dhall::Variable["Natural"],
977
- body: Dhall::Variable["Bool"]
974
+ Builtins[:Natural],
975
+ body: Builtins[:Bool]
978
976
  ),
979
977
  "Natural/even" => Dhall::Forall.of_arguments(
980
- Dhall::Variable["Natural"],
981
- body: Dhall::Variable["Bool"]
978
+ Builtins[:Natural],
979
+ body: Builtins[:Bool]
982
980
  ),
983
981
  "Natural/odd" => Dhall::Forall.of_arguments(
984
- Dhall::Variable["Natural"],
985
- body: Dhall::Variable["Bool"]
982
+ Builtins[:Natural],
983
+ body: Builtins[:Bool]
986
984
  ),
987
985
  "Natural/toInteger" => Dhall::Forall.of_arguments(
988
- Dhall::Variable["Natural"],
989
- body: Dhall::Variable["Integer"]
986
+ Builtins[:Natural],
987
+ body: Builtins[:Integer]
990
988
  ),
991
989
  "Natural/show" => Dhall::Forall.of_arguments(
992
- Dhall::Variable["Natural"],
993
- body: Dhall::Variable["Text"]
990
+ Builtins[:Natural],
991
+ body: Builtins[:Text]
994
992
  ),
995
993
  "Text/show" => Dhall::Forall.of_arguments(
996
- Dhall::Variable["Text"],
997
- body: Dhall::Variable["Text"]
994
+ Builtins[:Text],
995
+ body: Builtins[:Text]
998
996
  ),
999
997
  "List/build" => Dhall::Forall.new(
1000
998
  var: "a",
1001
- type: Dhall::Variable["Type"],
999
+ type: Builtins[:Type],
1002
1000
  body: Dhall::Forall.of_arguments(
1003
1001
  Dhall::Forall.new(
1004
1002
  var: "list",
1005
- type: Dhall::Variable["Type"],
1003
+ type: Builtins[:Type],
1006
1004
  body: Dhall::Forall.new(
1007
1005
  var: "cons",
1008
1006
  type: Dhall::Forall.of_arguments(
@@ -1018,22 +1016,22 @@ module Dhall
1018
1016
  )
1019
1017
  ),
1020
1018
  body: Dhall::Application.new(
1021
- function: Dhall::Variable["List"],
1019
+ function: Builtins[:List],
1022
1020
  argument: Dhall::Variable["a"]
1023
1021
  )
1024
1022
  )
1025
1023
  ),
1026
1024
  "List/fold" => Dhall::Forall.new(
1027
1025
  var: "a",
1028
- type: Dhall::Variable["Type"],
1026
+ type: Builtins[:Type],
1029
1027
  body: Dhall::Forall.of_arguments(
1030
1028
  Dhall::Application.new(
1031
- function: Dhall::Variable["List"],
1029
+ function: Builtins[:List],
1032
1030
  argument: Dhall::Variable["a"]
1033
1031
  ),
1034
1032
  body: Dhall::Forall.new(
1035
1033
  var: "list",
1036
- type: Dhall::Variable["Type"],
1034
+ type: Builtins[:Type],
1037
1035
  body: Dhall::Forall.new(
1038
1036
  var: "cons",
1039
1037
  type: Dhall::Forall.of_arguments(
@@ -1052,56 +1050,56 @@ module Dhall
1052
1050
  ),
1053
1051
  "List/length" => Dhall::Forall.new(
1054
1052
  var: "a",
1055
- type: Dhall::Variable["Type"],
1053
+ type: Builtins[:Type],
1056
1054
  body: Dhall::Forall.of_arguments(
1057
1055
  Dhall::Application.new(
1058
- function: Dhall::Variable["List"],
1056
+ function: Builtins[:List],
1059
1057
  argument: Dhall::Variable["a"]
1060
1058
  ),
1061
- body: Dhall::Variable["Natural"]
1059
+ body: Builtins[:Natural]
1062
1060
  )
1063
1061
  ),
1064
1062
  "List/head" => Dhall::Forall.new(
1065
1063
  var: "a",
1066
- type: Dhall::Variable["Type"],
1064
+ type: Builtins[:Type],
1067
1065
  body: Dhall::Forall.of_arguments(
1068
1066
  Dhall::Application.new(
1069
- function: Dhall::Variable["List"],
1067
+ function: Builtins[:List],
1070
1068
  argument: Dhall::Variable["a"]
1071
1069
  ),
1072
1070
  body: Dhall::Application.new(
1073
- function: Dhall::Variable["Optional"],
1071
+ function: Builtins[:Optional],
1074
1072
  argument: Dhall::Variable["a"]
1075
1073
  )
1076
1074
  )
1077
1075
  ),
1078
1076
  "List/last" => Dhall::Forall.new(
1079
1077
  var: "a",
1080
- type: Dhall::Variable["Type"],
1078
+ type: Builtins[:Type],
1081
1079
  body: Dhall::Forall.of_arguments(
1082
1080
  Dhall::Application.new(
1083
- function: Dhall::Variable["List"],
1081
+ function: Builtins[:List],
1084
1082
  argument: Dhall::Variable["a"]
1085
1083
  ),
1086
1084
  body: Dhall::Application.new(
1087
- function: Dhall::Variable["Optional"],
1085
+ function: Builtins[:Optional],
1088
1086
  argument: Dhall::Variable["a"]
1089
1087
  )
1090
1088
  )
1091
1089
  ),
1092
1090
  "List/indexed" => Dhall::Forall.new(
1093
1091
  var: "a",
1094
- type: Dhall::Variable["Type"],
1092
+ type: Builtins[:Type],
1095
1093
  body: Dhall::Forall.of_arguments(
1096
1094
  Dhall::Application.new(
1097
- function: Dhall::Variable["List"],
1095
+ function: Builtins[:List],
1098
1096
  argument: Dhall::Variable["a"]
1099
1097
  ),
1100
1098
  body: Dhall::Application.new(
1101
- function: Dhall::Variable["List"],
1099
+ function: Builtins[:List],
1102
1100
  argument: Dhall::RecordType.new(
1103
1101
  record: {
1104
- "index" => Dhall::Variable["Natural"],
1102
+ "index" => Builtins[:Natural],
1105
1103
  "value" => Dhall::Variable["a"]
1106
1104
  }
1107
1105
  )
@@ -1110,29 +1108,29 @@ module Dhall
1110
1108
  ),
1111
1109
  "List/reverse" => Dhall::Forall.new(
1112
1110
  var: "a",
1113
- type: Dhall::Variable["Type"],
1111
+ type: Builtins[:Type],
1114
1112
  body: Dhall::Forall.of_arguments(
1115
1113
  Dhall::Application.new(
1116
- function: Dhall::Variable["List"],
1114
+ function: Builtins[:List],
1117
1115
  argument: Dhall::Variable["a"]
1118
1116
  ),
1119
1117
  body: Dhall::Application.new(
1120
- function: Dhall::Variable["List"],
1118
+ function: Builtins[:List],
1121
1119
  argument: Dhall::Variable["a"]
1122
1120
  )
1123
1121
  )
1124
1122
  ),
1125
1123
  "Optional/fold" => Dhall::Forall.new(
1126
1124
  var: "a",
1127
- type: Dhall::Variable["Type"],
1125
+ type: Builtins[:Type],
1128
1126
  body: Dhall::Forall.of_arguments(
1129
1127
  Dhall::Application.new(
1130
- function: Dhall::Variable["Optional"],
1128
+ function: Builtins[:Optional],
1131
1129
  argument: Dhall::Variable["a"]
1132
1130
  ),
1133
1131
  body: Dhall::Forall.new(
1134
1132
  var: "optional",
1135
- type: Dhall::Variable["Type"],
1133
+ type: Builtins[:Type],
1136
1134
  body: Dhall::Forall.new(
1137
1135
  var: "just",
1138
1136
  type: Dhall::Forall.of_arguments(
@@ -1150,11 +1148,11 @@ module Dhall
1150
1148
  ),
1151
1149
  "Optional/build" => Dhall::Forall.new(
1152
1150
  var: "a",
1153
- type: Dhall::Variable["Type"],
1151
+ type: Builtins[:Type],
1154
1152
  body: Dhall::Forall.of_arguments(
1155
1153
  Dhall::Forall.new(
1156
1154
  var: "optional",
1157
- type: Dhall::Variable["Type"],
1155
+ type: Builtins[:Type],
1158
1156
  body: Dhall::Forall.new(
1159
1157
  var: "just",
1160
1158
  type: Dhall::Forall.of_arguments(
@@ -1169,22 +1167,22 @@ module Dhall
1169
1167
  )
1170
1168
  ),
1171
1169
  body: Dhall::Application.new(
1172
- function: Dhall::Variable["Optional"],
1170
+ function: Builtins[:Optional],
1173
1171
  argument: Dhall::Variable["a"]
1174
1172
  )
1175
1173
  )
1176
1174
  ),
1177
1175
  "Integer/show" => Dhall::Forall.of_arguments(
1178
- Dhall::Variable["Integer"],
1179
- body: Dhall::Variable["Text"]
1176
+ Builtins[:Integer],
1177
+ body: Builtins[:Text]
1180
1178
  ),
1181
1179
  "Integer/toDouble" => Dhall::Forall.of_arguments(
1182
- Dhall::Variable["Integer"],
1183
- body: Dhall::Variable["Double"]
1180
+ Builtins[:Integer],
1181
+ body: Builtins[:Double]
1184
1182
  ),
1185
1183
  "Double/show" => Dhall::Forall.of_arguments(
1186
- Dhall::Variable["Double"],
1187
- body: Dhall::Variable["Text"]
1184
+ Builtins[:Double],
1185
+ body: Builtins[:Text]
1188
1186
  )
1189
1187
  }.freeze
1190
1188