Skip to content

Commit 73d3f5a

Browse files
committed
Avoid unnecessary quotation in elaboration
This reduces some bloat in the elaboration output
1 parent 5932033 commit 73d3f5a

File tree

20 files changed

+171
-234
lines changed

20 files changed

+171
-234
lines changed

fathom/src/surface/elaboration.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,10 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
645645
for item in elab_order.iter().copied().map(|i| &surface_module.items[i]) {
646646
match item {
647647
Item::Def(item) => {
648-
let (expr, type_value) =
648+
let (expr, r#type) =
649649
self.synth_fun_lit(item.range, item.params, item.expr, item.r#type);
650650
let expr_value = self.eval_env().eval(&expr);
651-
let r#type = self.quote_env().quote(self.scope, &type_value);
651+
let type_value = self.eval_env().eval(&r#type);
652652

653653
self.item_env
654654
.push_definition(item.label.1, type_value, expr_value);
@@ -887,13 +887,17 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
887887
&mut self,
888888
pattern: &Pattern<ByteRange>,
889889
r#type: Option<&Term<'_, ByteRange>>,
890-
) -> (CheckedPattern, ArcValue<'arena>) {
890+
) -> (CheckedPattern, core::Term<'arena>, ArcValue<'arena>) {
891891
match r#type {
892-
None => self.synth_pattern(pattern),
892+
None => {
893+
let (pattern, type_value) = self.synth_pattern(pattern);
894+
let r#type = self.quote_env().quote(self.scope, &type_value);
895+
(pattern, r#type, type_value)
896+
}
893897
Some(r#type) => {
894898
let r#type = self.check(r#type, &self.universe.clone());
895899
let type_value = self.eval_env().eval(&r#type);
896-
(self.check_pattern(pattern, &type_value), type_value)
900+
(self.check_pattern(pattern, &type_value), r#type, type_value)
897901
}
898902
}
899903
}
@@ -958,9 +962,8 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
958962

959963
Vec::from_iter(params.iter().map(|param| {
960964
let range = param.pattern.range();
961-
let (pattern, type_value) =
965+
let (pattern, r#type, type_value) =
962966
self.synth_ann_pattern(&param.pattern, param.r#type.as_ref());
963-
let r#type = self.quote_env().quote(self.scope, &type_value);
964967
let (name, _) = self.push_local_param(pattern, type_value);
965968

966969
(range, param.plicity, name, r#type)
@@ -980,8 +983,8 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
980983

981984
match (surface_term, expected_type.as_ref()) {
982985
(Term::Let(_, def_pattern, def_type, def_expr, body_expr), _) => {
983-
let (def_pattern, def_type_value) = self.synth_ann_pattern(def_pattern, *def_type);
984-
let def_type = self.quote_env().quote(self.scope, &def_type_value);
986+
let (def_pattern, def_type, def_type_value) =
987+
self.synth_ann_pattern(def_pattern, *def_type);
985988
let def_expr = self.check(def_expr, &def_type_value);
986989
let def_expr_value = self.eval_env().eval(&def_expr);
987990

@@ -1376,8 +1379,8 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
13761379
(ann_expr, type_value)
13771380
}
13781381
Term::Let(_, def_pattern, def_type, def_expr, body_expr) => {
1379-
let (def_pattern, def_type_value) = self.synth_ann_pattern(def_pattern, *def_type);
1380-
let def_type = self.quote_env().quote(self.scope, &def_type_value);
1382+
let (def_pattern, def_type, def_type_value) =
1383+
self.synth_ann_pattern(def_pattern, *def_type);
13811384
let def_expr = self.check(def_expr, &def_type_value);
13821385
let def_expr_value = self.eval_env().eval(&def_expr);
13831386

@@ -1472,7 +1475,8 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
14721475
(fun_type, self.universe.clone())
14731476
}
14741477
Term::FunLiteral(range, params, body_expr) => {
1475-
self.synth_fun_lit(*range, params, body_expr, None)
1478+
let (expr, r#type) = self.synth_fun_lit(*range, params, body_expr, None);
1479+
(expr, self.eval_env().eval(&r#type))
14761480
}
14771481
Term::App(range, head_expr, args) => {
14781482
let mut head_range = head_expr.range();
@@ -1775,7 +1779,8 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
17751779
Value::Stuck(Head::MetaVar(_), _) => {
17761780
let range = ByteRange::merge(param.pattern.range(), body_expr.range());
17771781
let (expr, r#type) = self.synth_fun_lit(range, params, body_expr, None);
1778-
self.convert(range, expr, &r#type, expected_type)
1782+
let type_value = self.eval_env().eval(&r#type);
1783+
self.convert(range, expr, &type_value, expected_type)
17791784
}
17801785
Value::Stuck(Head::Prim(Prim::ReportedError), _) => {
17811786
core::Term::Prim(file_range.into(), Prim::ReportedError)
@@ -1801,7 +1806,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
18011806
params: &[Param<'_, ByteRange>],
18021807
body_expr: &Term<'_, ByteRange>,
18031808
body_type: Option<&Term<'_, ByteRange>>,
1804-
) -> (core::Term<'arena>, ArcValue<'arena>) {
1809+
) -> (core::Term<'arena>, core::Term<'arena>) {
18051810
self.local_env.reserve(params.len());
18061811
let initial_local_len = self.local_env.len();
18071812

@@ -1843,7 +1848,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
18431848
);
18441849
}
18451850

1846-
(fun_lit, self.eval_env().eval(&fun_type))
1851+
(fun_lit, fun_type)
18471852
}
18481853

18491854
fn synth_bin_op(

formats/opentype.snap

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@ def link_table : Pos -> {
3030
} -> Format -> Format =
3131
fun file_start table_record table_format => link (file_start + table_record.offset) (limit32 table_record.length table_format);
3232
def platform_id : Format = u16be;
33-
def encoding_id : U16 -> Format = fun platform => u16be;
33+
def encoding_id : Repr platform_id -> Format = fun platform => u16be;
3434
def empty : Format = ();
3535
def offset32 : Pos -> Format -> Format = fun base format => {
3636
offset <- u32be,
3737
link <- match offset { 0 => empty, _ => link (base + offset) format },
3838
};
3939
def language_id : Format = u16be;
40-
def cmap_language_id : U16 -> Format = fun platform => language_id;
40+
def cmap_language_id : Repr platform_id -> Format = fun platform => language_id;
4141
def small_glyph_id : Format = u8;
42-
def cmap_subtable_format0 : U16 -> Format = fun platform => {
42+
def cmap_subtable_format0 : Repr platform_id -> Format = fun platform => {
4343
length <- u16be,
4444
language <- cmap_language_id platform,
4545
glyph_id_array <- array16 256 small_glyph_id,
4646
};
47-
def cmap_subtable_format2 : U16 -> Format = fun platform => {
47+
def cmap_subtable_format2 : Repr platform_id -> Format = fun platform => {
4848
length <- u16be,
4949
language <- cmap_language_id platform,
5050
sub_header_keys <- array16 256 u16be,
5151
};
5252
def reserved : fun (format : Format) -> Repr format -> Format =
5353
fun format default => format;
54-
def cmap_subtable_format4 : U16 -> Format = fun platform => {
54+
def cmap_subtable_format4 : Repr platform_id -> Format = fun platform => {
5555
length <- u16be,
5656
language <- cmap_language_id platform,
5757
seg_count_x2 <- u16be,
@@ -65,45 +65,46 @@ def cmap_subtable_format4 : U16 -> Format = fun platform => {
6565
id_delta <- array16 seg_count s16be,
6666
id_range_offsets <- array16 seg_count u16be,
6767
};
68-
def cmap_subtable_format6 : U16 -> Format = fun platform => {
68+
def cmap_subtable_format6 : Repr platform_id -> Format = fun platform => {
6969
length <- u16be,
7070
language <- cmap_language_id platform,
7171
first_code <- u16be,
7272
entry_count <- u16be,
7373
glyph_id_array <- array16 entry_count u16be,
7474
};
7575
def language_id32 : Format = u32be;
76-
def cmap_language_id32 : U16 -> Format = fun platform => language_id32;
76+
def cmap_language_id32 : Repr platform_id -> Format =
77+
fun platform => language_id32;
7778
def sequential_map_group : Format = {
7879
start_char_code <- u32be,
7980
end_char_code <- u32be,
8081
start_glyph_id <- u32be,
8182
};
82-
def cmap_subtable_format8 : U16 -> Format = fun platform => {
83+
def cmap_subtable_format8 : Repr platform_id -> Format = fun platform => {
8384
_reserved <- reserved u16be 0,
8485
length <- u32be,
8586
language <- cmap_language_id32 platform,
8687
is32 <- array16 8192 u8,
8788
num_groups <- u32be,
8889
groups <- array32 num_groups sequential_map_group,
8990
};
90-
def cmap_subtable_format10 : U16 -> Format = fun platform => {
91+
def cmap_subtable_format10 : Repr platform_id -> Format = fun platform => {
9192
_reserved <- reserved u16be 0,
9293
length <- u32be,
9394
language <- cmap_language_id32 platform,
9495
start_char_code <- u32be,
9596
num_chars <- u32be,
9697
glyph_id_array <- array32 num_chars u16be,
9798
};
98-
def cmap_subtable_format12 : U16 -> Format = fun platform => {
99+
def cmap_subtable_format12 : Repr platform_id -> Format = fun platform => {
99100
_reserved <- reserved u16be 0,
100101
length <- u32be,
101102
language <- cmap_language_id32 platform,
102103
num_groups <- u32be,
103104
groups <- array32 num_groups sequential_map_group,
104105
};
105106
def constant_map_group : Format = sequential_map_group;
106-
def cmap_subtable_format13 : U16 -> Format = fun platform => {
107+
def cmap_subtable_format13 : Repr platform_id -> Format = fun platform => {
107108
_reserved <- reserved u16be 0,
108109
length <- u32be,
109110
language <- cmap_language_id32 platform,
@@ -129,14 +130,14 @@ def variation_selector : Pos -> Format = fun table_start => {
129130
default_uvs_offset <- offset32 table_start default_uvs_table,
130131
non_default_uvs_offset <- offset32 table_start non_default_uvs_table,
131132
};
132-
def cmap_subtable_format14 : U16 -> Pos -> Format =
133+
def cmap_subtable_format14 : Repr platform_id -> Pos -> Format =
133134
fun platform table_start => {
134135
length <- u32be,
135136
num_var_selector_records <- u32be,
136137
var_selector <- array32 num_var_selector_records (variation_selector table_start),
137138
};
138139
def unknown_table : Format = ();
139-
def cmap_subtable : U16 -> Format = fun platform => {
140+
def cmap_subtable : Repr platform_id -> Format = fun platform => {
140141
table_start <- stream_pos,
141142
format <- u16be,
142143
data <- match format {

tests/succeed/equality.snap

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,40 @@ stdout = '''
22
let id : fun (A : Type) -> A -> A = fun A a => a;
33
let Eq : fun (A : Type) -> A -> A -> Type = fun A a0 a1 => fun (P : A ->
44
Type) -> P a0 -> P a1;
5-
let refl : fun (A : Type) (a : A) (P : A -> Type) -> P a -> P a =
6-
fun A a P => id (P a);
7-
let fun_eta_left : fun (f : Type -> Type) (P : (Type -> Type) -> Type) -> P f ->
8-
P (fun x => f x) = fun f => refl (Type -> Type) f;
9-
let fun_eta_right : fun (f : Type -> Type) (P : (Type -> Type) -> Type) ->
10-
P (fun x => f x) -> P f = fun f => refl (Type -> Type) f;
11-
let fun_eta_left : fun (f : Type -> Type -> Type) (P : (Type -> Type -> Type) ->
12-
Type) -> P f -> P (fun x => f x) = fun f => refl (Type -> Type -> Type) f;
13-
let fun_eta_right : fun (f : Type -> Type -> Type) (P : (Type -> Type -> Type)
14-
-> Type) -> P (fun x => f x) -> P f = fun f => refl (Type -> Type -> Type) f;
15-
let fun_eta_left : fun (f : Type -> Type -> Type) (P : (Type -> Type -> Type) ->
16-
Type) -> P f -> P (fun x y => f x y) = fun f => refl (Type -> Type -> Type) f;
17-
let fun_eta_right : fun (f : Type -> Type -> Type) (P : (Type -> Type -> Type)
18-
-> Type) -> P (fun x y => f x y) -> P f = fun f => refl (Type -> Type ->
5+
let refl : fun (A : Type) (a : A) -> Eq A a a = fun A a P => id (P a);
6+
let fun_eta_left : fun (f : Type -> Type) -> Eq (Type ->
7+
Type) f (fun x => f x) = fun f => refl (Type -> Type) f;
8+
let fun_eta_right : fun (f : Type -> Type) -> Eq (Type ->
9+
Type) (fun x => f x) f = fun f => refl (Type -> Type) f;
10+
let fun_eta_left : fun (f : Type -> Type -> Type) -> Eq (Type -> Type ->
11+
Type) f (fun x => f x) = fun f => refl (Type -> Type -> Type) f;
12+
let fun_eta_right : fun (f : Type -> Type -> Type) -> Eq (Type -> Type ->
13+
Type) (fun x => f x) f = fun f => refl (Type -> Type -> Type) f;
14+
let fun_eta_left : fun (f : Type -> Type -> Type) -> Eq (Type -> Type ->
15+
Type) f (fun x y => f x y) = fun f => refl (Type -> Type -> Type) f;
16+
let fun_eta_right : fun (f : Type -> Type -> Type) -> Eq (Type -> Type ->
17+
Type) (fun x y => f x y) f = fun f => refl (Type -> Type -> Type) f;
18+
let fun_eta_left : fun (f : Type -> Type -> Type) -> Eq (Type -> Type ->
19+
Type) (fun x => f x) (fun x y => f x y) = fun f => refl (Type -> Type ->
1920
Type) f;
20-
let fun_eta_left : fun (f : Type -> Type -> Type) (P : (Type -> Type -> Type) ->
21-
Type) -> P (fun x => f x) -> P (fun x y => f x y) = fun f => refl (Type -> Type
22-
-> Type) f;
23-
let fun_eta_right : fun (f : Type -> Type -> Type) (P : (Type -> Type -> Type)
24-
-> Type) -> P (fun x y => f x y) -> P (fun x => f x) = fun f => refl (Type ->
25-
Type -> Type) f;
26-
let record_eta_left : fun (r : { x : Type, y : Type }) (P : {
27-
x : Type,
28-
y : Type,
29-
} -> Type) -> P r -> P { x = r.x, y = r.y } = fun r => refl {
30-
x : Type,
31-
y : Type,
32-
} r;
33-
let record_eta_right : fun (r : { x : Type, y : Type }) (P : {
21+
let fun_eta_right : fun (f : Type -> Type -> Type) -> Eq (Type -> Type ->
22+
Type) (fun x y => f x y) (fun x => f x) = fun f => refl (Type -> Type ->
23+
Type) f;
24+
let record_eta_left : fun (r : { x : Type, y : Type }) -> Eq {
3425
x : Type,
3526
y : Type,
36-
} -> Type) -> P { x = r.x, y = r.y } -> P r = fun r => refl {
27+
} r { x = r.x, y = r.y } = fun r => refl { x : Type, y : Type } r;
28+
let record_eta_right : fun (r : { x : Type, y : Type }) -> Eq {
3729
x : Type,
3830
y : Type,
39-
} r;
40-
let four_chars : fun (P : U32 -> Type) -> P "beng" -> P 1650814567 =
41-
refl U32 "beng";
42-
let three_chars : fun (P : U32 -> Type) -> P "BEN " -> P 1111838240 =
43-
refl U32 "BEN ";
31+
} { x = r.x, y = r.y } r = fun r => refl { x : Type, y : Type } r;
32+
let four_chars : Eq U32 "beng" 1650814567 = refl U32 "beng";
33+
let three_chars : Eq U32 "BEN " 1111838240 = refl U32 "BEN ";
4434
let foo : U32 -> U32 = fun x => match x { 1 => 0, x => x };
45-
let eq_foo : fun (P : (U32 -> U32) -> Type) -> P (fun x => match x {
35+
let eq_foo : Eq (U32 -> U32) foo foo = refl (U32 -> U32) (fun a => match a {
4636
1 => 0,
4737
x => x,
48-
}) -> P (fun x => match x { 1 => 0, x => x }) = refl (U32 ->
49-
U32) (fun a => match a { 1 => 0, x => x });
38+
});
5039
Type : Type
5140
'''
5241
stderr = ''

tests/succeed/format-cond/simple.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ stdout = '''
22
let format : Format = {
33
sfnt_version <- { version <- u32be | version == (0xffff : U32) },
44
};
5-
let _ : { sfnt_version : U32 } -> { sfnt_version : U32 } = fun x => x;
5+
let _ : Repr format -> { sfnt_version : Repr u32be } = fun x => x;
66
() : ()
77
'''
88
stderr = ''

tests/succeed/format-overlap/dependent.snap

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ let silly : Format = overlap {
88
record0 <- record0,
99
record1 <- record1 record0.length,
1010
};
11-
let _ : {
12-
record0 : { length : U8 },
13-
record1 : { _length : U8, data : Array8 record0.length U8 },
14-
} -> {
11+
let _ : Repr silly -> {
1512
record0 : { length : U8 },
1613
record1 : { _length : U8, data : Array8 record0.length U8 },
1714
} = fun silly => silly;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
stdout = '''
22
let number : Format = overlap { u <- u32be where u >= (2 : U32), s <- s32be };
3-
let _ : { u : U32, s : S32 } -> { u : U32, s : S32 } = fun n => n;
3+
let _ : Repr number -> { u : U32, s : S32 } = fun n => n;
44
() : ()
55
'''
66
stderr = ''
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
stdout = '''
22
let number : Format = overlap { u <- u32be, s <- s32be };
3-
let _ : { u : U32, s : S32 } -> { u : U32, s : S32 } = fun n => n;
3+
let _ : Repr number -> { u : U32, s : S32 } = fun n => n;
44
() : ()
55
'''
66
stderr = ''

tests/succeed/format-record/computed-fields.snap

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@ let format : Format = {
55
start_code <- array16 seg_count u16be,
66
id_delta <- array16 seg_count s16be,
77
};
8-
let _ : {
9-
seg_count_x2 : U16,
10-
seg_count : U16,
11-
start_code : Array16 seg_count U16,
12-
id_delta : Array16 seg_count S16,
13-
} -> {
8+
let _ : Repr format -> {
149
seg_count_x2 : U16,
1510
seg_count : U16,
1611
start_code : Array16 seg_count U16,
1712
id_delta : Array16 seg_count S16,
1813
} = fun x => x;
1914
let format : Format = { let x : U32 = 256 };
20-
let _ : { x : U32 } -> { x : U32 } = fun x => x;
15+
let _ : Repr format -> { x : U32 } = fun x => x;
2116
() : ()
2217
'''
2318
stderr = ''

tests/succeed/format-record/field-refinements.snap

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ let format : Format = {
44
len <- u8 where len <= (16 : U8),
55
data <- array8 len u8,
66
};
7-
let _ : { magic : U64, len : U8, data : Array8 len U8 } -> {
8-
magic : U64,
9-
len : U8,
10-
data : Array8 len U8,
11-
} = fun x => x;
7+
let _ : Repr format -> { magic : U64, len : U8, data : Array8 len U8 } =
8+
fun x => x;
129
() : ()
1310
'''
1411
stderr = ''
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
stdout = '''
22
let array32 : U32 -> Format -> Format = fun len Elem => Elem;
33
let pair : Format = { len <- u32be, data <- array32 len u32be };
4-
let test_pair : { len : U32, data : U32 } -> { len : U32, data : U32 } =
5-
fun p => p;
4+
let test_pair : Repr pair -> { len : U32, data : U32 } = fun p => p;
65
pair : Format
76
'''
87
stderr = ''

0 commit comments

Comments
 (0)