Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add record constructor to subtyping #2007

Merged
merged 24 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 8 additions & 98 deletions core/src/typecheck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub mod reporting;
#[macro_use]
pub mod mk_uniftype;
pub mod eq;
pub mod subtyping;
pub mod unif;

use eq::{SimpleTermEnvironment, TermEnvironment};
Expand All @@ -90,6 +91,8 @@ use operation::{get_bop_type, get_nop_type, get_uop_type};
use pattern::{PatternTypeData, PatternTypes};
use unif::*;

use self::subtyping::SubsumedBy;

/// The max depth parameter used to limit the work performed when inferring the type of the stdlib.
const INFER_RECORD_MAX_DEPTH: u8 = 4;

Expand Down Expand Up @@ -2167,7 +2170,8 @@ fn check<V: TypecheckVisitor>(
let inferred = infer(state, ctxt.clone(), visitor, rt)?;

// We call to `subsumption` to perform the switch from infer mode to checking mode.
subsumption(state, ctxt, inferred, ty)
inferred
.subsumed_by(ty, state, &mut ctxt.clone())
.map_err(|err| err.into_typecheck_err(state, rt.pos))
}
Term::Enum(id) => {
Expand Down Expand Up @@ -2363,102 +2367,6 @@ fn check<V: TypecheckVisitor>(
}
}

/// Change from inference mode to checking mode, and apply a potential subsumption rule.
///
/// Currently, there is record/dictionary subtyping, if we are not in this case we fallback to perform
/// polymorphic type instantiation with unification variable on the left (on the inferred type),
/// and then simply performs unification (put differently, the subtyping relation when it is not
/// a record/dictionary subtyping is the equality
/// relation).
///
/// The type instantiation corresponds to the zero-ary case of application in the current
/// specification (which is based on [A Quick Look at Impredicativity][quick-look], although we
/// currently don't support impredicative polymorphism).
///
/// In the future, this function might implement a other non-trivial subsumption rule.
///
/// [quick-look]: https://www.microsoft.com/en-us/research/uploads/prod/2020/01/quick-look-icfp20-fixed.pdf
pub fn subsumption(
state: &mut State,
mut ctxt: Context,
inferred: UnifType,
checked: UnifType,
) -> Result<(), UnifError> {
let inferred_inst = instantiate_foralls(state, &mut ctxt, inferred, ForallInst::UnifVar);
let checked = checked.into_root(state.table);
match (inferred_inst, checked) {
(
UnifType::Concrete {
typ: TypeF::Record(rrows),
..
},
UnifType::Concrete {
typ:
TypeF::Dict {
type_fields,
flavour,
},
var_levels_data,
},
) => {
for row in rrows.iter() {
match row {
GenericUnifRecordRowsIteratorItem::Row(a) => {
subsumption(state, ctxt.clone(), a.typ.clone(), *type_fields.clone())?
}
GenericUnifRecordRowsIteratorItem::TailUnifVar { id, .. } =>
// We don't need to perform any variable level checks when unifying a free
// unification variable with a ground type
// We close the tail because there is no garanty that
// { a : Number, b : Number, _ : a?} <= { _ : Number}
{
state
.table
.assign_rrows(id, UnifRecordRows::concrete(RecordRowsF::Empty))
}
GenericUnifRecordRowsIteratorItem::TailConstant(id) => {
let checked = UnifType::Concrete {
typ: TypeF::Dict {
type_fields: type_fields.clone(),
flavour,
},
var_levels_data,
};
Err(UnifError::WithConst {
var_kind: VarKindDiscriminant::RecordRows,
expected_const_id: id,
inferred: checked,
})?
}
_ => (),
}
}
Ok(())
}
(
UnifType::Concrete {
typ: TypeF::Array(a),
..
},
UnifType::Concrete {
typ: TypeF::Array(b),
..
},
)
| (
UnifType::Concrete {
typ: TypeF::Dict { type_fields: a, .. },
..
},
UnifType::Concrete {
typ: TypeF::Dict { type_fields: b, .. },
..
},
) => subsumption(state, ctxt.clone(), *a, *b),
(inferred_inst, checked) => checked.unify(inferred_inst, state, &ctxt),
}
}

