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 Dimension type and unit module #524

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
print! "hello, ", end := ""
# TypeError: `.times!` is a method of `Nat` (0 or more Int), not `Int`

{Meter; Sec; meter; yard; sec} = import "unit"
{Meter; Sec; meter; kg; sec} = import "unit"

velocity x: Meter, t: Sec = x / t

v = velocity 3yard, 2sec # TypeError: the type of `x` was mismatched: expect `Meter`, found `Yard`
v = velocity 3kg, 2sec # TypeError: the type of `x` was mismatched: expect `Meter`, found `Kg`
v = velocity 3meter, 2sec # v == 1.5 m/s
```

Expand Down
4 changes: 2 additions & 2 deletions README_JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
print! "hello,", end := ""
# TypeError: `.times!`は`Nat`(0以上のInt)のメソッドです、`Int`ではありません

{Meter; Sec; meter; yard; sec} = import "unit"
{Meter; Sec; Kg; meter; sec} = import "unit"

velocity x: Meter, t: Sec = x / t

v = velocity 3yard, 2sec # TypeError: `x`の型が適合しません。`Meter`を予期しましたが、`Yard`が渡されました
v = velocity 3kg, 2sec # TypeError: `x`の型が適合しません。`Meter`を予期しましたが、`Kg`が渡されました
v = velocity 3meter, 2sec # v == 1.5 m/s
```

Expand Down
4 changes: 2 additions & 2 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
print! "hello, ", end := ""
# 类型错误: `.times!`是`Nat`(0或更大整数)的方法,不是`Int`的

{Meter; Sec; meter; yard; sec} = import "unit"
{Meter; Sec; meter; kg; sec} = import "unit"

velocity x: Meter, t: Sec = x / t

