Skip to content

Commit f260440

Browse files
committed
House-cleaning of AST variants
This churns the history pretty badly, but I think it’s important. We group together common variants in a consistent order in their definitions and usages in the compiler passes. I’ve also tried to order from weaker to stronger claims, and so have put functions above records in the ASTs.
1 parent c54b68f commit f260440

File tree

9 files changed

+747
-668
lines changed

9 files changed

+747
-668
lines changed

pikelet/src/lang/core.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,19 @@ impl From<u32> for UniverseOffset {
6767
/// Terms in the core language.
6868
#[derive(Clone, Debug, PartialEq)]
6969
pub enum Term {
70-
/// The type of types.
71-
Universe(UniverseLevel),
7270
/// Global variables.
7371
Global(String),
7472
/// Local variables.
7573
Local(LocalIndex),
76-
/// Constants.
77-
Constant(Constant),
78-
/// Ordered sequences.
79-
Sequence(Vec<Arc<Term>>),
74+
8075
/// Annotated terms
8176
Ann(Arc<Term>, Arc<Term>),
82-
/// Record types.
83-
RecordType(Arc<[(String, Arc<Term>)]>),
84-
/// Record terms.
85-
RecordTerm(BTreeMap<String, Arc<Term>>),
86-
/// Record eliminations.
87-
///
88-
/// Also known as: record projection, field lookup.
89-
RecordElim(Arc<Term>, String),
77+
78+
/// The type of types.
79+
Universe(UniverseLevel),
80+
/// Lift a term by the given number of universe levels.
81+
Lift(Arc<Term>, UniverseOffset),
82+
9083
/// Function types.
9184
///
9285
/// Also known as: pi type, dependent product type.
@@ -99,8 +92,22 @@ pub enum Term {
9992
///
10093
/// Also known as: function application.
10194
FunctionElim(Arc<Term>, Arc<Term>),
102-
/// Lift a term by the given number of universe levels.
103-
Lift(Arc<Term>, UniverseOffset),
95+
96+
/// Record types.
97+
RecordType(Arc<[(String, Arc<Term>)]>),
98+
/// Record terms.
99+
RecordTerm(BTreeMap<String, Arc<Term>>),
100+
/// Record eliminations.
101+
///
102+
/// Also known as: record projection, field lookup.
103+
RecordElim(Arc<Term>, String),
104+
105+
/// Ordered sequences.
106+
Sequence(Vec<Arc<Term>>),
107+
108+
/// Constants.
109+
Constant(Constant),
110+
104111
/// Error sentinel.
105112
Error,
106113
}

pikelet/src/lang/core/semantics.rs

Lines changed: 117 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ pub enum Value {
4242

4343
/// The type of types.
4444
Universe(UniverseLevel),
45-
/// Constants.
46-
Constant(Constant),
47-
/// Ordered sequences.
48-
Sequence(Vec<Arc<Value>>),
49-
/// Record types.
50-
RecordType(RecordTypeClosure),
51-
/// Record terms.
52-
RecordTerm(BTreeMap<String, Arc<Value>>),
45+
5346
/// Function types.
5447
///
5548
/// Also known as: pi type, dependent product type.
@@ -59,6 +52,17 @@ pub enum Value {
5952
/// Also known as: lambda abstraction, anonymous function.
6053
FunctionTerm(String, Closure),
6154

55+
/// Record types.
56+
RecordType(RecordTypeClosure),
57+
/// Record terms.
58+
RecordTerm(BTreeMap<String, Arc<Value>>),
59+
60+
/// Ordered sequences.
61+
Sequence(Vec<Arc<Value>>),
62+
63+
/// Constants.
64+
Constant(Constant),
65+
6266
/// Error sentinel.
6367
Error,
6468
}
@@ -100,14 +104,14 @@ pub enum Head {
100104
/// An eliminator, to be used in the spine of an elimination.
101105
#[derive(Clone, Debug)]
102106
pub enum Elim {
103-
/// Record eliminators.
104-
///
105-
/// Also known as: record projections, field lookup.
106-
Record(String),
107107
/// Function eliminators.
108108
///
109109
/// Also known as: function application.
110110
Function(Arc<LazyValue>),
111+
/// Record eliminators.
112+
///
113+
/// Also known as: record projections, field lookup.
114+
Record(String),
111115
}
112116

113117
/// Closure, capturing the current universe offset and the current locals in scope.
@@ -259,9 +263,6 @@ pub fn eval_term(
259263
term: &Term,
260264
) -> Arc<Value> {
261265
match term {
262-
Term::Universe(level) => Arc::new(Value::universe(
263-
(*level + universe_offset).unwrap(), // FIXME: Handle overflow
264-
)),
265266
Term::Global(name) => {
266267
let head = Head::Global(name.into(), universe_offset);
267268
match globals.get(name) {
@@ -282,16 +283,17 @@ pub fn eval_term(
282283
None => Arc::new(Value::Stuck(head, Vec::new())),
283284
}
284285
}
285-
Term::Constant(constant) => Arc::new(Value::Constant(constant.clone())),
286-
Term::Sequence(term_entries) => {
287-
let value_entries = term_entries
288-
.iter()
289-
.map(|entry_term| eval_term(globals, universe_offset, values, entry_term))
290-
.collect();
291286

292-
Arc::new(Value::Sequence(value_entries))
293-
}
294287
Term::Ann(term, _) => eval_term(globals, universe_offset, values, term),
288+
289+
Term::Universe(level) => Arc::new(Value::universe(
290+
(*level + universe_offset).unwrap(), // FIXME: Handle overflow
291+
)),
292+
Term::Lift(term, offset) => {
293+
let universe_offset = (universe_offset + *offset).unwrap(); // FIXME: Handle overflow
294+
eval_term(globals, universe_offset, values, term)
295+
}
296+
295297
Term::RecordType(type_entries) => Arc::new(Value::RecordType(RecordTypeClosure::new(
296298
universe_offset,
297299
values.clone(),
@@ -312,6 +314,7 @@ pub fn eval_term(
312314
let head = eval_term(globals, universe_offset, values, head);
313315
apply_record_elim(&head, label)
314316
}
317+
315318
Term::FunctionType(input_name_hint, input_type, output_type) => {
316319
Arc::new(Value::FunctionType(
317320
input_name_hint.clone(),
@@ -328,10 +331,18 @@ pub fn eval_term(
328331
let input = LazyValue::eval_term(universe_offset, values.clone(), input.clone());
329332
apply_function_elim(globals, &head, Arc::new(input))
330333
}
331-
Term::Lift(term, offset) => {
332-
let universe_offset = (universe_offset + *offset).unwrap(); // FIXME: Handle overflow
333-
eval_term(globals, universe_offset, values, term)
334+
335+
Term::Sequence(term_entries) => {
336+
let value_entries = term_entries
337+
.iter()
338+
.map(|entry_term| eval_term(globals, universe_offset, values, entry_term))
339+
.collect();
340+
341+
Arc::new(Value::Sequence(value_entries))
334342
}
343+
344+
Term::Constant(constant) => Arc::new(Value::Constant(constant.clone())),
345+
335346
Term::Error => Arc::new(Value::Error),
336347
}
337348
}
@@ -425,11 +436,11 @@ fn read_back_spine(
425436
};
426437

427438
spine.iter().fold(head, |head, elim| match elim {
428-
Elim::Record(label) => Term::RecordElim(Arc::new(head), label.clone()),
429439
Elim::Function(input) => {
430440
let input = read_back_value(globals, local_size, unfold, input.force(globals));
431441
Term::FunctionElim(Arc::new(head), Arc::new(input))
432442
}
443+
Elim::Record(label) => Term::RecordElim(Arc::new(head), label.clone()),
433444
})
434445
}
435446

@@ -448,17 +459,25 @@ pub fn read_back_value(
448459
},
449460

450461
Value::Universe(level) => Term::Universe(*level),
451-
Value::Constant(constant) => Term::Constant(constant.clone()),
452-
Value::Sequence(value_entries) => {
453-
let term_entries = value_entries
454-
.iter()
455-
.map(|value_entry| {
456-
Arc::new(read_back_value(globals, local_size, unfold, value_entry))
457-
})
458-
.collect();
459462

460-
Term::Sequence(term_entries)
463+
Value::FunctionType(input_name_hint, input_type, output_closure) => {
464+
let local = Arc::new(Value::local(local_size.next_level()));
465+
let input_type = Arc::new(read_back_value(globals, local_size, unfold, input_type));
466+
let output_type = output_closure.elim(globals, local);
467+
let output_type =
468+
read_back_value(globals, local_size.increment(), unfold, &output_type);
469+
470+
Term::FunctionType(input_name_hint.clone(), input_type, Arc::new(output_type))
461471
}
472+
Value::FunctionTerm(input_name_hint, output_closure) => {
473+
let local = Arc::new(Value::local(local_size.next_level()));
474+
let output_term = output_closure.elim(globals, local);
475+
let output_term =
476+
read_back_value(globals, local_size.increment(), unfold, &output_term);
477+
478+
Term::FunctionTerm(input_name_hint.clone(), Arc::new(output_term))
479+
}
480+
462481
Value::RecordType(closure) => {
463482
let mut local_size = local_size;
464483
let mut type_entries = Vec::with_capacity(closure.entries.len());
@@ -486,24 +505,20 @@ pub fn read_back_value(
486505

487506
Term::RecordTerm(term_entries)
488507
}
489-
Value::FunctionType(input_name_hint, input_type, output_closure) => {
490-
let local = Arc::new(Value::local(local_size.next_level()));
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);
495508

496-
Term::FunctionType(input_name_hint.clone(), input_type, Arc::new(output_type))
497-
}
498-
Value::FunctionTerm(input_name_hint, output_closure) => {
499-
let local = Arc::new(Value::local(local_size.next_level()));
500-
let output_term = output_closure.elim(globals, local);
501-
let output_term =
502-
read_back_value(globals, local_size.increment(), unfold, &output_term);
509+
Value::Sequence(value_entries) => {
510+
let term_entries = value_entries
511+
.iter()
512+
.map(|value_entry| {
513+
Arc::new(read_back_value(globals, local_size, unfold, value_entry))
514+
})
515+
.collect();
503516

504-
Term::FunctionTerm(input_name_hint.clone(), Arc::new(output_term))
517+
Term::Sequence(term_entries)
505518
}
506519

520+
Value::Constant(constant) => Term::Constant(constant.clone()),
521+
507522
Value::Error => Term::Error,
508523
}
509524
}
@@ -561,18 +576,33 @@ fn is_equal(globals: &Globals, local_size: LocalSize, value0: &Value, value1: &V
561576
}
562577

563578
(Value::Universe(level0), Value::Universe(level1)) => level0 == level1,
564-
(Value::Constant(constant0), Value::Constant(constant1)) => constant0 == constant1,
565-
(Value::Sequence(value_entries0), Value::Sequence(value_entries1)) => {
566-
if value_entries0.len() != value_entries1.len() {
579+
580+
(
581+
Value::FunctionType(_, input_type0, output_closure0),
582+
Value::FunctionType(_, input_type1, output_closure1),
583+
) => {
584+
if !is_equal(globals, local_size, input_type1, input_type0) {
567585
return false;
568586
}
569587

570-
Iterator::zip(value_entries0.iter(), value_entries1.iter()).all(
571-
|(value_entry0, value_entry1)| {
572-
is_equal(globals, local_size, value_entry0, value_entry1)
573-
},
588+
let local = Arc::new(Value::local(local_size.next_level()));
589+
is_equal(
590+
globals,
591+
local_size.increment(),
592+
&output_closure0.elim(globals, local.clone()),
593+
&output_closure1.elim(globals, local),
574594
)
575595
}
596+
(Value::FunctionTerm(_, output_closure0), Value::FunctionTerm(_, output_closure1)) => {
597+
let local = Arc::new(Value::local(local_size.next_level()));
598+
is_equal(
599+
globals,
600+
local_size.increment(),
601+
&output_closure0.elim(globals, local.clone()),
602+
&output_closure1.elim(globals, local),
603+
)
604+
}
605+
576606
(Value::RecordType(closure0), Value::RecordType(closure1)) => {
577607
if closure0.entries.len() != closure1.entries.len() {
578608
return false;
@@ -617,32 +647,21 @@ fn is_equal(globals: &Globals, local_size: LocalSize, value0: &Value, value1: &V
617647
},
618648
)
619649
}
620-
(
621-
Value::FunctionType(_, input_type0, output_closure0),
622-
Value::FunctionType(_, input_type1, output_closure1),
623-
) => {
624-
if !is_equal(globals, local_size, input_type1, input_type0) {
650+
651+
(Value::Sequence(value_entries0), Value::Sequence(value_entries1)) => {
652+
if value_entries0.len() != value_entries1.len() {
625653
return false;
626654
}
627655

628-
let local = Arc::new(Value::local(local_size.next_level()));
629-
is_equal(
630-
globals,
631-
local_size.increment(),
632-
&output_closure0.elim(globals, local.clone()),
633-
&output_closure1.elim(globals, local),
634-
)
635-
}
636-
(Value::FunctionTerm(_, output_closure0), Value::FunctionTerm(_, output_closure1)) => {
637-
let local = Arc::new(Value::local(local_size.next_level()));
638-
is_equal(
639-
globals,
640-
local_size.increment(),
641-
&output_closure0.elim(globals, local.clone()),
642-
&output_closure1.elim(globals, local),
656+
Iterator::zip(value_entries0.iter(), value_entries1.iter()).all(
657+
|(value_entry0, value_entry1)| {
658+
is_equal(globals, local_size, value_entry0, value_entry1)
659+
},
643660
)
644661
}
645662

663+
(Value::Constant(constant0), Value::Constant(constant1)) => constant0 == constant1,
664+
646665
// Errors are always treated as subtypes, regardless of what they are compared with.
647666
(Value::Error, _) | (_, Value::Error) => true,
648667
// Anything else is not equal!
@@ -679,6 +698,27 @@ pub fn is_subtype(
679698
}
680699

681700
(Value::Universe(level0), Value::Universe(level1)) => level0 <= level1,
701+
702+
(
703+
Value::FunctionType(_, input_type0, output_closure0),
704+
Value::FunctionType(_, input_type1, output_closure1),
705+
) => {
706+
if !is_subtype(globals, local_size, input_type1, input_type0) {
707+
return false;
708+
}
709+
710+
let local = Arc::new(Value::local(local_size.next_level()));
711+
let output_term0 = output_closure0.elim(globals, local.clone());
712+
let output_term1 = output_closure1.elim(globals, local);
713+
714+
is_subtype(
715+
globals,
716+
local_size.increment(),
717+
&output_term0,
718+
&output_term1,
719+
)
720+
}
721+
682722
(Value::RecordType(closure0), Value::RecordType(closure1)) => {
683723
if closure0.entries.len() != closure1.entries.len() {
684724
return false;
@@ -712,25 +752,6 @@ pub fn is_subtype(
712752

713753
true
714754
}
715-
(
716-
Value::FunctionType(_, input_type0, output_closure0),
717-
Value::FunctionType(_, input_type1, output_closure1),
718-
) => {
719-
if !is_subtype(globals, local_size, input_type1, input_type0) {
720-
return false;
721-
}
722-
723-
let local = Arc::new(Value::local(local_size.next_level()));
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-
)
733-
}
734755

735756
// Errors are always treated as subtypes, regardless of what they are compared with.
736757
(Value::Error, _) | (_, Value::Error) => true,

0 commit comments

Comments
 (0)