Module: Dhallish

Included in:
Ast
Defined in:
lib/ast.rb,
lib/types.rb,
lib/stdlib.rb,
lib/dhallish.rb

Defined Under Namespace

Modules: Ast, Types Classes: BuiltinFunction, Context, Function, Union

Constant Summary collapse

@@parser =
DhallishGrammarParser.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_ctx(dhallcode, basedir = Dir.pwd) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/dhallish.rb', line 53

def create_ctx(dhallcode, basedir=Dir.pwd)
	empty_ctx = empty_context(basedir)
	ctx, type = evaluate(dhallcode, empty_ctx)
	if type != Types::Record.new({}) or ctx["<#TYPES#>"].nil? or ctx["<#VALS#>"].nil?
		raise DhallError, "return type of dhallcode supplied to `create_ctx` did not return a context (but something of type `#{type}`). use `???`"
	end
	ctx
end

.empty_context(basedir = Dir.pwd) ⇒ Object



20
21
22
23
24
25
# File 'lib/dhallish.rb', line 20

def empty_context(basedir=Dir.pwd)
	ctx = { "<#TYPES#>" => Context.new, "<#VALS#>" => Context.new }
	ctx["<#TYPES#>"]["<#DIR#>"] = basedir
	fill_context(ctx["<#VALS#>"], ctx["<#TYPES#>"])
	ctx
end