fn check_field<V: TypecheckVisitor>(
state: &mut State,
ctxt: Context,
Expand Down Expand Up @@ -2489,7 +2397,9 @@ fn check_field<V: TypecheckVisitor>(
field.value.as_ref(),
)?;

subsumption(state, ctxt, inferred, ty).map_err(|err| err.into_typecheck_err(state, pos))
inferred
.subsumed_by(ty, state, &mut ctxt.clone())
.map_err(|err| err.into_typecheck_err(state, pos))
}
}

Expand Down
244 changes: 244 additions & 0 deletions core/src/typecheck/subtyping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
//! Type subsumption
/// Type subsumption is generally used when we change from inference mode to checking mode.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! Type subsumption
/// Type subsumption is generally used when we change from inference mode to checking mode.
//! Type subsumption
//!
/// Type subsumption is generally used when we change from inference mode to checking mode.

/// Currently, there is one subtyping relations :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, and below, you should continue to use //! instead of ///, because it's part of the top-level module documentation.

/// - Record / Dictionary : `{a1 : T1,...,an : Tn} <: {_ : U}` if for every n `Tn <: U`
///
/// And we extend subtyping to type constructors :
/// - Array / Array : `Array T <: Array U` if `T <: U`
/// - Dictionary / Dictionary : `{_ : T} <: {_ : U}` if `T <: U`
/// - Record / Record : `{a1 : T1,...,an : Tn} <: {b1 : U1,...,bn : Un}` if for every n `Tn <: Un`
Eckaos marked this conversation as resolved.
Show resolved Hide resolved
///
/// When we are not in these cases, we fallback to perform polymorphic type instantiation
/// with unification variable on the left (on the inferred type), and
/// then simply performs unification (put differently, the subtyping
/// relation is the equality relation).
///
/// The type instantiation corresponds to the zero-ary case of application in the current
/// specification (which is based on [A Quick Look at Impredicativity][quick-look], although we
/// currently don't support impredicative polymorphism).
///
/// In the future, this function might implement a other non-trivial subsumption rule.
///
/// [quick-look]: https://www.microsoft.com/en-us/research/uploads/prod/2020/01/quick-look-icfp20-fixed.pdf
use super::*;

pub(super) trait SubsumedBy {
type Error;
fn subsumed_by(
self,
t2: Self,
state: &mut State,
ctxt: &mut Context,
) -> Result<(), Self::Error>;
}

impl SubsumedBy for UnifType {
type Error = UnifError;
fn subsumed_by(
self,
t2: Self,
state: &mut State,
ctxt: &mut Context,
) -> Result<(), Self::Error> {
let inferred = instantiate_foralls(state, ctxt, self, ForallInst::UnifVar);
let checked = t2.into_root(state.table);
match (inferred, checked) {
(
UnifType::Concrete {
typ: TypeF::Record(rrows),
..
},
UnifType::Concrete {
typ:
TypeF::Dict {
type_fields,
flavour,
},
var_levels_data,
},
) => {
for row in rrows.iter() {
match row {
GenericUnifRecordRowsIteratorItem::Row(a) => {
a.typ
.clone()
.subsumed_by(*type_fields.clone(), state, ctxt)?
}
GenericUnifRecordRowsIteratorItem::TailUnifVar { id, .. } =>
// We don't need to perform any variable level checks when unifying a free
// unification variable with a ground type
// We close the tail because there is no guarantee that
// { a : Number, b : Number, _ : a?} <= { _ : Number}
{
state
.table
.assign_rrows(id, UnifRecordRows::concrete(RecordRowsF::Empty))
}
GenericUnifRecordRowsIteratorItem::TailConstant(id) => {
let checked = UnifType::Concrete {
typ: TypeF::Dict {
type_fields: type_fields.clone(),
flavour,
},
var_levels_data,
};
Err(UnifError::WithConst {
var_kind: VarKindDiscriminant::RecordRows,
expected_const_id: id,
inferred: checked,
})?
}
_ => (),
}
}
Ok(())
}
(
UnifType::Concrete {
typ: TypeF::Array(a),
..
},
UnifType::Concrete {
typ: TypeF::Array(b),
..
},
)
| (
UnifType::Concrete {
typ: TypeF::Dict { type_fields: a, .. },
..
},
UnifType::Concrete {
typ: TypeF::Dict { type_fields: b, .. },
..
},
) => a.subsumed_by(*b, state, ctxt),
(
UnifType::Concrete {
typ: TypeF::Record(rrows1),
..
},
UnifType::Concrete {
typ: TypeF::Record(rrows2),
..
},
) => rrows1
.clone()
.subsumed_by(rrows2.clone(), state, ctxt)
.map_err(|err| err.into_unif_err(mk_uty_record!(;rrows2), mk_uty_record!(;rrows1))),
(inferred, checked) => checked.unify(inferred, state, ctxt),
}
}
}

