From 01cd0cfbe1f906afba5281dc71452fc19e5234e4 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sat, 2 Feb 2019 16:09:10 -1000 Subject: [PATCH] Split out some modules --- src/canonical.rs | 1 + src/constrain.rs | 24 +++++++++++++ src/expr.rs | 16 +++++++++ src/lib.rs | 5 +++ src/name.rs | 1 + src/solve.rs | 94 +++++++++++++++--------------------------------- src/typ.rs | 12 +++++++ 7 files changed, 87 insertions(+), 66 deletions(-) create mode 100644 src/canonical.rs create mode 100644 src/constrain.rs create mode 100644 src/expr.rs create mode 100644 src/name.rs create mode 100644 src/typ.rs diff --git a/src/canonical.rs b/src/canonical.rs new file mode 100644 index 0000000000..9b31875b62 --- /dev/null +++ b/src/canonical.rs @@ -0,0 +1 @@ +pub enum Annotation {} diff --git a/src/constrain.rs b/src/constrain.rs new file mode 100644 index 0000000000..43492d1b1f --- /dev/null +++ b/src/constrain.rs @@ -0,0 +1,24 @@ +use typ::Type; + +// constrainDecls :: Can.Decls -> Constraint -> IO Constraint +// constrainDecls decls finalConstraint = +// case decls of +// Can.Declare def otherDecls -> +// Expr.constrainDef Map.empty def =<< constrainDecls otherDecls finalConstraint + +// Can.DeclareRec defs otherDecls -> +// Expr.constrainRecursiveDefs Map.empty defs =<< constrainDecls otherDecls finalConstraint + +// Can.SaveTheEnvironment -> +// return finalConstraint + + + +pub type ExpectedType = Type; + + +pub enum Constraint { + True, + Equal(Type, ExpectedType), + Batch(Vec), +} diff --git a/src/expr.rs b/src/expr.rs new file mode 100644 index 0000000000..329054d75d --- /dev/null +++ b/src/expr.rs @@ -0,0 +1,16 @@ + + +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum Operator { + Plus, Minus, FloatDivision, IntDivision, +} + +#[derive(Debug, PartialEq)] +pub enum Expr { + HexOctalBinary(i64), // : Int + FractionalNumber(f64), // : Float + WholeNumber(i64), // : Int | Float + + // Functions + CallOperator(Operator, Box, Box), +} diff --git a/src/lib.rs b/src/lib.rs index 77b05bbca0..91e73b30d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,11 @@ // pub mod repl; pub mod solve; +mod expr; +mod constrain; +mod canonical; +mod name; +mod typ; mod ena; #[macro_use] diff --git a/src/name.rs b/src/name.rs new file mode 100644 index 0000000000..13a9337411 --- /dev/null +++ b/src/name.rs @@ -0,0 +1 @@ +pub type Name = String; diff --git a/src/solve.rs b/src/solve.rs index 32f285cd07..8fb99f8faa 100644 --- a/src/solve.rs +++ b/src/solve.rs @@ -1,47 +1,16 @@ use std::collections::BTreeSet; +use std::collections::HashMap; +use constrain::Constraint; +use typ::Type; +use canonical::Annotation; +use name::Name; use self::Variable::*; use ena::unify::{UnificationTable, UnifyKey, InPlace}; -pub type Name = String; - -pub type ModuleName = String; - type UTable = UnificationTable>; #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub enum Type { - // Symbol(String), - // Int, - // Float, - // Number, - // TypeUnion(BTreeSet), - // Function(Box, Box), - CallOperator(Operator, Box, Box), -} - - -#[derive(Debug, PartialEq)] -pub enum Expr { - HexOctalBinary(i64), // : Int - FractionalNumber(f64), // : Float - WholeNumber(i64), // : Int | Float - - // Functions - CallOperator(Operator, Box, Box), -} - -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub enum Operator { - Plus, Minus, FloatDivision, IntDivision, -} - -#[derive(Debug, PartialEq)] -pub enum Problem { - Mismatch -} - -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub enum Variable { +enum Variable { Wildcard, RigidVar(Name), FlexUnion(BTreeSet), @@ -50,10 +19,8 @@ pub enum Variable { Mismatch } -type CanonicalModuleName = String; - #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub enum FlatType { +enum FlatType { Function(VarId, VarId), // Apply a higher-kinded type constructor by name. For example: @@ -166,7 +133,7 @@ fn unify_vars(utable: &mut UTable, first: &Variable, second: &Variable) -> Varia } #[inline] -pub fn unify_structure(utable: &mut UTable, flat_type: &FlatType, var: &Variable, other: &Variable) -> Variable { +fn unify_structure(utable: &mut UTable, flat_type: &FlatType, var: &Variable, other: &Variable) -> Variable { match other { Wildcard => var.clone(), RigidVar(_) => Mismatch, @@ -178,7 +145,7 @@ pub fn unify_structure(utable: &mut UTable, flat_type: &FlatType, var: &Variable } #[inline] -pub fn unify_flat_types(utable: &mut UTable, flat_type: &FlatType, other_flat_type: &FlatType) -> Variable { +fn unify_flat_types(utable: &mut UTable, flat_type: &FlatType, other_flat_type: &FlatType) -> Variable { match (flat_type, other_flat_type) { (FlatType::Function(my_arg, my_return), FlatType::Function(other_arg, other_return)) => { @@ -257,40 +224,27 @@ fn unify_flex_union_with_structure(flex_union: &BTreeSet, var: &Variable) // } } -type ExpectedType = Type; - -pub enum Constraint { - True, - Equal(Type, ExpectedType), - Batch(Vec), -} - -pub fn infer_type(expr: Expr) -> Result { - Err(Problem::Mismatch) -} - -struct State { - errors: Vec -} // Given a type, create a constraint variable for it and add it to the table. // Return the VarId corresponding to the variable in the table. fn type_to_var_id(utable: &mut UTable, typ: Type) -> VarId { match typ { - Type::CallOperator(op, box left_type, box right_type) => { - let left_var_id = type_to_var_id(utable, left_type); - let right_var_id = type_to_var_id(utable, right_type); + Type::Call(box fn_type, box arg_type) => { + panic!("TODO"); + utable.new_key(Mismatch) + // let left_var_id = type_to_var_id(utable, left_type); + // let right_var_id = type_to_var_id(utable, right_type); - // TODO should we match on op to hardcode the types we expect? - let flat_type = FlatType::Function(left_var_id, right_var_id); + // // TODO should we match on op to hardcode the types we expect? + // let flat_type = FlatType::Function(left_var_id, right_var_id); - utable.new_key(Structure(flat_type)) + // utable.new_key(Structure(flat_type)) } } } #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct VarId(u32); +struct VarId(u32); impl UnifyKey for VarId { type Value = Variable; @@ -313,9 +267,17 @@ fn unify_var_ids(utable: &mut UTable, left_id: VarId, right_id: VarId) -> Variab } } -type TypeError = String; +pub type TypeError = String; -pub fn solve(utable: &mut UTable, errors: &mut Vec, constraint: Constraint) { +pub fn solve_constraint(constraint: Constraint) -> Result> { + let mut utable: UTable = UnificationTable::new(); + + solve(&mut utable, constraint); + + Ok("TODO: actually gather errors etc".to_owned()) +} + +fn solve(utable: &mut UTable, constraint: Constraint) { match constraint { Constraint::True => {}, diff --git a/src/typ.rs b/src/typ.rs new file mode 100644 index 0000000000..5f211e92e8 --- /dev/null +++ b/src/typ.rs @@ -0,0 +1,12 @@ + + +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum Type { + // Symbol(String), + // Int, + // Float, + // Number, + // TypeUnion(BTreeSet), + // Function(Box, Box), + Call(Box, Box), +}