v = velocity 3yard, 2sec # 类型错误: `x`的类型不匹配: 预期为`Meter`,找到`Yard'`
v = velocity 3kg, 2sec # 类型错误: `x`的类型不匹配: 预期为`Meter`,找到`Kg`
v = velocity 3meter, 2sec # v == 1.5 m/s
```

Expand Down
4 changes: 2 additions & 2 deletions README_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
print! "hello, ", end := ""
# 類型錯誤: `.times!`是`Nat`(0或更大整數)的方法,不是`Int`的

{Meter; Sec; meter; yard; sec} = import "unit"
{Meter; Sec; meter; kg; sec} = import "unit"

velocity x: Meter, t: Sec = x / t

v = velocity 3yard, 2sec # 類型錯誤: `x`的類型不匹配: 預期為`Meter`,找到`Yard'`
v = velocity 3kg, 2sec # 類型錯誤: `x`的類型不匹配: 預期為`Meter`,找到`Kg`
v = velocity 3meter, 2sec # v == 1.5 m/s
```

Expand Down
2 changes: 1 addition & 1 deletion crates/els/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn comp_item_kind(t: &Type, muty: Mutability) -> CompletionItemKind {
CompletionItemKind::VARIABLE
}
}
Type::And(tys) => {
Type::And(tys, _) => {
for k in tys.iter().map(|t| comp_item_kind(t, muty)) {
if k != CompletionItemKind::VARIABLE {
return k;
Expand Down
6 changes: 3 additions & 3 deletions crates/erg_compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2716,7 +2716,7 @@ impl PyCodeGenerator {
Expr::Accessor(Accessor::Ident(ident)) if ident.vis().is_private() => {
self.emit_call_local(ident, call.args)
}
other if other.ref_t().is_poly_type_meta() => {
other if other.ref_t().is_poly_meta_type() => {
self.emit_expr(other);
self.emit_index_args(call.args);
}
Expand Down Expand Up @@ -2754,7 +2754,7 @@ impl PyCodeGenerator {
self.emit_load_name_instr(Identifier::private("#sum"));
self.emit_args_311(args, Name);
}
other if local.ref_t().is_poly_type_meta() && other != "classof" => {
other if local.ref_t().is_poly_meta_type() && other != "classof" => {
if self.py_version.minor <= Some(9) {
self.load_fake_generic();
self.emit_load_name_instr(Identifier::private("#FakeGenericAlias"));
Expand Down Expand Up @@ -2798,7 +2798,7 @@ impl PyCodeGenerator {
if let Some(func_name) = debind(&method_name) {
return self.emit_call_fake_method(obj, func_name, method_name, args);
}
let is_type = method_name.ref_t().is_poly_type_meta();
let is_type = method_name.ref_t().is_poly_meta_type();
let kind = if self.py_version.minor >= Some(11) || method_name.vi.t.is_method() {
BoundAttr
} else {
Expand Down
39 changes: 33 additions & 6 deletions crates/erg_compiler/context/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ impl Context {
(lhs, Or(ors)) => ors.iter().all(|or| self.supertype_of(lhs, or)),
// Hash and Eq :> HashEq and ... == true
// Add(T) and Eq :> Add(Int) and Eq == true
(And(l), And(r)) => {
(And(l, _), And(r, _)) => {
if r.iter().any(|r| l.iter().all(|l| self.supertype_of(l, r))) {
return true;
}
Expand All @@ -843,9 +843,9 @@ impl Context {
false
}
// (Num and Show) :> Show == false
(And(ands), rhs) => ands.iter().all(|and| self.supertype_of(and, rhs)),
(And(ands, _), rhs) => ands.iter().all(|and| self.supertype_of(and, rhs)),
// Show :> (Num and Show) == true
(lhs, And(ands)) => ands.iter().any(|and| self.supertype_of(lhs, and)),
(lhs, And(ands, _)) => ands.iter().any(|and| self.supertype_of(lhs, and)),
// Not(Eq) :> Float == !(Eq :> Float) == true
(Not(_), Obj) => false,
(Not(l), rhs) => !self.supertype_of(l, rhs),
Expand Down Expand Up @@ -1097,6 +1097,23 @@ impl Context {
Variance::Covariant => self.supertype_of(sup, sub),
Variance::Invariant => self.same_type_of(sup, sub),
},
(TyParam::Type(sup), TyParam::Value(ValueObj::Type(sub))) => match variance {
Variance::Contravariant => self.subtype_of(sup, sub.typ()),
Variance::Covariant => self.supertype_of(sup, sub.typ()),
Variance::Invariant => self.same_type_of(sup, sub.typ()),
},
(TyParam::Value(ValueObj::Type(sup)), TyParam::Type(sub)) => match variance {
Variance::Contravariant => self.subtype_of(sup.typ(), sub),
Variance::Covariant => self.supertype_of(sup.typ(), sub),
Variance::Invariant => self.same_type_of(sup.typ(), sub),
},
(TyParam::Value(ValueObj::Type(sup)), TyParam::Value(ValueObj::Type(sub))) => {
match variance {
Variance::Contravariant => self.subtype_of(sup.typ(), sub.typ()),
Variance::Covariant => self.supertype_of(sup.typ(), sub.typ()),
Variance::Invariant => self.same_type_of(sup.typ(), sub.typ()),
}
}
(
TyParam::App { name, args },
TyParam::App {
Expand Down Expand Up @@ -1154,12 +1171,15 @@ impl Context {
Type::Obj
}
};
/*
if variance == Variance::Contravariant {
self.subtype_of(&fv_t, &sub_t)
} else {
// REVIEW: covariant, invariant
self.supertype_of(&fv_t, &sub_t)
}
*/
self.subtype_of(&sub_t, &fv_t) || self.supertype_of(&sub_t, &fv_t)
}
(_, TyParam::FreeVar(fv)) if fv.is_unbound() => {
let Some(fv_t) = fv.get_type() else {
Expand All @@ -1172,14 +1192,18 @@ impl Context {
Type::Obj
}
};
/*
if variance == Variance::Contravariant {
self.subtype_of(&sup_t, &fv_t)
} else {
// REVIEW: covariant, invariant
self.supertype_of(&sup_t, &fv_t)
}
*/
self.subtype_of(&sup_t, &fv_t) || self.supertype_of(&sup_t, &fv_t)
}
(TyParam::Value(sup), _) => {
// Value([Value(1)]) => TyParam([Value(1)])
if let Ok(sup) = Self::convert_value_into_tp(sup.clone()) {
self.supertype_of_tp(&sup, sub_p, variance)
} else {
Expand Down Expand Up @@ -1408,6 +1432,7 @@ impl Context {
/// union(?T(<: Str), ?U(<: Int)) == ?T or ?U
/// union(List(Int, 2), List(Str, 2)) == List(Int or Str, 2)
/// union(List(Int, 2), List(Str, 3)) == List(Int, 2) or List(Int, 3)
/// union(List(Int, 2), List(Int, ?N)) == List(Int, ?N)
/// union({ .a = Int }, { .a = Str }) == { .a = Int or Str }
/// union({ .a = Int }, { .a = Int; .b = Int }) == { .a = Int } or { .a = Int; .b = Int } # not to lost `b` information
/// union((A and B) or C) == (A or C) and (B or C)
Expand Down Expand Up @@ -1477,7 +1502,7 @@ impl Context {
},
(other, or @ Or(_)) | (or @ Or(_), other) => self.union_add(or, other),
// (A and B) or C ==> (A or C) and (B or C)
(And(ands), other) | (other, And(ands)) => {
(And(ands, _), other) | (other, And(ands, _)) => {
let mut t = Type::Obj;
for branch in ands.iter() {
let union = self.union(branch, other);
Expand Down Expand Up @@ -1697,7 +1722,9 @@ impl Context {
(_, Not(r)) => self.diff(lhs, r),
(Not(l), _) => self.diff(rhs, l),
// A and B and A == A and B
(other, and @ And(_)) | (and @ And(_), other) => self.intersection_add(and, other),
(other, and @ And(_, _)) | (and @ And(_, _), other) => {
self.intersection_add(and, other)
}
// (A or B) and C == (A and C) or (B and C)
(Or(ors), other) | (other, Or(ors)) => {
if ors.iter().any(|t| t.has_unbound_var()) {
Expand Down Expand Up @@ -1995,7 +2022,7 @@ impl Context {
Or(ors) => ors
.iter()
.fold(Obj, |l, r| self.intersection(&l, &self.complement(r))),
And(ands) => ands
And(ands, _) => ands
.iter()
.fold(Never, |l, r| self.union(&l, &self.complement(r))),
other => not(other.clone()),
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,7 @@ impl Context {
Err((t, errs))
}
}
Type::And(ands) => {
Type::And(ands, _) => {
let mut new_ands = set! {};
for and in ands.into_iter() {
match self.eval_t_params(and, level, t_loc) {
Expand Down
6 changes: 3 additions & 3 deletions crates/erg_compiler/context/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,13 @@ impl Generalizer {
}
proj_call(lhs, attr_name, args)
}
And(ands) => {
And(ands, idx) => {
// not `self.intersection` because types are generalized
let ands = ands
.into_iter()
.map(|t| self.generalize_t(t, uninit))
.collect();
Type::checked_and(ands)
Type::checked_and(ands, idx)
}
Or(ors) => {
// not `self.union` because types are generalized
Expand Down Expand Up @@ -1049,7 +1049,7 @@ impl<'c, 'q, 'l, L: Locational> Dereferencer<'c, 'q, 'l, L> {
let pred = self.deref_pred(*refine.pred)?;
Ok(refinement(refine.var, t, pred))
}
And(ands) => {
And(ands, _) => {
let mut new_ands = vec![];
for t in ands.into_iter() {
new_ands.push(self.deref_tyvar(t)?);
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Context {
return Some(hint);
}
}
(Type::And(tys), found) if tys.len() == 2 => {
(Type::And(tys, _), found) if tys.len() == 2 => {
let mut iter = tys.iter();
let l = iter.next().unwrap();
let r = iter.next().unwrap();
Expand Down
Loading
Loading