├── .circleci └── config.yml ├── .gitattributes ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── appveyor.yml └── src ├── main.rs ├── syntax.rs ├── syntax ├── abs.rs ├── elab.rs └── lexical.rs ├── type_check.rs └── type_check ├── context.rs ├── elaborate.rs └── pragma.rs /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | jobs: 4 | build: 5 | docker: 6 | - image: circleci/rust:latest 7 | 8 | steps: 9 | - checkout 10 | - restore_cache: 11 | key: project-cache 12 | - run: 13 | name: Install Nightly Compiler 14 | command: rustup install nightly 15 | - run: 16 | name: Check formatting 17 | command: | 18 | rustup component add rustfmt 19 | rustfmt --version 20 | cargo fmt -- --check 21 | - run: 22 | name: Nightly Build 23 | command: | 24 | rustup run nightly rustc --version --verbose 25 | rustup run nightly cargo --version --verbose 26 | rustup run nightly cargo build 27 | - run: 28 | name: Stable Build 29 | command: | 30 | rustup install stable 31 | rustup run stable rustc --version --verbose 32 | rustup run stable cargo --version --verbose 33 | rustup run stable cargo build 34 | - run: 35 | name: Test 36 | command: rustup run stable cargo test 37 | # - run: 38 | # name: Upload Coverage 39 | # command: ./scripts/codecov.sh 40 | - save_cache: 41 | key: project-cache 42 | paths: 43 | - "/usr/local/cargo/registry" 44 | - "~/.cargo" 45 | - "./target" 46 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | .vscode 5 | .idea -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "owo" 3 | version = "0.1.0" 4 | authors = ["ice1000 "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | #symmetric-interaction-calculus = { git = "https://github.com/owo-lang/Symmetric-Interaction-Calculus" } 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2019 Tesla Ice Zhang 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## OwO 2 | 3 | [![CircleCI][Circle]][CircleIMG] 4 | [![AppVeyor][AV]][AVIMG] 5 | [![Join the chat at https://gitter.im/owo-rfcs/Lobby][Gitter]][GitterIMG] 6 | 7 | [AV]: https://ci.appveyor.com/api/projects/status/tuhny5tndmtv23be/branch/master?svg=true 8 | [AVIMG]: https://ci.appveyor.com/project/ice1000/owo/branch/master 9 | [Circle]: https://circleci.com/gh/owo-lang/OwO.svg?style=svg 10 | [CircleIMG]: https://circleci.com/gh/owo-lang/OwO 11 | [Gitter]: https://badges.gitter.im/owo-rfcs/Lobby.svg 12 | [GitterIMG]: https://gitter.im/owo-rfcs/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge 13 | 14 | The compiler of the OwO programming language. WIP. 15 | There is an [unfinished Haskell version][Hs] left abandoned. 16 | 17 | OwO is inspired mainly by Agda, MLPolyR and Haskell. 18 | It's a functional programming language and a proof assistant. 19 | 20 | [Hs]: https://github.com/owo-lang/OwO-Haskell-Deprecated 21 | 22 | + Website (WIP) [repo](https://github.com/owo-lang/owo-lang.github.io) 23 | 24 | ## How to pronouns OwO 25 | 26 | /'əʊwəʊ/ 27 | 28 | ## License 29 | 30 | Apache-2.0 31 | 32 | ```text 33 | ___ ___ 34 | / _ \ / _ \ 35 | | | | |_ _| | | | 36 | | | | \ \ /\ / / | | | 37 | | |_| |\ V V /| |_| | 38 | \___/ \_/\_/ \___/ 39 | (What's this?) 40 | ``` 41 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Template: https://github.com/starkat99/appveyor-rust 2 | 3 | os: Visual Studio 2017 4 | 5 | ## Build Matrix ## 6 | 7 | # You may use the `cargoflags` and `RUSTFLAGS` variables to set additional flags 8 | # for cargo commands and rustc, respectively. 9 | environment: 10 | matrix: 11 | 12 | ### MSVC Toolchains ### 13 | 14 | # Stable 64-bit MSVC 15 | - channel: stable 16 | target: x86_64-pc-windows-msvc 17 | # Stable 32-bit MSVC 18 | - channel: stable 19 | target: i686-pc-windows-msvc 20 | # Beta 64-bit MSVC 21 | - channel: beta 22 | target: x86_64-pc-windows-msvc 23 | # Beta 32-bit MSVC 24 | - channel: beta 25 | target: i686-pc-windows-msvc 26 | # Nightly 64-bit MSVC 27 | - channel: nightly 28 | target: x86_64-pc-windows-msvc 29 | #cargoflags: --features "unstable" 30 | # Nightly 32-bit MSVC 31 | - channel: nightly 32 | target: i686-pc-windows-msvc 33 | #cargoflags: --features "unstable" 34 | 35 | ### GNU Toolchains ### 36 | 37 | # Stable 64-bit GNU 38 | - channel: stable 39 | target: x86_64-pc-windows-gnu 40 | # Stable 32-bit GNU 41 | - channel: stable 42 | target: i686-pc-windows-gnu 43 | # Beta 64-bit GNU 44 | - channel: beta 45 | target: x86_64-pc-windows-gnu 46 | # Beta 32-bit GNU 47 | - channel: beta 48 | target: i686-pc-windows-gnu 49 | # Nightly 64-bit GNU 50 | - channel: nightly 51 | target: x86_64-pc-windows-gnu 52 | #cargoflags: --features "unstable" 53 | # Nightly 32-bit GNU 54 | - channel: nightly 55 | target: i686-pc-windows-gnu 56 | #cargoflags: --features "unstable" 57 | 58 | matrix: 59 | allow_failures: 60 | - channel: nightly 61 | - channel: beta 62 | 63 | install: 64 | - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe 65 | - rustup-init -yv --default-toolchain %channel% --default-host %target% 66 | - set PATH=%PATH%;%USERPROFILE%\.cargo\bin 67 | - rustc -vV 68 | - cargo -vV 69 | 70 | build: off 71 | 72 | test_script: 73 | - cargo test --verbose %cargoflags% 74 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | pub mod syntax; 2 | pub mod type_check; 3 | 4 | fn main() { 5 | println!("Hello, OwO!"); 6 | } 7 | -------------------------------------------------------------------------------- /src/syntax.rs: -------------------------------------------------------------------------------- 1 | // Concrete Syntax Tree, Abstract Syntax Tree, Elaborated Syntax Tree 2 | // Lexer/Parser 3 | // Translation, Desugaring, etc. 4 | 5 | pub mod abs; 6 | pub mod elab; 7 | pub mod lexical; 8 | -------------------------------------------------------------------------------- /src/syntax/abs.rs: -------------------------------------------------------------------------------- 1 | // Abstract syntax, desugared 2 | 3 | use super::lexical::{Locatable, Location, Name}; 4 | 5 | // TODO: Instance argument 6 | #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Ord, Eq)] 7 | pub enum ParamVisibility { 8 | Explicit, 9 | Implicit, 10 | } 11 | 12 | #[derive(Clone)] 13 | pub enum Binder { 14 | /// Lambda, optionally typed 15 | Lambda(Option), 16 | /// Pi type, parameter can be implicit/explicit 17 | Pi(Option, ParamVisibility), 18 | /// Auto-generated type argument 19 | Generalized, 20 | } 21 | 22 | #[derive(Clone)] 23 | pub enum AstTerm { 24 | Bind { 25 | name: Name, 26 | binder: Box, 27 | body: Box, 28 | }, 29 | /// Application, applying a term on another term. 30 | /// Can be implicit/instance application so there's visibility 31 | App { 32 | func: Box, 33 | arg: Box, 34 | app_visibility: ParamVisibility, 35 | }, 36 | /// Meta variable 37 | Meta { 38 | name: Name, 39 | }, 40 | /// Named reference 41 | Ref { 42 | name: Name, 43 | }, 44 | // TODO level 45 | Type, 46 | } 47 | 48 | impl Locatable for AstTerm { 49 | fn location(&self) -> Location { 50 | use self::AstTerm::*; 51 | match self { 52 | Meta { name } => name.location.clone(), 53 | Ref { name } => name.location.clone(), 54 | _ => unimplemented!(), 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/syntax/elab.rs: -------------------------------------------------------------------------------- 1 | // Elaborated syntax, meta variables are allowed 2 | 3 | use crate::syntax::abs::ParamVisibility; 4 | use crate::syntax::lexical::{Locatable, Location, Name}; 5 | 6 | /// Constrains on a meta var. Currently I decide to only make 7 | /// a simple trivial meta solver. 8 | #[derive(Clone, Debug)] 9 | pub enum MetaConstraint { 10 | IsTypeOf(Term), 11 | IsInstanceOf(Term), 12 | IsEquivalentTo(Term), 13 | } 14 | 15 | /// Core language term 16 | #[derive(Clone, Debug)] 17 | pub enum Term { 18 | App { 19 | func: Box, 20 | arg: Box, 21 | }, 22 | Bind { 23 | arg_type: Box, 24 | arg_visibility: ParamVisibility, 25 | body: Box, 26 | }, 27 | /// De-Bruijn Index 28 | Var { 29 | index: usize, 30 | }, 31 | /// Global reference 32 | Ref { 33 | name: Name, 34 | }, 35 | // TODO level 36 | Type, 37 | Meta { 38 | name: Name, 39 | constraints: Vec>, 40 | }, 41 | } 42 | 43 | impl Locatable for Term { 44 | fn location(&self) -> Location { 45 | use self::Term::*; 46 | match self { 47 | Meta { name, constraints } => name.location.clone(), 48 | Ref { name } => name.location.clone(), 49 | _ => unimplemented!(), 50 | } 51 | } 52 | } 53 | 54 | impl Term { 55 | pub fn fresh_meta(name: Name) -> Term { 56 | Term::Meta { 57 | name, 58 | constraints: vec![], 59 | } 60 | } 61 | 62 | pub fn anonymous_meta(location: Location) -> Term { 63 | Term::fresh_meta(Name { 64 | text_name: None, 65 | location, 66 | }) 67 | } 68 | } 69 | 70 | pub struct Def { 71 | /// Type 72 | set: Term, 73 | /// Body 74 | term: Term, 75 | } 76 | -------------------------------------------------------------------------------- /src/syntax/lexical.rs: -------------------------------------------------------------------------------- 1 | // Position, Location, Names 2 | 3 | use std::fmt::{Debug, Error, Formatter}; 4 | 5 | #[derive(Clone, Hash)] 6 | pub struct Position { 7 | /// Column number, starts from 1 8 | pub line: u32, 9 | /// Line number, starts from 1 10 | pub column: u32, 11 | /// Absolute position, starts from 0 12 | pub position: u32, 13 | } 14 | 15 | #[derive(Clone, Hash, Default)] 16 | pub struct Location { 17 | pub file_name: String, 18 | pub start: Position, 19 | pub end: Position, 20 | } 21 | 22 | pub trait Locatable { 23 | fn location(&self) -> Location; 24 | } 25 | 26 | #[derive(Clone, Hash)] 27 | pub struct Name { 28 | pub text_name: Option, 29 | pub location: Location, 30 | } 31 | 32 | impl Name { 33 | pub fn pretty_text(&self) -> String { 34 | self.text_name.clone().unwrap_or(String::from("anonymous")) 35 | } 36 | } 37 | 38 | impl Debug for Name { 39 | fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { 40 | f.write_str(self.pretty_text().as_str()) 41 | } 42 | } 43 | 44 | impl Default for Position { 45 | fn default() -> Self { 46 | Position { 47 | line: 1, 48 | column: 1, 49 | position: 0, 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/type_check.rs: -------------------------------------------------------------------------------- 1 | // TypeChecking module 2 | 3 | pub mod context; 4 | pub mod elaborate; 5 | pub mod pragma; 6 | -------------------------------------------------------------------------------- /src/type_check/context.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | 3 | use crate::syntax::lexical::*; 4 | use crate::type_check::pragma::Pragma; 5 | 6 | pub type NameKey = String; 7 | pub type Stack = Vec; 8 | 9 | /// Type-checking state 10 | pub struct TCState { 11 | pub symbol_table: HashMap>, 12 | /// Name and value. Abstracted value is None, "let"ed value is Some(actual) 13 | pub local_vars: Stack<(NameKey, Option)>, 14 | pub pragmas: Vec, 15 | /// Non-fatal errors 16 | pub warnings: Vec, 17 | } 18 | 19 | /// In this way, [`T`] no longer need to implement [`Default`]. 20 | impl Default for TCState { 21 | fn default() -> Self { 22 | TCState { 23 | local_vars: Default::default(), 24 | pragmas: Default::default(), 25 | symbol_table: Default::default(), 26 | warnings: Default::default(), 27 | } 28 | } 29 | } 30 | 31 | /// Type-checking error 32 | #[derive(Clone)] 33 | pub enum TCError { 34 | /// Cannot find definition of some variable 35 | UnresolvedReference(Name), 36 | /// Invalid application 37 | IncorrectApplication(Location, String), 38 | /// Don't support application on a meta 39 | ApplicationOnMeta(Location, String), 40 | /// Something is not a type 41 | InvalidType, 42 | } 43 | 44 | pub type TCResult = Result; 45 | -------------------------------------------------------------------------------- /src/type_check/elaborate.rs: -------------------------------------------------------------------------------- 1 | use crate::syntax::abs::ParamVisibility::*; 2 | use crate::syntax::abs::{AstTerm, Binder}; 3 | use crate::syntax::elab::{Def, Term}; 4 | use crate::syntax::lexical::Locatable; 5 | use crate::type_check::context::TCError::*; 6 | use crate::type_check::context::{TCError, TCResult, TCState}; 7 | 8 | /// If something is a type 9 | pub fn is_type(state: &TCState, term: &AstTerm) -> TCResult<()> { 10 | match term { 11 | AstTerm::Bind { binder, name, body } => match **binder { 12 | Binder::Pi(_, _) => Ok(()), 13 | _ => Err(InvalidType), 14 | }, 15 | AstTerm::Type => Ok(()), 16 | _ => Err(InvalidType), 17 | } 18 | } 19 | 20 | pub fn is_instance(state: &TCState, term: &Term, expected_type: &Term) -> TCResult<()> { 21 | // TODO 22 | Ok(()) 23 | } 24 | 25 | pub fn elaborate(state: &mut TCState, term: &AstTerm) -> TCResult { 26 | use crate::syntax::abs::Binder::*; 27 | match term { 28 | AstTerm::Type => Ok(Term::Type), 29 | AstTerm::Meta { name } => Ok(Term::fresh_meta(name.clone())), 30 | AstTerm::App { 31 | func, 32 | arg, 33 | app_visibility, 34 | } => { 35 | let func = elaborate(state, func)?; 36 | let (arg_visibility, arg_type, body) = match &func { 37 | Term::Bind { 38 | arg_type, 39 | arg_visibility, 40 | body, 41 | } => (*arg_visibility, arg_type, body), 42 | // TODO: apply on meta? 43 | _ => { 44 | return Err(IncorrectApplication( 45 | term.location(), 46 | String::from("Cannot apply on a non-function"), 47 | )); 48 | } 49 | }; 50 | match (app_visibility, arg_visibility) { 51 | (Implicit, Explicit) => explicitly_apply_on_implicit(state, arg, &func, body), 52 | (Implicit, Implicit) | (Explicit, Explicit) => { 53 | let arg = elaborate(state, arg)?; 54 | is_instance(state, &arg, arg_type).map(|()| Term::App { 55 | arg: Box::new(arg), 56 | func: Box::new(func), 57 | }) 58 | } 59 | (Explicit, Implicit) => Err(IncorrectApplication( 60 | term.location(), 61 | String::from("Cannot implicitly apply on an explicit parameter"), 62 | )), 63 | } 64 | } 65 | AstTerm::Ref { name } => { 66 | if let Some(index) = state 67 | .local_vars 68 | .iter() 69 | .position(|(def_name, _)| Some(def_name) == name.text_name.as_ref()) 70 | { 71 | Ok(Term::Var { index }) 72 | } else { 73 | // TODO: global reference 74 | Err(UnresolvedReference(name.clone())) 75 | } 76 | } 77 | AstTerm::Bind { name, binder, body } => { 78 | let mut arg_type = Term::anonymous_meta(name.location.clone()); 79 | let mut visibility = Explicit; 80 | if let Some(name) = name.text_name.clone() { 81 | match *binder.clone() { 82 | Pi(Some(term), bind_visibility) => { 83 | visibility = bind_visibility; 84 | arg_type = elaborate(state, &term)?; 85 | } 86 | Pi(None, bind_visibility) => { 87 | visibility = bind_visibility; 88 | } 89 | Lambda(Some(bind_type)) => { 90 | arg_type = elaborate(state, &bind_type)?; 91 | } 92 | Lambda(None) => {} 93 | Generalized => {} 94 | } 95 | state.local_vars.push((name.clone(), None)); 96 | } 97 | let body = elaborate(state, body)?; 98 | if name.text_name.is_some() { 99 | state.local_vars.pop().unwrap(); 100 | } 101 | Ok(Term::Bind { 102 | body: Box::new(body), 103 | arg_visibility: Explicit, 104 | arg_type: Box::new(arg_type), 105 | }) 106 | } 107 | } 108 | } 109 | 110 | /// A very long part of [`to_core`], extracted 111 | fn explicitly_apply_on_implicit( 112 | state: &mut TCState, 113 | arg: &Box, 114 | func: &Term, 115 | body: &Box, 116 | ) -> TCResult { 117 | let arg = elaborate(state, arg)?; 118 | let mut new_func = Term::App { 119 | func: Box::new(func.clone()), 120 | arg: Box::new(Term::anonymous_meta(Default::default())), 121 | }; 122 | loop { 123 | match *body.clone() { 124 | Term::Bind { 125 | arg_visibility: Explicit, 126 | arg_type, 127 | body, 128 | } => { 129 | new_func = Term::App { 130 | func: Box::new(new_func), 131 | arg: Box::new(arg), 132 | }; 133 | break; 134 | } 135 | Term::Bind { 136 | arg_visibility: Implicit, 137 | arg_type, 138 | body, 139 | } => { 140 | new_func = Term::App { 141 | func: Box::new(new_func), 142 | arg: Box::new(Term::anonymous_meta(arg.location())), 143 | }; 144 | } 145 | _ => { 146 | return Err(IncorrectApplication( 147 | arg.location(), 148 | String::from("Cannot apply on a non-function"), 149 | )); 150 | } 151 | } 152 | } 153 | Ok(new_func) 154 | } 155 | 156 | mod tests { 157 | use crate::syntax::abs::AstTerm::{App, Bind, Meta, Ref}; 158 | use crate::syntax::abs::Binder::Lambda; 159 | use crate::syntax::abs::ParamVisibility::*; 160 | use crate::syntax::lexical::Name; 161 | use crate::type_check::context::TCError::*; 162 | use crate::type_check::context::TCState; 163 | 164 | use super::elaborate; 165 | 166 | #[test] 167 | #[rustfmt::skip] 168 | fn app_on_bind() { 169 | let text_name = Some(String::from("name")); 170 | let name = Name { text_name, location: Default::default() }; 171 | let reference = Ref { name: name.clone() }; 172 | let arg = Meta { name: name.clone() }; 173 | let func = Bind { name, binder: Box::new(Lambda(None)), body: Box::new(reference) }; 174 | let app = App { arg: Box::new(arg), func: Box::new(func), app_visibility: Explicit }; 175 | let term = elaborate(&mut TCState::default(), &app); 176 | assert_eq!(term.is_ok(), true); 177 | } 178 | 179 | #[test] 180 | #[rustfmt::skip] 181 | fn unresolved_reference() { 182 | let text_name = Some(String::from("name")); 183 | let name = Name { text_name, location: Default::default() }; 184 | let wrong_name = Name { text_name: Some(String::from("a")), location: Default::default() }; 185 | let reference = Ref { name: wrong_name }; 186 | let arg = Meta { name: name.clone() }; 187 | let func = Bind { name, binder: Box::new(Lambda(None)), body: Box::new(reference) }; 188 | let app = App { arg: Box::new(arg), func: Box::new(func), app_visibility: Explicit }; 189 | let term = elaborate(&mut TCState::default(), &app); 190 | match term { 191 | Err(UnresolvedReference(_)) => {} 192 | _ => panic!("test failed") 193 | }; 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /src/type_check/pragma.rs: -------------------------------------------------------------------------------- 1 | #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] 2 | pub enum Pragma { 3 | PositivityCheck, 4 | } 5 | --------------------------------------------------------------------------------