Compare commits
4 commits
6a5c27cfba
...
a52f38dadf
Author | SHA1 | Date | |
---|---|---|---|
a52f38dadf | |||
f09ee8c7c0 | |||
a75a7a56a0 | |||
658134d56a |
6 changed files with 58 additions and 199 deletions
57
README.md
57
README.md
|
@ -5,6 +5,8 @@ Rust Implementation of Ladder-Types (parsing, unification, rewriting, etc)
|
||||||
|
|
||||||
## Ladder Types
|
## Ladder Types
|
||||||
|
|
||||||
|
### Motivation
|
||||||
|
|
||||||
In order to implement complex datastructures and algorithms, usually
|
In order to implement complex datastructures and algorithms, usually
|
||||||
many layers of abstraction are built ontop of each other.
|
many layers of abstraction are built ontop of each other.
|
||||||
Consequently higher-level data types are encoded into lower-level data
|
Consequently higher-level data types are encoded into lower-level data
|
||||||
|
@ -57,6 +59,48 @@ this:
|
||||||
1696093021:1696093039:1528324679:1539892301:1638141920:1688010253
|
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
|
## How to use this crate
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
@ -73,6 +117,19 @@ 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
|
## License
|
||||||
[GPLv3](COPYING)
|
[GPLv3](COPYING)
|
||||||
|
|
|
@ -7,7 +7,6 @@ pub mod parser;
|
||||||
pub mod unparser;
|
pub mod unparser;
|
||||||
pub mod curry;
|
pub mod curry;
|
||||||
pub mod lnf;
|
pub mod lnf;
|
||||||
pub mod pnf;
|
|
||||||
pub mod subtype;
|
pub mod subtype;
|
||||||
pub mod unification;
|
pub mod unification;
|
||||||
pub mod morphism;
|
pub mod morphism;
|
||||||
|
|
113
src/pnf.rs
113
src/pnf.rs
|
@ -1,113 +0,0 @@
|
||||||
use crate::term::TypeTerm;
|
|
||||||
|
|
||||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
|
||||||
|
|
||||||
impl TypeTerm {
|
|
||||||
/// transmute type into Parameter-Normal-Form (PNF)
|
|
||||||
///
|
|
||||||
/// Example:
|
|
||||||
/// ```ignore
|
|
||||||
/// <Seq <Digit 10>>~<Seq Char>
|
|
||||||
/// ⇒ <Seq <Digit 10>~Char>
|
|
||||||
/// ```
|
|
||||||
pub fn param_normalize(self) -> Self {
|
|
||||||
match self {
|
|
||||||
TypeTerm::Ladder(mut rungs) => {
|
|
||||||
if rungs.len() > 0 {
|
|
||||||
// normalize all rungs separately
|
|
||||||
for r in rungs.iter_mut() {
|
|
||||||
*r = r.clone().param_normalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
// take top-rung
|
|
||||||
match rungs.remove(0) {
|
|
||||||
TypeTerm::App(params_top) => {
|
|
||||||
let mut params_ladders = Vec::new();
|
|
||||||
let mut tail : Vec<TypeTerm> = Vec::new();
|
|
||||||
|
|
||||||
// append all other rungs to ladders inside
|
|
||||||
// the application
|
|
||||||
for p in params_top {
|
|
||||||
params_ladders.push(vec![ p ]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for r in rungs {
|
|
||||||
match r {
|
|
||||||
TypeTerm::App(mut params_rung) => {
|
|
||||||
if params_rung.len() > 0 {
|
|
||||||
let mut first_param = params_rung.remove(0);
|
|
||||||
|
|
||||||
if first_param == params_ladders[0][0] {
|
|
||||||
for (l, p) in params_ladders.iter_mut().skip(1).zip(params_rung) {
|
|
||||||
l.push(p.param_normalize());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
params_rung.insert(0, first_param);
|
|
||||||
tail.push(TypeTerm::App(params_rung));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TypeTerm::Ladder(mut rs) => {
|
|
||||||
for r in rs {
|
|
||||||
tail.push(r.param_normalize());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
atomic => {
|
|
||||||
tail.push(atomic);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let head = TypeTerm::App(
|
|
||||||
params_ladders.into_iter()
|
|
||||||
.map(
|
|
||||||
|mut l| {
|
|
||||||
l.dedup();
|
|
||||||
match l.len() {
|
|
||||||
0 => TypeTerm::unit(),
|
|
||||||
1 => l.remove(0),
|
|
||||||
_ => TypeTerm::Ladder(l).param_normalize()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.collect()
|
|
||||||
);
|
|
||||||
|
|
||||||
if tail.len() > 0 {
|
|
||||||
tail.insert(0, head);
|
|
||||||
TypeTerm::Ladder(tail)
|
|
||||||
} else {
|
|
||||||
head
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TypeTerm::Ladder(mut r) => {
|
|
||||||
r.append(&mut rungs);
|
|
||||||
TypeTerm::Ladder(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
atomic => {
|
|
||||||
rungs.insert(0, atomic);
|
|
||||||
TypeTerm::Ladder(rungs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
TypeTerm::unit()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TypeTerm::App(params) => {
|
|
||||||
TypeTerm::App(
|
|
||||||
params.into_iter()
|
|
||||||
.map(|p| p.param_normalize())
|
|
||||||
.collect())
|
|
||||||
}
|
|
||||||
|
|
||||||
atomic => atomic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
|
|
@ -2,49 +2,7 @@ use crate::term::TypeTerm;
|
||||||
|
|
||||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
|
||||||
|
|
||||||
impl TypeTerm {
|
impl TypeTerm {
|
||||||
pub fn find_semantic_subtype_matches(&self, expected_type: &TypeTerm)
|
|
||||||
-> Option<(TypeTerm, TypeTerm, TypeTerm)>
|
|
||||||
{
|
|
||||||
let provided_lnf = self.clone().get_lnf_vec();
|
|
||||||
let expected_lnf = expected_type.clone().get_lnf_vec();
|
|
||||||
|
|
||||||
for i in 0..provided_lnf.len() {
|
|
||||||
if provided_lnf[i] == expected_lnf[0] {
|
|
||||||
// found first match.
|
|
||||||
// now find first mismatch.
|
|
||||||
for j in i..usize::min(provided_lnf.len(), i+expected_lnf.len()) {
|
|
||||||
if provided_lnf[j] != expected_lnf[ j-i ] {
|
|
||||||
|
|
||||||
eprintln!("found match at {}, mismatch at {}", i, j);
|
|
||||||
let syntactic_subladder = TypeTerm::Ladder( provided_lnf[ 0 .. j ].into_iter().cloned().collect() );
|
|
||||||
let provided_reprladder = TypeTerm::Ladder( provided_lnf[ j .. ].into_iter().cloned().collect() );
|
|
||||||
let expected_reprladder = TypeTerm::Ladder( expected_lnf[ j-i .. ].into_iter().cloned().collect() );
|
|
||||||
return Some((syntactic_subladder, provided_reprladder, expected_reprladder));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eprintln!("only syntactic subtype");
|
|
||||||
|
|
||||||
// syntactic subtype
|
|
||||||
let n = {
|
|
||||||
if provided_lnf.len() + i < expected_lnf.len() {
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
2
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let syntactic_subladder = TypeTerm::Ladder( provided_lnf[ 0 .. provided_lnf.len()-1 ].into_iter().cloned().collect() );
|
|
||||||
let provided_reprladder = TypeTerm::Ladder( provided_lnf[ provided_lnf.len()-n .. ].into_iter().cloned().collect() );
|
|
||||||
let expected_reprladder = TypeTerm::Ladder( expected_lnf[ provided_lnf.len()-n-i .. ].into_iter().cloned().collect() );
|
|
||||||
return Some((syntactic_subladder, provided_reprladder, expected_reprladder));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns ladder-step of first match and provided representation-type
|
// returns ladder-step of first match and provided representation-type
|
||||||
pub fn is_semantic_subtype_of(&self, expected_type: &TypeTerm) -> Option<(usize, TypeTerm)> {
|
pub fn is_semantic_subtype_of(&self, expected_type: &TypeTerm) -> Option<(usize, TypeTerm)> {
|
||||||
let provided_lnf = self.clone().get_lnf_vec();
|
let provided_lnf = self.clone().get_lnf_vec();
|
||||||
|
|
|
@ -3,7 +3,6 @@ pub mod lexer;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
pub mod curry;
|
pub mod curry;
|
||||||
pub mod lnf;
|
pub mod lnf;
|
||||||
pub mod pnf;
|
|
||||||
pub mod subtype;
|
pub mod subtype;
|
||||||
pub mod substitution;
|
pub mod substitution;
|
||||||
pub mod unification;
|
pub mod unification;
|
||||||
|
|
|
@ -1,41 +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 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("<Seq <Digit 10>~Char>").expect("parse error"),
|
|
||||||
dict.parse("<Seq <Digit 10>>~<Seq Char>").expect("parse errror").param_normalize(),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
dict.parse("<A <B C~D~E> F~G H H>").expect("parse error"),
|
|
||||||
dict.parse("<A <B C> F H H>
|
|
||||||
~<A <B D> F H H>
|
|
||||||
~<A <B E> F H H>
|
|
||||||
~<A <B E> G H H>").expect("parse errror")
|
|
||||||
.param_normalize(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue