Skip to content

Commit e6f7e9d

Browse files
committed
Adjust function naming
- argument -> input - parameter-> input - body -> output
1 parent 94d2dab commit e6f7e9d

File tree

12 files changed

+289
-268
lines changed

12 files changed

+289
-268
lines changed

examples/prelude.pi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ record {
3232
(a-b-c : Fun (a : A) (b : B a) -> C a b)
3333
-> (Fun (a : A) -> C a (a-b a)),
3434

35-
||| Flip the order of the first two arguments to a function
35+
||| Flip the order of the first two inputs to a function
3636
flip : Fun (A B C : Type) -> (A -> B -> C) -> (B -> A -> C),
3737

38-
||| Flip the order of the first two arguments to a dependent function.
38+
||| Flip the order of the first two inputs to a dependent function.
3939
dep-flip :
4040
Fun (A B : Type)
4141
(C : A -> B -> Type)
@@ -47,9 +47,8 @@ record {
4747

4848
||| Dependent substitution.
4949
|||
50-
||| Takes three arguments and then returns the first argument applied to the
51-
||| third, which is then applied to the result of the second argument applied to
52-
||| the third.
50+
||| Takes three inputs and then returns the first input applied to the third,
51+
||| which is then applied to the result of the second input applied to the third.
5352
|||
5453
||| Also known as the 'S Combinator' in the [SKI combinator calculus][ski-wiki].
5554
|||

pikelet/src/lang/core/semantics.rs

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ impl Closure {
132132
}
133133

134134
/// Eliminate a closure.
135-
pub fn elim(&self, globals: &Globals, argument: Arc<Value>) -> Arc<Value> {
135+
pub fn elim(&self, globals: &Globals, input: Arc<Value>) -> Arc<Value> {
136136
let mut values = self.values.clone();
137-
values.push(argument);
137+
values.push(input);
138138
eval_term(globals, self.universe_offset, &mut values, &self.term)
139139
}
140140
}
@@ -232,8 +232,8 @@ impl LazyValue {
232232
Some(LazyInit::ApplyElim(head, Elim::Record(label))) => {
233233
apply_record_elim(head.force(globals), &label)
234234
}
235-
Some(LazyInit::ApplyElim(head, Elim::Function(argument))) => {
236-
apply_function_elim(globals, head.force(globals), argument)
235+
Some(LazyInit::ApplyElim(head, Elim::Function(input))) => {
236+
apply_function_elim(globals, head.force(globals), input)
237237
}
238238
None => panic!("Lazy instance has previously been poisoned"),
239239
})
@@ -312,21 +312,21 @@ pub fn eval_term(
312312
let head = eval_term(globals, universe_offset, values, head);
313313
apply_record_elim(&head, label)
314314
}
315-
Term::FunctionType(param_name_hint, param_type, body_type) => {
316-
let param_name_hint = param_name_hint.clone();
317-
let param_type = eval_term(globals, universe_offset, values, param_type);
318-
let body_type = Closure::new(universe_offset, values.clone(), body_type.clone());
319-
320-
Arc::new(Value::FunctionType(param_name_hint, param_type, body_type))
315+
Term::FunctionType(input_name_hint, input_type, output_type) => {
316+
Arc::new(Value::FunctionType(
317+
input_name_hint.clone(),
318+
eval_term(globals, universe_offset, values, input_type),
319+
Closure::new(universe_offset, values.clone(), output_type.clone()),
320+
))
321321
}
322-
Term::FunctionTerm(param_name, body) => Arc::new(Value::FunctionTerm(
323-
param_name.clone(),
324-
Closure::new(universe_offset, values.clone(), body.clone()),
322+
Term::FunctionTerm(input_name, output_term) => Arc::new(Value::FunctionTerm(
323+
input_name.clone(),
324+
Closure::new(universe_offset, values.clone(), output_term.clone()),
325325
)),
326-
Term::FunctionElim(head, argument) => {
326+
Term::FunctionElim(head, input) => {
327327
let head = eval_term(globals, universe_offset, values, head);
328-
let argument = LazyValue::eval_term(universe_offset, values.clone(), argument.clone());
329-
apply_function_elim(globals, &head, Arc::new(argument))
328+
let input = LazyValue::eval_term(universe_offset, values.clone(), input.clone());
329+
apply_function_elim(globals, &head, Arc::new(input))
330330
}
331331
Term::Lift(term, offset) => {
332332
let universe_offset = (universe_offset + *offset).unwrap(); // FIXME: Handle overflow
@@ -379,26 +379,22 @@ fn apply_record_elim(head_value: &Value, label: &str) -> Arc<Value> {
379379
}
380380

381381
/// Apply a function term elimination.
382-
fn apply_function_elim(
383-
globals: &Globals,
384-
head_value: &Value,
385-
argument: Arc<LazyValue>,
386-
) -> Arc<Value> {
382+
fn apply_function_elim(globals: &Globals, head_value: &Value, input: Arc<LazyValue>) -> Arc<Value> {
387383
match head_value {
388384
Value::Stuck(head, spine) => {
389385
let mut spine = spine.clone(); // FIXME: Avoid clone of spine?
390-
spine.push(Elim::Function(argument));
386+
spine.push(Elim::Function(input));
391387
Arc::new(Value::Stuck(head.clone(), spine))
392388
}
393389
Value::Unstuck(head, spine, value) => {
394390
let mut spine = spine.clone(); // FIXME: Avoid clone of spine?
395-
spine.push(Elim::Function(argument.clone()));
396-
let value = LazyValue::apply_elim(value.clone(), Elim::Function(argument));
391+
spine.push(Elim::Function(input.clone()));
392+
let value = LazyValue::apply_elim(value.clone(), Elim::Function(input));
397393
Arc::new(Value::Unstuck(head.clone(), spine, Arc::new(value)))
398394
}
399395

400-
Value::FunctionTerm(_, body_closure) => {
401-
body_closure.elim(globals, argument.force(globals).clone())
396+
Value::FunctionTerm(_, output_closure) => {
397+
output_closure.elim(globals, input.force(globals).clone())
402398
}
403399

404400
_ => Arc::new(Value::Error),
@@ -430,9 +426,9 @@ fn read_back_spine(
430426

431427
spine.iter().fold(head, |head, elim| match elim {
432428
Elim::Record(label) => Term::RecordElim(Arc::new(head), label.clone()),
433-
Elim::Function(argument) => {
434-
let argument = read_back_value(globals, local_size, unfold, argument.force(globals));
435-
Term::FunctionElim(Arc::new(head), Arc::new(argument))
429+
Elim::Function(input) => {
430+
let input = read_back_value(globals, local_size, unfold, input.force(globals));
431+
Term::FunctionElim(Arc::new(head), Arc::new(input))
436432
}
437433
})
438434
}
@@ -490,20 +486,22 @@ pub fn read_back_value(
490486

491487
Term::RecordTerm(term_entries)
492488
}
493-
Value::FunctionType(param_name_hint, param_type, body_closure) => {
489+
Value::FunctionType(input_name_hint, input_type, output_closure) => {
494490
let local = Arc::new(Value::local(local_size.next_level()));
495-
let param_type = Arc::new(read_back_value(globals, local_size, unfold, param_type));
496-
let body_type = body_closure.elim(globals, local);
497-
let body_type = read_back_value(globals, local_size.increment(), unfold, &body_type);
491+
let input_type = Arc::new(read_back_value(globals, local_size, unfold, input_type));
492+
let output_type = output_closure.elim(globals, local);
493+
let output_type =
494+
read_back_value(globals, local_size.increment(), unfold, &output_type);
498495

499-
Term::FunctionType(param_name_hint.clone(), param_type, Arc::new(body_type))
496+
Term::FunctionType(input_name_hint.clone(), input_type, Arc::new(output_type))
500497
}
501-
Value::FunctionTerm(param_name_hint, body_closure) => {
498+
Value::FunctionTerm(input_name_hint, output_closure) => {
502499
let local = Arc::new(Value::local(local_size.next_level()));
503-
let body = body_closure.elim(globals, local);
504-
let body = read_back_value(globals, local_size.increment(), unfold, &body);
500+
let output_term = output_closure.elim(globals, local);
501+
let output_term =
502+
read_back_value(globals, local_size.increment(), unfold, &output_term);
505503

506-
Term::FunctionTerm(param_name_hint.clone(), Arc::new(body))
504+
Term::FunctionTerm(input_name_hint.clone(), Arc::new(output_term))
507505
}
508506

509507
Value::Error => Term::Error,
@@ -523,11 +521,11 @@ pub fn is_equal_spine(
523521

524522
for (elim0, elim1) in Iterator::zip(spine0.iter(), spine1.iter()) {
525523
match (elim0, elim1) {
526-
(Elim::Function(argument0), Elim::Function(argument1)) => {
527-
let argument0 = argument0.force(globals);
528-
let argument1 = argument1.force(globals);
524+
(Elim::Function(input0), Elim::Function(input1)) => {
525+
let input0 = input0.force(globals);
526+
let input1 = input1.force(globals);
529527

530-
if !is_equal(globals, local_size, argument0, argument1) {
528+
if !is_equal(globals, local_size, input0, input1) {
531529
return false;
532530
}
533531
}
@@ -620,25 +618,29 @@ fn is_equal(globals: &Globals, local_size: LocalSize, value0: &Value, value1: &V
620618
)
621619
}
622620
(
623-
Value::FunctionType(_, param_type0, body_closure0),
624-
Value::FunctionType(_, param_type1, body_closure1),
621+
Value::FunctionType(_, input_type0, output_closure0),
622+
Value::FunctionType(_, input_type1, output_closure1),
625623
) => {
626-
if !is_equal(globals, local_size, param_type1, param_type0) {
624+
if !is_equal(globals, local_size, input_type1, input_type0) {
627625
return false;
628626
}
629627

630628
let local = Arc::new(Value::local(local_size.next_level()));
631-
let body_term0 = body_closure0.elim(globals, local.clone());
632-
let body_term1 = body_closure1.elim(globals, local);
633-
634-
is_equal(globals, local_size.increment(), &body_term0, &body_term1)
629+
is_equal(
630+
globals,
631+
local_size.increment(),
632+
&output_closure0.elim(globals, local.clone()),
633+
&output_closure1.elim(globals, local),
634+
)
635635
}
636-
(Value::FunctionTerm(_, body_closure0), Value::FunctionTerm(_, body_closure1)) => {
636+
(Value::FunctionTerm(_, output_closure0), Value::FunctionTerm(_, output_closure1)) => {
637637
let local = Arc::new(Value::local(local_size.next_level()));
638-
let body_value0 = body_closure0.elim(globals, local.clone());
639-
let body_value1 = body_closure1.elim(globals, local);
640-
641-
is_equal(globals, local_size.increment(), &body_value0, &body_value1)
638+
is_equal(
639+
globals,
640+
local_size.increment(),
641+
&output_closure0.elim(globals, local.clone()),
642+
&output_closure1.elim(globals, local),
643+
)
642644
}
643645

644646
// Errors are always treated as subtypes, regardless of what they are compared with.
@@ -711,18 +713,23 @@ pub fn is_subtype(
711713
true
712714
}
713715
(
714-
Value::FunctionType(_, param_type0, body_closure0),
715-
Value::FunctionType(_, param_type1, body_closure1),
716+
Value::FunctionType(_, input_type0, output_closure0),
717+
Value::FunctionType(_, input_type1, output_closure1),
716718
) => {
717-
if !is_subtype(globals, local_size, param_type1, param_type0) {
719+
if !is_subtype(globals, local_size, input_type1, input_type0) {
718720
return false;
719721
}
720722

721723
let local = Arc::new(Value::local(local_size.next_level()));
722-
let body_term0 = body_closure0.elim(globals, local.clone());
723-
let body_term1 = body_closure1.elim(globals, local);
724-
725-
is_subtype(globals, local_size.increment(), &body_term0, &body_term1)
724+
let output_term0 = output_closure0.elim(globals, local.clone());
725+
let output_term1 = output_closure1.elim(globals, local);
726+
727+
is_subtype(
728+
globals,
729+
local_size.increment(),
730+
&output_term0,
731+
&output_term1,
732+
)
726733
}
727734

728735
// Errors are always treated as subtypes, regardless of what they are compared with.

pikelet/src/lang/core/typing.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,17 @@ pub fn check_type(state: &mut State<'_>, term: &Term, expected_type: &Arc<Value>
190190
});
191191
}
192192
}
193-
(Term::FunctionTerm(_, body), Value::FunctionType(_, param_type, body_closure)) => {
194-
let param = state.push_local_param(param_type.clone());
195-
check_type(state, body, &body_closure.elim(state.globals, param));
193+
(
194+
Term::FunctionTerm(_, output_term),
195+
Value::FunctionType(_, input_type, output_closure),
196+
) => {
197+
let input_term = state.push_local_param(input_type.clone());
198+
let output_type = output_closure.elim(state.globals, input_term);
199+
check_type(state, output_term, &output_type);
196200
state.pop_local();
197201
}
198202
(Term::FunctionTerm(_, _), _) => {
199-
state.report(Message::TooManyParameters);
203+
state.report(Message::TooManyInputsInFunctionTerm);
200204
}
201205
(term, _) => match synth_type(state, term) {
202206
found_type if state.is_subtype(&found_type, expected_type) => {}
@@ -304,12 +308,12 @@ pub fn synth_type(state: &mut State<'_>, term: &Term) -> Arc<Value> {
304308

305309
Arc::new(Value::Universe(max_level))
306310
}
307-
Term::RecordElim(head, label) => {
308-
let head_type = synth_type(state, head);
311+
Term::RecordElim(head_term, label) => {
312+
let head_type = synth_type(state, head_term);
309313

310314
match head_type.force(state.globals) {
311315
Value::RecordType(closure) => {
312-
let head_value = state.eval_term(head);
316+
let head_value = state.eval_term(head_term);
313317

314318
if let Some(entry_type) = state.record_elim_type(&head_value, label, closure) {
315319
return entry_type;
@@ -325,20 +329,20 @@ pub fn synth_type(state: &mut State<'_>, term: &Term) -> Arc<Value> {
325329
});
326330
Arc::new(Value::Error)
327331
}
328-
Term::FunctionType(_, param_type, body_type) => {
329-
let param_level = is_type(state, param_type);
330-
let param_type = match param_level {
332+
Term::FunctionType(_, input_type, output_type) => {
333+
let input_level = is_type(state, input_type);
334+
let input_type = match input_level {
331335
None => Arc::new(Value::Error),
332-
Some(_) => state.eval_term(param_type),
336+
Some(_) => state.eval_term(input_type),
333337
};
334338

335-
state.push_local_param(param_type);
336-
let body_level = is_type(state, body_type);
339+
state.push_local_param(input_type);
340+
let output_level = is_type(state, output_type);
337341
state.pop_local();
338342

339-
match (param_level, body_level) {
340-
(Some(param_level), Some(body_level)) => {
341-
Arc::new(Value::Universe(std::cmp::max(param_level, body_level)))
343+
match (input_level, output_level) {
344+
(Some(input_level), Some(output_level)) => {
345+
Arc::new(Value::Universe(std::cmp::max(input_level, output_level)))
342346
}
343347
(_, _) => Arc::new(Value::Error),
344348
}
@@ -349,17 +353,17 @@ pub fn synth_type(state: &mut State<'_>, term: &Term) -> Arc<Value> {
349353
});
350354
Arc::new(Value::Error)
351355
}
352-
Term::FunctionElim(head, argument) => {
353-
let head_type = synth_type(state, head);
356+
Term::FunctionElim(head_term, input_term) => {
357+
let head_type = synth_type(state, head_term);
354358
match head_type.force(state.globals) {
355-
Value::FunctionType(_, param_type, body_closure) => {
356-
check_type(state, argument, &param_type);
357-
let argument_value = state.eval_term(argument);
358-
body_closure.elim(state.globals, argument_value)
359+
Value::FunctionType(_, input_type, output_closure) => {
360+
check_type(state, input_term, &input_type);
361+
let input_value = state.eval_term(input_term);
362+
output_closure.elim(state.globals, input_value)
359363
}
360364
Value::Error => Arc::new(Value::Error),
361365
_ => {
362-
state.report(Message::TooManyArguments { head_type });
366+
state.report(Message::TooManyInputsInFunctionElim { head_type });
363367
Arc::new(Value::Error)
364368
}
365369
}

pikelet/src/lang/core/typing/reporting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub enum Message {
2020
expected_label: String,
2121
head_type: Arc<Value>,
2222
},
23-
TooManyParameters,
24-
TooManyArguments {
23+
TooManyInputsInFunctionTerm,
24+
TooManyInputsInFunctionElim {
2525
head_type: Arc<Value>,
2626
},
2727
MismatchedSequenceLength {

pikelet/src/lang/surface.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub enum Literal<S> {
2626
pub type TypeEntry<S> = (Range<usize>, S, Option<S>, Term<S>);
2727
/// Entry in a [record term](Term::RecordTerm).
2828
pub type TermEntry<S> = (Range<usize>, S, Term<S>);
29-
/// A group of function parameters that are elements of the same type.
30-
pub type ParameterGroup<S> = (Vec<(Range<usize>, S)>, Term<S>);
29+
/// A group of function inputs that are elements of the same type.
30+
pub type InputGroup<S> = (Vec<(Range<usize>, S)>, Term<S>);
3131

3232
/// Terms in the surface language.
3333
#[derive(Debug, Clone)]
@@ -51,7 +51,7 @@ pub enum Term<S> {
5151
/// Function types.
5252
///
5353
/// Also known as: pi type, dependent product type.
54-
FunctionType(RangeFrom<usize>, Vec<ParameterGroup<S>>, Box<Term<S>>),
54+
FunctionType(RangeFrom<usize>, Vec<InputGroup<S>>, Box<Term<S>>),
5555
/// Arrow function types.
5656
///
5757
/// Also known as: non-dependent function type.
@@ -94,14 +94,14 @@ impl<T> Term<T> {
9494
| Term::Error(range) => range.clone(),
9595
Term::Ann(term, r#type) => term.range().start..r#type.range().end,
9696
Term::RecordElim(term, label_range, _) => term.range().start..label_range.end,
97-
Term::FunctionType(range, _, body_type) => range.start..body_type.range().end,
98-
Term::FunctionArrowType(param_type, body_type) => {
99-
param_type.range().start..body_type.range().end
97+
Term::FunctionType(range, _, output_type) => range.start..output_type.range().end,
98+
Term::FunctionArrowType(input_type, output_type) => {
99+
input_type.range().start..output_type.range().end
100100
}
101-
Term::FunctionTerm(range, _, body) => range.start..body.range().end,
102-
Term::FunctionElim(head, arguments) => match arguments.last() {
103-
Some(argument) => head.range().start..argument.range().end,
104-
None => head.range(),
101+
Term::FunctionTerm(range, _, output_term) => range.start..output_term.range().end,
102+
Term::FunctionElim(head_term, input_terms) => match input_terms.last() {
103+
Some(input_term) => head_term.range().start..input_term.range().end,
104+
None => head_term.range(),
105105
},
106106
}
107107
}

0 commit comments

Comments
 (0)