Skip to content

Polymorphic type #89

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ log = "0.4.8"
env_logger = "0.7.1"
regex = "1.3.7"
wasm = { version = "0.1.2", package = "web-assembler" }
itertools = "0.10.3"


[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
5 changes: 5 additions & 0 deletions ml_example/online.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fun fib 0 = 1
| fib 1 = 1
| fib n = fib (n - 1) + fib (n - 2)

val () = printInt (fib 39)
9 changes: 9 additions & 0 deletions ml_example/polymorphic_function.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fun id x = x
val x = id 1
val y = id false
val id2 = id
val x = id2 1
val y = id2 false
val id3 = id2
val x = id3 1
val y = id3 false
1 change: 1 addition & 0 deletions ml_example/stringFromInt2digits.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val s = stringFromInt 9
4 changes: 3 additions & 1 deletion src/ast/case_simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,9 @@ impl CaseSimplifyPass {
) -> bool {
use Type::*;
match ty {
Real | Variable(_) | Fun(_, _) => panic!("no way to pattern match against this type"),
Real | Variable(_) | Fun(_, _) | TyAbs(_, _) => {
panic!("no way to pattern match against this type")
}
Char | Int => false,
Tuple(_) => {
// unlikely reachable, but writing incase it reaches.
Expand Down
5 changes: 5 additions & 0 deletions src/ast/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl Desugar {
} => self.transform_externcall(span, module, fun, args, argty, retty),
Fn { param, body } => self.transform_fn(span, param, body),
App { fun, arg } => self.transform_app(span, fun, arg),
TyApp { fun, arg } => self.transform_tyapp(span, fun, arg),
Case { cond, clauses } => self.transform_case(span, cond, clauses),
Tuple { tuple } => self.transform_tuple(span, tuple),
Constructor { arg, name } => self.transform_constructor(span, arg, name),
Expand Down Expand Up @@ -284,6 +285,10 @@ impl Desugar {
}
}

fn transform_tyapp(&mut self, _: Span, fun: Symbol, arg: Vec<Empty>) -> UntypedCoreExprKind {
ExprKind::TyApp { fun, arg }
}

fn transform_if(
&mut self,
span: Span,
Expand Down
14 changes: 13 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod case_simplify;
mod collect_langitems;
mod desugar;
mod monomorphize;
mod pp;
mod rename;
mod resolve_overload;
Expand All @@ -11,6 +12,7 @@ mod var2constructor;
pub use self::case_simplify::CaseSimplify;
pub use self::collect_langitems::CollectLangItems;
pub use self::desugar::Desugar;
pub use self::monomorphize::Monomorphize;
pub use self::rename::Rename;
pub use self::resolve_overload::ResolveOverload;
pub use self::typing::Typer;
Expand Down Expand Up @@ -198,6 +200,10 @@ pub enum ExprKind<
fun: Box<Expr<Ty, DE, DS, DP>>,
arg: Box<Expr<Ty, DE, DS, DP>>,
},
TyApp {
fun: Symbol,
arg: Vec<Ty>,
},
Case {
cond: Box<Expr<Ty, DE, DS, DP>>,
clauses: Vec<(Pattern<Ty, DP>, Expr<Ty, DE, DS, DP>)>,
Expand Down Expand Up @@ -301,7 +307,7 @@ pub struct SymbolTable {

type TypeId = u64;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Type {
Variable(TypeId),
Char,
Expand All @@ -310,6 +316,7 @@ pub enum Type {
Fun(Box<Type>, Box<Type>),
Tuple(Vec<Type>),
Datatype(Symbol),
TyAbs(Vec<TypeId>, Box<Type>),
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -476,6 +483,10 @@ impl<Ty> CoreExpr<Ty> {
fun: fun.map_ty(f).boxed(),
arg: arg.map_ty(f).boxed(),
},
TyApp { fun, arg } => TyApp {
fun,
arg: arg.into_iter().map(f).collect(),
},
Case { cond, clauses } => Case {
cond: cond.map_ty(&mut *f).boxed(),
clauses: clauses
Expand Down Expand Up @@ -506,6 +517,7 @@ impl<Ty> CoreExpr<Ty> {
ExternCall { .. } => false,
Fn { .. } => true,
App { .. } => false,
TyApp { .. } => true,
// TODO: check compatibility with fn
Case { .. } => false,
Tuple { tuple } => tuple.iter().all(|e| e.is_value()),
Expand Down
Loading