.evaluate(dhallcode, ctx = nil, expected_type = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dhallish.rb', line 28

def evaluate(dhallcode, ctx=nil, expected_type=nil)
	if ctx.nil?; ctx = empty_context() end

	rawast = @@parser.parse dhallcode
	if rawast.nil?
		raise DhallError, "#{@@parser.failure_reason} (line/column: #{@@parser.failure_line}:#{@@parser.failure_column})"
	end

	ast = rawast.to_node

	type = ast.compute_type ctx["<#TYPES#>"]
	res = ast.evaluate ctx["<#VALS#>"]

	if !expected_type.nil?
		if !(type == expected_type)
			raise DhallError, "expression return type missmatch: expected `#{expected_type}`, got: `#{type}`"
		else
			res
		end
	else
		[res, type]
	end
end

.fill_context(globalctx, types) ⇒ Object



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/stdlib.rb', line 24

def fill_context(globalctx, types)

	# Types:
	globalctx["Natural"] = Types::Natural
	globalctx["Integer"] = Types::Integer
	globalctx["Double"] = Types::Double
	globalctx["Bool"] = Types::Bool
	globalctx["Text"] = Types::Text
	globalctx["Type"] = Types::Type.new

	types["Natural"] = Types::Type.new(Types::Natural)
	types["Integer"] = Types::Type.new(Types::Integer)
	types["Double"] = Types::Type.new(Types::Double)
	types["Bool"] = Types::Type.new(Types::Bool)
	types["Text"] = Types::Type.new(Types::Text)
	types["Type"] = Types::Type.new(Types::Type.new)

	# Shows:
	globalctx["Natural/show"] = BuiltinFunction.new { |arg| arg.to_s }
	types["Natural/show"] = Types::Function.new(Types::Natural, Types::Text)

	globalctx["Integer/show"] = BuiltinFunction.new { |arg| arg.to_s }
	types["Integer/show"] = Types::Function.new(Types::Integer, Types::Text)

	globalctx["Double/show"] =  BuiltinFunction.new { |arg| arg.to_s }
	types["Double/show"] = Types::Function.new(Types::Double, Types::Text)

	globalctx["Text/show"] = BuiltinFunction.new { |arg| arg }
	types["Text/show"] = Types::Function.new(Types::Text, Types::Text)

	# Casts:
	natToInt = BuiltinFunction.new { |arg| arg }
	globalctx["Natural/toInteger"] = natToInt
	types["Natural/toInteger"] = Types::Function.new(Types::Natural, Types::Integer)

	natToDou = BuiltinFunction.new { |arg| arg.to_f }
	globalctx["Natural/toDouble"] = natToDou
	types["Natural/toDouble"] = Types::Function.new(Types::Natural, Types::Double)


	intToNat = BuiltinFunction.new { |arg| arg.abs  }
	globalctx["Integer/toNatural"] = intToNat
	types["Integer/toNatural"] = Types::Function.new(Types::Integer, Types::Natural)

	intToDou = BuiltinFunction.new { |arg| arg.to_f }
	globalctx["Integer/toDouble"] = intToDou
	types["Integer/toDouble"] = Types::Function.new(Types::Integer, Types::Double)


	douToNat = BuiltinFunction.new { |arg| arg.round.abs }
	globalctx["Double/toNatural"] = douToNat
	types["Double/toNatural"] = Types::Function.new(Types::Double, Types::Natural)

	douToInt = BuiltinFunction.new { |arg| arg.round }
	globalctx["Double/toInteger"] = douToInt
	types["Double/toInteger"] = Types::Function.new(Types::Double, Types::Integer)


	# Lists:
	globalctx["List"] = BuiltinFunction.new { |a| Types::List.new(a) }
	types["List"] = make_fn_type([:list_a], Types::Type.new(Types::List.new(Types::Unresolved.new(:list_a))))

	list_length_type = make_fn_type([:list_len_a], Types::List.new(Types::Unresolved.new(:list_len_a)), Types::Natural)
	globalctx["List/length"] = BuiltinFunction.new{ |a|
		BuiltinFunction.new { |list|
			list.length
		}
	}
	types["List/length"] = list_length_type

	list_fold_type = make_fn_type(
		[:list_fold_a],
		Types::List.new(Types::Unresolved.new(:list_fold_a)),
		[:list_fold_b],
		make_fn_type(:list_fold_a, :list_fold_b, :list_fold_b),
		:list_fold_b, :list_fold_b)
	globalctx["List/fold"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |list|
			BuiltinFunction.new { |b|
				BuiltinFunction.new { |f|
					BuiltinFunction.new { |x|
						acc = x
						list.reverse_each { |elm| acc = f.call(elm).call(acc) }
						acc
					}
				}
			}
		}
	}
	types["List/fold"] = list_fold_type

	list_head_type = make_fn_type(
		[:list_head_a],
		Types::List.new(Types::Unresolved.new(:list_head_a)),
		Types::Optional.new(Types::Unresolved.new(:list_head_a)))
	globalctx["List/head"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |list|
			list.first
		}
	}
	types["List/head"] = list_head_type

	list_last_type = make_fn_type(
		[:list_last_a],
		Types::List.new(Types::Unresolved.new(:list_last_a)),
		Types::Optional.new(Types::Unresolved.new(:list_last_a)))
	globalctx["List/last"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |list|
			list.last
		}
	}
	types["List/last"] = list_last_type

	list_tail_type = make_fn_type(
		[:list_tail_a],
		Types::List.new(Types::Unresolved.new(:list_tail_a)),
		Types::List.new(Types::Unresolved.new(:list_tail_a)))
	globalctx["List/tail"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |list|
			if list.empty?
				[]
			else
				list[1..list.length]
			end
		}
	}
	types["List/tail"] = list_tail_type

	types["List/reverse"] = make_fn_type(
		[:list_reverse_a],
		Types::List.new(Types::Unresolved.new(:list_reverse_a)),
		Types::List.new(Types::Unresolved.new(:list_reverse_a)))
	globalctx["List/reverse"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |list| list.reverse }
	}

	types["List/build"] = make_fn_type(
		[:list_build_a],
		make_fn_type(
			[:list_build_b],
			make_fn_type(:list_build_a, :list_build_b, :list_build_b), :list_build_b, :list_build_b),
		Types::List.new(Types::Unresolved.new(:list_build_a)))
	globalctx["List/build"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |f|
			cons = BuiltinFunction.new { |x|
				BuiltinFunction.new { |list| [x] + list }
			}
			f.call(Types::List.new(a)).call(cons).call([])
		}
	}

	types["List/indexed"] = make_fn_type(
		[:list_indexed_a],
		Types::List.new(Types::Unresolved.new(:list_indexed_a)),
		Types::List.new(Types::Record.new({
			"index" => Types::Natural,
			"value" => Types::Unresolved.new(:list_indexed_a)})))
	globalctx["List/indexed"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |list|
			list.map.with_index { |val, idx|
				{ "index" => idx, "value" => val }
			}
		}
	}

	# Optionals:
	globalctx["Optional"] = BuiltinFunction.new { |a|
		Types::Optional.new(a)
	}
	types["Optional"] = make_fn_type([:opt_a], Types::Type.new(Types::Optional.new(Types::Unresolved.new(:opt_a))))

	optional_fold_type = make_fn_type(
		[:opt_fold_a],
		Types::Optional.new(Types::Unresolved.new(:opt_fold_a)),
		[:opt_fold_b],
		make_fn_type(:opt_fold_a, :opt_fold_b),
		:opt_fold_b, :opt_fold_b)
	globalctx["Optional/fold"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |opt|
			BuiltinFunction.new { |b|
				BuiltinFunction.new { |f|
					BuiltinFunction.new { |x|
						if opt.nil?
							x
						else
							f.call(opt)
						end
					}
				}
			}
		}
	}
	types["Optional/fold"] = optional_fold_type

	types["Optional/build"] = make_fn_type(
		[:opt_build_a],
		make_fn_type([:opt_build_b],
			make_fn_type(:opt_build_a, :opt_build_b), :opt_build_b, :opt_build_b),
		Types::Optional.new(Types::Unresolved.new(:opt_build_a)))
	globalctx["Optional/build"] = BuiltinFunction.new { |a|
		BuiltinFunction.new { |f|
			just = BuiltinFunction.new { |x| x }
			f.call(Types::Optional.new(a)).call(just).call(nil)
		}
	}


	# Naturals:
	natural_fold_type = make_fn_type(
		Types::Natural,
		[:nat_fold_a],
		make_fn_type(:nat_fold_a, :nat_fold_a),
		:nat_fold_a, :nat_fold_a)
	globalctx["Natural/fold"] = BuiltinFunction.new { |n|
		BuiltinFunction.new { |a|
			BuiltinFunction.new { |succ|
				BuiltinFunction.new { |zero|
					res = zero
					n.times { res = succ.call(res) }
					res
				}
			}
		}
	}
	types["Natural/fold"] = natural_fold_type

	types["Natural/even"] = make_fn_type(Types::Natural, Types::Bool)
	globalctx["Natural/even"] = BuiltinFunction.new { |n| n % 2 == 0 }

	types["Natural/odd"] = make_fn_type(Types::Natural, Types::Bool)
	globalctx["Natural/odd"] = BuiltinFunction.new { |n| n % 2 == 1 }

	types["Natural/isZero"] = make_fn_type(Types::Natural, Types::Bool)
	globalctx["Natural/isZero"] = BuiltinFunction.new { |n| n == 0 }

	globalctx["Natural/build"] = BuiltinFunction.new { |f|
		zero = 0
		succ = BuiltinFunction.new { |n| n + 1 }
		f.call(Types::Natural).call(succ).call(zero)
	}
	types["Natural/build"] = make_fn_type(make_fn_type([:nat_build_a], make_fn_type(:nat_build_a, :nat_build_a), :nat_build_a, :nat_build_a), Types::Natural)