impl SubsumedBy for UnifRecordRows {
type Error = RowUnifError;
fn subsumed_by(
self,
t2: Self,
state: &mut State,
ctxt: &mut Context,
) -> Result<(), Self::Error> {
let inferred = self.into_root(state.table);
let checked = t2.into_root(state.table);
match (inferred, checked) {
(
UnifRecordRows::Concrete { rrows: rrows1, .. },
UnifRecordRows::Concrete {
rrows: rrows2,
var_levels_data: levels2,
},
) => match (rrows1, rrows2) {
(RecordRowsF::Extend { row, tail }, rrows2 @ RecordRowsF::Extend { .. }) => {
let urrows2 = UnifRecordRows::Concrete {
rrows: rrows2,
var_levels_data: levels2,
};
let (ty_res, urrows_without_ty_res) = urrows2
.remove_row(&row.id, &row.typ, state, ctxt.var_level)
.map_err(|err| match err {
RemoveRowError::Missing => RowUnifError::MissingRow(row.id),
RemoveRowError::Conflict => {
RowUnifError::RecordRowConflict(row.clone())
}
})?;
if let RemoveRowResult::Extracted(ty) = ty_res {
row.typ.subsumed_by(ty, state, ctxt).map_err(|err| {
RowUnifError::RecordRowMismatch {
id: row.id,
cause: Box::new(err),
}
})?;
}
tail.subsumed_by(urrows_without_ty_res, state, ctxt)
}
(RecordRowsF::TailVar(id), _) | (_, RecordRowsF::TailVar(id)) => {
Err(RowUnifError::UnboundTypeVariable(id))
}
(RecordRowsF::Empty, RecordRowsF::Empty)
| (RecordRowsF::TailDyn, RecordRowsF::TailDyn) => Ok(()),
(RecordRowsF::Empty, RecordRowsF::TailDyn)
| (RecordRowsF::TailDyn, RecordRowsF::Empty) => Err(RowUnifError::ExtraDynTail),
(
RecordRowsF::Empty,
RecordRowsF::Extend {
row: UnifRecordRow { id, .. },
..
},
)
| (
RecordRowsF::TailDyn,
RecordRowsF::Extend {
row: UnifRecordRow { id, .. },
..
},
) => Err(RowUnifError::MissingRow(id)),
(
RecordRowsF::Extend {
row: UnifRecordRow { id, .. },
..
},
RecordRowsF::TailDyn,
)
| (
RecordRowsF::Extend {
row: UnifRecordRow { id, .. },
..
},
RecordRowsF::Empty,
) => Err(RowUnifError::ExtraRow(id)),
},
(UnifRecordRows::UnifVar { id, .. }, urrows)
| (urrows, UnifRecordRows::UnifVar { id, .. }) => {
if let UnifRecordRows::Constant(cst_id) = urrows {
let constant_level = state.table.get_rrows_level(cst_id);
state.table.force_rrows_updates(constant_level);
if state.table.get_rrows_level(id) < constant_level {
return Err(RowUnifError::VarLevelMismatch {
constant_id: cst_id,
var_kind: VarKindDiscriminant::RecordRows,
});
}
}
urrows.propagate_constrs(state.constr, id)?;
state.table.assign_rrows(id, urrows);
Ok(())
}
(UnifRecordRows::Constant(i1), UnifRecordRows::Constant(i2)) if i1 == i2 => Ok(()),
(UnifRecordRows::Constant(i1), UnifRecordRows::Constant(i2)) => {
Err(RowUnifError::ConstMismatch {
var_kind: VarKindDiscriminant::RecordRows,
expected_const_id: i2,
inferred_const_id: i1,
})
}
(urrows, UnifRecordRows::Constant(i)) | (UnifRecordRows::Constant(i), urrows) => {
Err(RowUnifError::WithConst {
var_kind: VarKindDiscriminant::RecordRows,
expected_const_id: i,
inferred: UnifType::concrete(TypeF::Record(urrows)),
})
}
}
}
}
Loading