Compare commits
3 commits
dev
...
topic-suga
Author | SHA1 | Date | |
---|---|---|---|
71a8f4e06b | |||
91cd1cd7d8 | |||
2d387e6279 |
12 changed files with 122 additions and 410 deletions
57
README.md
57
README.md
|
@ -5,8 +5,6 @@ Rust Implementation of Ladder-Types (parsing, unification, rewriting, etc)
|
|||
|
||||
## Ladder Types
|
||||
|
||||
### Motivation
|
||||
|
||||
In order to implement complex datastructures and algorithms, usually
|
||||
many layers of abstraction are built ontop of each other.
|
||||
Consequently higher-level data types are encoded into lower-level data
|
||||
|
@ -59,48 +57,6 @@ this:
|
|||
1696093021:1696093039:1528324679:1539892301:1638141920:1688010253
|
||||
```
|
||||
|
||||
### Syntax
|
||||
|
||||
In their core form, type-terms can be one of the following:
|
||||
- (**Atomic Type**) | `SomeTypeName`
|
||||
- (**Literal Integer**) | `0` | `1` | `2` | ...
|
||||
- (**Literal Character**) | `'a'` | `'b'` | `'c'` | ...
|
||||
- (**Literal String**) | `"abc"`
|
||||
- (**Parameter Application**) | `<T1 T2>` given `T1` and `T2` are type-terms
|
||||
- (**Ladder**) | `T1 ~ T2` given `T1` and `T2` are type-terms
|
||||
|
||||
Ontop of that, the following syntax-sugar is defined:
|
||||
|
||||
#### Complex Types
|
||||
- `[ T ]` <===> `<Seq T>`
|
||||
- `{ a:A b:B }` <===> `<Struct <"a" A> <"b" B>>`
|
||||
- `a:A | b:B` <===> `<Enum <"a" A> <"b" B>>`
|
||||
|
||||
#### Function Types
|
||||
- `A -> B` <===> `<Fn A B>`
|
||||
|
||||
#### Reference Types
|
||||
- `*A` <===> `<Ptr A>`
|
||||
- `&A` <===> `<ConstRef A>`
|
||||
- `&!A` <===> `<MutRef A>`
|
||||
|
||||
|
||||
### Equivalences
|
||||
|
||||
#### Currying
|
||||
`<<A B> C>` <===> `<A B C>`
|
||||
|
||||
#### Ladder-Normal-Form
|
||||
exhaustively apply `<A B~C>` ===> `<A B>~<A C>`
|
||||
|
||||
e.g. `[<Digit 10>]~[Char]~[Ascii]` is in **LNF**
|
||||
|
||||
#### Parameter-Normal-Form
|
||||
exhaustively apply `<A B>~<A C>` ===> `<A B~C>`
|
||||
|
||||
e.g. `[<Digit 10>~Char~Ascii]` is in **PNF**
|
||||
|
||||
|
||||
## How to use this crate
|
||||
|
||||
```rust
|
||||
|
@ -117,19 +73,6 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [x] (Un-)Parsing
|
||||
- [x] (De-)Currying
|
||||
- [x] Unification
|
||||
- [x] Ladder-Normal-Form
|
||||
- [x] Parameter-Normal-Form
|
||||
- [ ] (De)-Sugaring
|
||||
- [ ] Seq
|
||||
- [ ] Enum
|
||||
- [ ] Struct
|
||||
- [ ] References
|
||||
- [ ] Function
|
||||
|
||||
## License
|
||||
[GPLv3](COPYING)
|
||||
|
|
17
src/dict.rs
17
src/dict.rs
|
@ -1,4 +1,7 @@
|
|||
use crate::bimap::Bimap;
|
||||
use crate::{
|
||||
bimap::Bimap,
|
||||
sugar::SUGARID_LIMIT
|
||||
};
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
||||
|
||||
|
@ -20,11 +23,19 @@ pub struct TypeDict {
|
|||
|
||||
impl TypeDict {
|
||||
pub fn new() -> Self {
|
||||
TypeDict {
|
||||
let mut dict = TypeDict {
|
||||
typenames: Bimap::new(),
|
||||
type_lit_counter: 0,
|
||||
type_var_counter: 0,
|
||||
}
|
||||
};
|
||||
|
||||
dict.add_typename("Seq".into());
|
||||
dict.add_typename("Enum".into());
|
||||
dict.add_typename("Struct".into());
|
||||
|
||||
assert_eq!( dict.type_lit_counter, SUGARID_LIMIT );
|
||||
|
||||
dict
|
||||
}
|
||||
|
||||
pub fn add_varname(&mut self, tn: String) -> TypeID {
|
||||
|
|
11
src/lexer.rs
11
src/lexer.rs
|
@ -6,9 +6,9 @@ pub enum LadderTypeToken {
|
|||
Symbol( String ),
|
||||
Char( char ),
|
||||
Num( i64 ),
|
||||
Open,
|
||||
Close,
|
||||
Ladder,
|
||||
Open, OpenSeq, OpenStruct,
|
||||
Close, CloseSeq, CloseStruct,
|
||||
Ladder, Enum
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
|
@ -75,6 +75,11 @@ where It: Iterator<Item = char>
|
|||
match c {
|
||||
'<' => { self.chars.next(); return Some(Ok(LadderTypeToken::Open)); },
|
||||
'>' => { self.chars.next(); return Some(Ok(LadderTypeToken::Close)); },
|
||||
'[' => { self.chars.next(); return Some(Ok(LadderTypeToken::OpenSeq)); },
|
||||
']' => { self.chars.next(); return Some(Ok(LadderTypeToken::CloseSeq)); },
|
||||
'{' => { self.chars.next(); return Some(Ok(LadderTypeToken::OpenStruct)); },
|
||||
'}' => { self.chars.next(); return Some(Ok(LadderTypeToken::CloseStruct)); },
|
||||
'|' => { self.chars.next(); return Some(Ok(LadderTypeToken::Enum)); },
|
||||
'~' => { self.chars.next(); return Some(Ok(LadderTypeToken::Ladder)); },
|
||||
'\'' => { self.chars.next(); state = LexerState::Char(None); },
|
||||
c => {
|
||||
|
|
|
@ -7,8 +7,8 @@ pub mod parser;
|
|||
pub mod unparser;
|
||||
pub mod sugar;
|
||||
pub mod curry;
|
||||
pub mod sugar;
|
||||
pub mod lnf;
|
||||
pub mod pnf;
|
||||
pub mod subtype;
|
||||
pub mod unification;
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ use {
|
|||
crate::{
|
||||
dict::*,
|
||||
term::*,
|
||||
sugar::*,
|
||||
lexer::*
|
||||
}
|
||||
};
|
||||
|
@ -21,7 +22,7 @@ pub enum ParseError {
|
|||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
||||
|
||||
impl TypeDict {
|
||||
pub fn parse(&mut self, s: &str) -> Result<TypeTerm, ParseError> {
|
||||
pub fn parse(&mut self, s: &str) -> Result<SugaredTypeTerm, ParseError> {
|
||||
let mut tokens = LadderTypeLexer::from(s.chars()).peekable();
|
||||
|
||||
match self.parse_ladder(&mut tokens) {
|
||||
|
@ -36,7 +37,7 @@ impl TypeDict {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_app<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<TypeTerm, ParseError>
|
||||
fn parse_app<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<SugaredTypeTerm, ParseError>
|
||||
where It: Iterator<Item = char>
|
||||
{
|
||||
let mut args = Vec::new();
|
||||
|
@ -44,7 +45,11 @@ impl TypeDict {
|
|||
match tok {
|
||||
Ok(LadderTypeToken::Close) => {
|
||||
tokens.next();
|
||||
return Ok(TypeTerm::App(args));
|
||||
return Ok(SugaredTypeTerm::App(args));
|
||||
}
|
||||
Ok(LadderTypeToken::CloseSeq) |
|
||||
Ok(LadderTypeToken::CloseStruct) => {
|
||||
return Err(ParseError::UnexpectedToken)
|
||||
}
|
||||
_ => {
|
||||
match self.parse_ladder(tokens) {
|
||||
|
@ -57,29 +62,59 @@ impl TypeDict {
|
|||
Err(ParseError::UnexpectedEnd)
|
||||
}
|
||||
|
||||
fn parse_rung<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<TypeTerm, ParseError>
|
||||
fn parse_seq<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<SugaredTypeTerm, ParseError>
|
||||
where It: Iterator<Item = char>
|
||||
{
|
||||
let mut pattern = Vec::new();
|
||||
while let Some(tok) = tokens.peek() {
|
||||
match tok {
|
||||
Ok(LadderTypeToken::CloseSeq) => {
|
||||
tokens.next();
|
||||
return Ok(SugaredTypeTerm::Seq(pattern));
|
||||
}
|
||||
Ok(LadderTypeToken::Close) |
|
||||
Ok(LadderTypeToken::CloseStruct) => {
|
||||
return Err(ParseError::UnexpectedToken)
|
||||
}
|
||||
_ => {
|
||||
match self.parse_ladder(tokens) {
|
||||
Ok(a) => { pattern.push(a); }
|
||||
Err(err) => { return Err(err); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(ParseError::UnexpectedEnd)
|
||||
}
|
||||
|
||||
fn parse_rung<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<SugaredTypeTerm, ParseError>
|
||||
where It: Iterator<Item = char>
|
||||
{
|
||||
match tokens.next() {
|
||||
Some(Ok(LadderTypeToken::Open)) => self.parse_app(tokens),
|
||||
Some(Ok(LadderTypeToken::OpenSeq)) => self.parse_app(tokens),
|
||||
Some(Ok(LadderTypeToken::OpenStruct)) => self.parse_app(tokens),
|
||||
Some(Ok(LadderTypeToken::Enum)) => self.parse_app(tokens),
|
||||
Some(Ok(LadderTypeToken::Close)) => Err(ParseError::UnexpectedClose),
|
||||
Some(Ok(LadderTypeToken::CloseStruct)) => Err(ParseError::UnexpectedToken),
|
||||
Some(Ok(LadderTypeToken::CloseSeq)) => Err(ParseError::UnexpectedToken),
|
||||
Some(Ok(LadderTypeToken::Ladder)) => Err(ParseError::UnexpectedLadder),
|
||||
Some(Ok(LadderTypeToken::Symbol(s))) =>
|
||||
Ok(TypeTerm::TypeID(
|
||||
Ok(SugaredTypeTerm::TypeID(
|
||||
if let Some(tyid) = self.get_typeid(&s) {
|
||||
tyid
|
||||
} else {
|
||||
self.add_typename(s)
|
||||
}
|
||||
)),
|
||||
Some(Ok(LadderTypeToken::Char(c))) => Ok(TypeTerm::Char(c)),
|
||||
Some(Ok(LadderTypeToken::Num(n))) => Ok(TypeTerm::Num(n)),
|
||||
Some(Ok(LadderTypeToken::Char(c))) => Ok(SugaredTypeTerm::Char(c)),
|
||||
Some(Ok(LadderTypeToken::Num(n))) => Ok(SugaredTypeTerm::Num(n)),
|
||||
Some(Err(err)) => Err(ParseError::LexError(err)),
|
||||
None => Err(ParseError::UnexpectedEnd)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_ladder<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<TypeTerm, ParseError>
|
||||
fn parse_ladder<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<SugaredTypeTerm, ParseError>
|
||||
where It: Iterator<Item = char>
|
||||
{
|
||||
let mut rungs = Vec::new();
|
||||
|
@ -115,7 +150,7 @@ impl TypeDict {
|
|||
match rungs.len() {
|
||||
0 => Err(ParseError::UnexpectedEnd),
|
||||
1 => Ok(rungs[0].clone()),
|
||||
_ => Ok(TypeTerm::Ladder(rungs)),
|
||||
_ => Ok(SugaredTypeTerm::Ladder(rungs)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
138
src/pnf.rs
138
src/pnf.rs
|
@ -1,138 +0,0 @@
|
|||
use crate::term::TypeTerm;
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
||||
|
||||
pub fn splice_ladders( mut upper: Vec< TypeTerm >, mut lower: Vec< TypeTerm > ) -> Vec< TypeTerm > {
|
||||
for i in 0 .. upper.len() {
|
||||
if upper[i] == lower[0] {
|
||||
let mut result_ladder = Vec::<TypeTerm>::new();
|
||||
result_ladder.append(&mut upper[0..i].iter().cloned().collect());
|
||||
result_ladder.append(&mut lower);
|
||||
return result_ladder;
|
||||
}
|
||||
}
|
||||
|
||||
upper.append(&mut lower);
|
||||
upper
|
||||
}
|
||||
|
||||
impl TypeTerm {
|
||||
/// transmute type into Parameter-Normal-Form (PNF)
|
||||
///
|
||||
/// Example:
|
||||
/// ```ignore
|
||||
/// <Seq <Digit 10>>~<Seq Char>
|
||||
/// ⇒ <Seq <Digit 10>~Char>
|
||||
/// ```
|
||||
pub fn param_normalize(mut self) -> Self {
|
||||
match self {
|
||||
TypeTerm::Ladder(mut rungs) => {
|
||||
if rungs.len() > 0 {
|
||||
let mut new_rungs = Vec::new();
|
||||
while let Some(bottom) = rungs.pop() {
|
||||
if let Some(last_but) = rungs.last_mut() {
|
||||
match (bottom, last_but) {
|
||||
(TypeTerm::App(bot_args), TypeTerm::App(last_args)) => {
|
||||
if bot_args.len() == last_args.len() {
|
||||
let mut new_rung_params = Vec::new();
|
||||
let mut require_break = false;
|
||||
|
||||
if bot_args.len() > 0 {
|
||||
if let Ok(_idx) = last_args[0].is_syntactic_subtype_of(&bot_args[0]) {
|
||||
for i in 0 .. bot_args.len() {
|
||||
|
||||
let spliced_type_ladder = splice_ladders(
|
||||
last_args[i].clone().get_lnf_vec(),
|
||||
bot_args[i].clone().get_lnf_vec()
|
||||
);
|
||||
let spliced_type =
|
||||
if spliced_type_ladder.len() == 1 {
|
||||
spliced_type_ladder[0].clone()
|
||||
} else if spliced_type_ladder.len() > 1 {
|
||||
TypeTerm::Ladder(spliced_type_ladder)
|
||||
} else {
|
||||
TypeTerm::unit()
|
||||
};
|
||||
|
||||
new_rung_params.push( spliced_type.param_normalize() );
|
||||
}
|
||||
|
||||
} else {
|
||||
new_rung_params.push(
|
||||
TypeTerm::Ladder(vec![
|
||||
last_args[0].clone(),
|
||||
bot_args[0].clone()
|
||||
]).normalize()
|
||||
);
|
||||
|
||||
for i in 1 .. bot_args.len() {
|
||||
if let Ok(_idx) = last_args[i].is_syntactic_subtype_of(&bot_args[i]) {
|
||||
let spliced_type_ladder = splice_ladders(
|
||||
last_args[i].clone().get_lnf_vec(),
|
||||
bot_args[i].clone().get_lnf_vec()
|
||||
);
|
||||
let spliced_type =
|
||||
if spliced_type_ladder.len() == 1 {
|
||||
spliced_type_ladder[0].clone()
|
||||
} else if spliced_type_ladder.len() > 1 {
|
||||
TypeTerm::Ladder(spliced_type_ladder)
|
||||
} else {
|
||||
TypeTerm::unit()
|
||||
};
|
||||
|
||||
new_rung_params.push( spliced_type.param_normalize() );
|
||||
} else {
|
||||
new_rung_params.push( bot_args[i].clone() );
|
||||
require_break = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if require_break {
|
||||
new_rungs.push( TypeTerm::App(new_rung_params) );
|
||||
} else {
|
||||
rungs.pop();
|
||||
rungs.push(TypeTerm::App(new_rung_params));
|
||||
}
|
||||
|
||||
} else {
|
||||
new_rungs.push( TypeTerm::App(bot_args) );
|
||||
}
|
||||
}
|
||||
(bottom, last_buf) => {
|
||||
new_rungs.push( bottom );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
new_rungs.push( bottom );
|
||||
}
|
||||
}
|
||||
|
||||
new_rungs.reverse();
|
||||
|
||||
if new_rungs.len() > 1 {
|
||||
TypeTerm::Ladder(new_rungs)
|
||||
} else if new_rungs.len() == 1 {
|
||||
new_rungs[0].clone()
|
||||
} else {
|
||||
TypeTerm::unit()
|
||||
}
|
||||
} else {
|
||||
TypeTerm::unit()
|
||||
}
|
||||
}
|
||||
|
||||
TypeTerm::App(params) => {
|
||||
TypeTerm::App(
|
||||
params.into_iter()
|
||||
.map(|p| p.param_normalize())
|
||||
.collect())
|
||||
}
|
||||
|
||||
atomic => atomic
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
95
src/sugar.rs
95
src/sugar.rs
|
@ -1,95 +0,0 @@
|
|||
use {
|
||||
crate::{TypeTerm, TypeID}
|
||||
};
|
||||
|
||||
pub enum SugaredTypeTerm {
|
||||
TypeID(TypeID),
|
||||
Num(i64),
|
||||
Char(char),
|
||||
Univ(Box< SugaredTypeTerm >),
|
||||
Spec(Vec< SugaredTypeTerm >),
|
||||
Func(Vec< SugaredTypeTerm >),
|
||||
Morph(Vec< SugaredTypeTerm >),
|
||||
Ladder(Vec< SugaredTypeTerm >),
|
||||
Struct(Vec< SugaredTypeTerm >),
|
||||
Enum(Vec< SugaredTypeTerm >),
|
||||
Seq(Vec< SugaredTypeTerm >)
|
||||
}
|
||||
|
||||
impl TypeTerm {
|
||||
pub fn sugar(self: TypeTerm, dict: &mut crate::TypeDict) -> SugaredTypeTerm {
|
||||
match self {
|
||||
TypeTerm::TypeID(id) => SugaredTypeTerm::TypeID(id),
|
||||
TypeTerm::Num(n) => SugaredTypeTerm::Num(n),
|
||||
TypeTerm::Char(c) => SugaredTypeTerm::Char(c),
|
||||
TypeTerm::App(args) => if let Some(first) = args.first() {
|
||||
if first == &dict.parse("Func").unwrap() {
|
||||
SugaredTypeTerm::Func( args[1..].into_iter().map(|t| t.clone().sugar(dict)).collect() )
|
||||
}
|
||||
else if first == &dict.parse("Morph").unwrap() {
|
||||
SugaredTypeTerm::Morph( args[1..].into_iter().map(|t| t.clone().sugar(dict)).collect() )
|
||||
}
|
||||
else if first == &dict.parse("Struct").unwrap() {
|
||||
SugaredTypeTerm::Struct( args[1..].into_iter().map(|t| t.clone().sugar(dict)).collect() )
|
||||
}
|
||||
else if first == &dict.parse("Enum").unwrap() {
|
||||
SugaredTypeTerm::Enum( args[1..].into_iter().map(|t| t.clone().sugar(dict)).collect() )
|
||||
}
|
||||
else if first == &dict.parse("Seq").unwrap() {
|
||||
SugaredTypeTerm::Seq( args[1..].into_iter().map(|t| t.clone().sugar(dict)).collect() )
|
||||
}
|
||||
else if first == &dict.parse("Spec").unwrap() {
|
||||
SugaredTypeTerm::Spec( args[1..].into_iter().map(|t| t.clone().sugar(dict)).collect() )
|
||||
}
|
||||
else if first == &dict.parse("Univ").unwrap() {
|
||||
SugaredTypeTerm::Univ(Box::new(
|
||||
SugaredTypeTerm::Spec(
|
||||
args[1..].into_iter().map(|t| t.clone().sugar(dict)).collect()
|
||||
)
|
||||
))
|
||||
}
|
||||
else {
|
||||
SugaredTypeTerm::Spec(args.into_iter().map(|t| t.sugar(dict)).collect())
|
||||
}
|
||||
} else {
|
||||
SugaredTypeTerm::Spec(args.into_iter().map(|t| t.sugar(dict)).collect())
|
||||
},
|
||||
TypeTerm::Ladder(rungs) =>
|
||||
SugaredTypeTerm::Ladder(rungs.into_iter().map(|t| t.sugar(dict)).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SugaredTypeTerm {
|
||||
pub fn desugar(self, dict: &mut crate::TypeDict) -> TypeTerm {
|
||||
match self {
|
||||
SugaredTypeTerm::TypeID(id) => TypeTerm::TypeID(id),
|
||||
SugaredTypeTerm::Num(n) => TypeTerm::Num(n),
|
||||
SugaredTypeTerm::Char(c) => TypeTerm::Char(c),
|
||||
SugaredTypeTerm::Univ(t) => t.desugar(dict),
|
||||
SugaredTypeTerm::Spec(ts) => TypeTerm::App(ts.into_iter().map(|t| t.desugar(dict)).collect()),
|
||||
SugaredTypeTerm::Ladder(ts) => TypeTerm::Ladder(ts.into_iter().map(|t|t.desugar(dict)).collect()),
|
||||
SugaredTypeTerm::Func(ts) => TypeTerm::App(
|
||||
std::iter::once( dict.parse("Func").unwrap() ).chain(
|
||||
ts.into_iter().map(|t| t.desugar(dict))
|
||||
).collect()),
|
||||
SugaredTypeTerm::Morph(ts) => TypeTerm::App(
|
||||
std::iter::once( dict.parse("Morph").unwrap() ).chain(
|
||||
ts.into_iter().map(|t| t.desugar(dict))
|
||||
).collect()),
|
||||
SugaredTypeTerm::Struct(ts) => TypeTerm::App(
|
||||
std::iter::once( dict.parse("Struct").unwrap() ).chain(
|
||||
ts.into_iter().map(|t| t.desugar(dict))
|
||||
).collect()),
|
||||
SugaredTypeTerm::Enum(ts) => TypeTerm::App(
|
||||
std::iter::once( dict.parse("Enum").unwrap() ).chain(
|
||||
ts.into_iter().map(|t| t.desugar(dict))
|
||||
).collect()),
|
||||
SugaredTypeTerm::Seq(ts) => TypeTerm::App(
|
||||
std::iter::once( dict.parse("Seq").unwrap() ).chain(
|
||||
ts.into_iter().map(|t| t.desugar(dict))
|
||||
).collect()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,8 +2,8 @@
|
|||
pub mod lexer;
|
||||
pub mod parser;
|
||||
pub mod curry;
|
||||
pub mod sugar;
|
||||
pub mod lnf;
|
||||
pub mod pnf;
|
||||
pub mod subtype;
|
||||
pub mod substitution;
|
||||
pub mod unification;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
use {
|
||||
crate::{term::*, dict::*, parser::*}
|
||||
crate::{term::*, dict::*, parser::*, sugar::SUGARID_LIMIT}
|
||||
};
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
||||
|
@ -17,7 +17,7 @@ fn test_parser_id() {
|
|||
);
|
||||
|
||||
assert_eq!(
|
||||
Ok(TypeTerm::TypeID(TypeID::Fun(0))),
|
||||
Ok(TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0))),
|
||||
dict.parse("A")
|
||||
);
|
||||
}
|
||||
|
@ -43,16 +43,16 @@ fn test_parser_app() {
|
|||
assert_eq!(
|
||||
TypeDict::new().parse("<A B>"),
|
||||
Ok(TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
]))
|
||||
);
|
||||
assert_eq!(
|
||||
TypeDict::new().parse("<A B C>"),
|
||||
Ok(TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+2)),
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
@ -78,16 +78,16 @@ fn test_parser_ladder() {
|
|||
assert_eq!(
|
||||
TypeDict::new().parse("A~B"),
|
||||
Ok(TypeTerm::Ladder(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
]))
|
||||
);
|
||||
assert_eq!(
|
||||
TypeDict::new().parse("A~B~C"),
|
||||
Ok(TypeTerm::Ladder(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+2)),
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
@ -98,12 +98,12 @@ fn test_parser_ladder_outside() {
|
|||
TypeDict::new().parse("<A B>~C"),
|
||||
Ok(TypeTerm::Ladder(vec![
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
]),
|
||||
TypeTerm::TypeID(TypeID::Fun(2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+2)),
|
||||
]))
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -111,10 +111,10 @@ fn test_parser_ladder_inside() {
|
|||
assert_eq!(
|
||||
TypeDict::new().parse("<A B~C>"),
|
||||
Ok(TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::Ladder(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+2)),
|
||||
])
|
||||
]))
|
||||
);
|
||||
|
@ -125,12 +125,12 @@ fn test_parser_ladder_between() {
|
|||
assert_eq!(
|
||||
TypeDict::new().parse("<A B~<C D>>"),
|
||||
Ok(TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::Ladder(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(3)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+3)),
|
||||
])
|
||||
])
|
||||
]))
|
||||
|
@ -156,48 +156,48 @@ fn test_parser_ladder_large() {
|
|||
Ok(
|
||||
TypeTerm::Ladder(vec![
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::Ladder(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(1)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+1)),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(3))
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+2)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+3))
|
||||
]),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(4)),
|
||||
TypeTerm::TypeID(TypeID::Fun(5))
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+4)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+5))
|
||||
]),
|
||||
TypeTerm::TypeID(TypeID::Fun(6)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+6)),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(7)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+7)),
|
||||
TypeTerm::Num(10),
|
||||
TypeTerm::TypeID(TypeID::Fun(8))
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+8))
|
||||
]),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::Ladder(vec![
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(9)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+9)),
|
||||
TypeTerm::Num(10)
|
||||
]),
|
||||
TypeTerm::TypeID(TypeID::Fun(10))
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+10))
|
||||
])
|
||||
])
|
||||
])
|
||||
]),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(11)),
|
||||
TypeTerm::TypeID(TypeID::Fun(10)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+11)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+10)),
|
||||
TypeTerm::Char(':')
|
||||
]),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(10))
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+10))
|
||||
]),
|
||||
TypeTerm::TypeID(TypeID::Fun(12)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+12)),
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(TypeID::Fun(0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(13))
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+0)),
|
||||
TypeTerm::TypeID(TypeID::Fun(SUGARID_LIMIT+13))
|
||||
])
|
||||
])
|
||||
)
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
use crate::dict::TypeDict;
|
||||
|
||||
#[test]
|
||||
fn test_param_normalize() {
|
||||
let mut dict = TypeDict::new();
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("A~B~C").expect("parse error"),
|
||||
dict.parse("A~B~C").expect("parse error").param_normalize(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<A B>~C").expect("parse error"),
|
||||
dict.parse("<A B>~C").expect("parse error").param_normalize(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<A B~C>").expect("parse error"),
|
||||
dict.parse("<A B>~<A C>").expect("parse error").param_normalize(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<A~Y B>").expect("parse error"),
|
||||
dict.parse("<A B>~<Y B>").expect("parse error").param_normalize(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<A B~C D~E>").expect("parse error"),
|
||||
dict.parse("<A B D>~<A C D>~<A C E>").expect("parse errror").param_normalize(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<A~X B~C D~E>").expect("parse error"),
|
||||
dict.parse("<A B D>~<A B~C E>~<X C E>").expect("parse errror").param_normalize(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<Seq <Digit 10>~Char>").expect("parse error"),
|
||||
dict.parse("<Seq <Digit 10>>~<Seq Char>").expect("parse errror").param_normalize(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<Seq Char> ~ <<ValueDelim '\\0'> Char> ~ <<ValueDelim '\\0'> Ascii~x86.UInt8>").expect("parse error").param_normalize(),
|
||||
dict.parse("<Seq~<ValueDelim '\\0'> Char~Ascii~x86.UInt8>").expect("parse error")
|
||||
);
|
||||
assert_eq!(
|
||||
dict.parse("<Seq Char~Ascii> ~ <<ValueDelim '\\0'> Char~Ascii> ~ <<ValueDelim '\\0'> x86.UInt8>").expect("parse error").param_normalize(),
|
||||
dict.parse("<Seq~<ValueDelim '\\0'> Char~Ascii~x86.UInt8>").expect("parse error")
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
dict.parse("<A~Y <B C~D~E> F H H>").expect("parse error"),
|
||||
dict.parse("<A <B C> F H H>
|
||||
~<A <B D> F H H>
|
||||
~<A~Y <B E> F H H>").expect("parse errror")
|
||||
.param_normalize(),
|
||||
);
|
||||
}
|
||||
|
10
src/test/sugar.rs
Normal file
10
src/test/sugar.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
|
||||
#[test]
|
||||
fn test_sugar() {
|
||||
|
||||
let mut dict = crate::TypeDict::new();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -13,8 +13,8 @@ fn test_unify(ts1: &str, ts2: &str, expect_unificator: bool) {
|
|||
dict.add_varname(String::from("V"));
|
||||
dict.add_varname(String::from("W"));
|
||||
|
||||
let mut t1 = dict.parse(ts1).unwrap();
|
||||
let mut t2 = dict.parse(ts2).unwrap();
|
||||
let mut t1 = dict.parse(ts1).unwrap().desugar();
|
||||
let mut t2 = dict.parse(ts2).unwrap().desugar();
|
||||
let σ = crate::unify( &t1, &t2 );
|
||||
|
||||
if expect_unificator {
|
||||
|
|
Loading…
Reference in a new issue