end

.make_fn_type(*args, rettype) ⇒ Object

Utility function to create dhallish function types: Use [:name] to introduce a new unresolved type and write :name to use it. Look at ‘fill_context` for examples.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/stdlib.rb', line 9

def make_fn_type(*args, rettype)
	if Types::not_a_type?(rettype); rettype = Types::Unresolved.new(rettype) end
	args.reverse.reduce(rettype) { |rettype, arg|
		argtype = arg
		name = nil
		if arg.is_a? Array
			name = arg[0]
			argtype = Types::Type.new(Types::Unresolved.new(name))
		end
		if Types::not_a_type?(argtype); argtype = Types::Unresolved.new(argtype) end
		Types::Function.new(argtype, rettype, name)
	}
end

.mergeRecordsPrefereRight(a, b) ⇒ Object



434
435
436
437
438
# File 'lib/types.rb', line 434

def mergeRecordsPrefereRight(a, b)
	mergedVals = a.clone
	b.each { |key, val| mergedVals[key] = val }
	mergedVals
end

.mergeRecordsRecursively(a, b) ⇒ Object

TODO: .val weg!



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/types.rb', line 409

def mergeRecordsRecursively(a, b)
	merged = a.clone
	b.each { |key, val|
		if merged.key? key
			if val.is_a? Hash and merged[key].is_a? Hash
				merged[key] = mergeRecordsRecursively(merged[key], val)
			else
				raise DhallError, "key `#{key}` apeares in left and right side of `/\\` (should not happen becouse of static type checks)"
			end
		else
			merged[key] = val
		end
	}
	merged
end

.mergeRecordTypes(a, b) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/types.rb', line 388

def mergeRecordTypes(a, b)
	assert("records expected") { a.is_a? Types::Record and b.is_a? Types::Record }

	merged = a.types.clone
	b.types.each { |key, type|
		if merged.key? key
			if type.is_a? Types::Record and merged[key].is_a? Types::Record
				merged[key] = mergeRecordTypes(merged[key], type)
			else
				raise DhallError, "key `#{key}` apeares in left and right side of `//\\\\` with different types"
			end
		else
			merged[key] = type
		end
	}

	Types::Record.new merged
end

.mergeRecordTypesPrefereRight(a, b) ⇒ Object



426
427
428
429
430
431
# File 'lib/types.rb', line 426

def mergeRecordTypesPrefereRight(a, b)
	assert("expecting records for `//`") { a.is_a? Types::Record and b.is_a? Types::Record }
	mergedTypes = a.types.clone
	b.types.each { |key, type| mergedTypes[key] = type }
	Types::Record.new(mergedTypes)
end

.to_json(dhallval) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/types.rb', line 292

def to_json(dhallval)
	case dhallval
	when Integer
		dhallval.to_s
	when Float
		dhallval.to_s
	when String
		 "\"#{escape_str(dhallval)}\""
	when TrueClass
		"true"
	when FalseClass
		"false"
	when Array
		"[#{ dhallval.map{ |val| to_json(val) }.join(", ") }]"
	when Hash
		"{#{ dhallval.map{ |key, val| "\"#{escape_str(key)}\": #{ to_json(val) }" }.join(", ") }}"
	when nil
		"null"
	else
		if Types::is_a_type? dhallval
			"\"#{dhallval.to_s}\""
		else
			"\"<#{escape_str(dhallval.class.to_s)}##{dhallval.hash.abs.to_s(16)}>\""
		end
	end
end

Instance Method Details

#create_ctx_from_file(dhallfile, basedir = nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/dhallish.rb', line 63

def create_ctx_from_file(dhallfile, basedir=nil)
	if basedir.nil?; basedir = File.dirname(dhallfile) end
	file = File.open(dhallfile)
	res = create_ctx(file.read, basedir)
	file.close
	res
end