1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Scoping of parse structures to data structures with references.

pub mod pattern;
pub mod rterm;
mod symbol;
mod symbols;

pub use pattern::Pattern;
pub use rterm::RTerm;
pub use symbol::Symbol;
pub use symbols::Symbols;

/// Rewrite rules with strings as bound variable identifiers,
/// a top pattern (symbol application) as left-hand side, and
/// a shared term as right-hand side.
pub type Rule<'s> = crate::Rule<String, pattern::TopPattern<'s>, RTerm<'s>>;

/// The way we introduce a new name.
pub type Intro<'s> = crate::Intro<RTerm<'s>, RTerm<'s>>;

/// Signature-changing command.
pub type Command<'s, Id> = crate::Command<Id, Intro<'s>, Rule<'s>>;

pub type Term<'s> = crate::Term<Symbol<'s>, String, RTerm<'s>>;

use crate::error::{Error as KoError, ScopeError as Error};
use crate::parse::{self, parse};
use crate::stack::Stack;
use alloc::{string::String, string::ToString, vec::Vec};
use core::convert::TryFrom;
use pattern::TopPattern;

type Bound = Stack<String>;

impl parse::Symbol {
    fn scope<'s>(self, syms: &Symbols<'s>) -> Result<Symbol<'s>, Error> {
        syms.get(&self.path, &self.name)
            .ok_or_else(|| Error::UndeclaredSymbol(self.to_string()))
    }
}

impl parse::Term {
    fn scoper<'s>(self, syms: &Symbols<'s>, bnd: &mut Bound) -> Result<RTerm<'s>, Error> {
        Ok(RTerm::new(self.scopen(syms, bnd)?))
    }

    /// Scope an open preterm using supplied bound variables.
    fn scopen<'s>(self, syms: &Symbols<'s>, bnd: &mut Bound) -> Result<Term<'s>, Error> {
        match self {
            Self::Symb(sym) if sym.name == "_" => Err(Error::Underscore),
            Self::Symb(sym) if sym.path.is_empty() => {
                if sym.name == "Type" {
                    Ok(Term::Type)
                } else if let Some(idx) = bnd.iter().position(|id| *id == *sym.name) {
                    Ok(Term::BVar(idx))
                } else {
                    Ok(Term::Symb(sym.scope(syms)?))
                }
            }
            Self::Symb(sym) => Ok(Term::Symb(sym.scope(syms)?)),
            Self::Appl(head, tail) => {
                let tail: Result<_, _> = tail.into_iter().map(|tm| tm.scoper(syms, bnd)).collect();
                Ok(Term::Appl(head.scoper(syms, bnd)?, tail?))
            }
            Self::Prod(arg, tm) => {
                let arg = arg.map_ty_res(|ty| ty.scoper(syms, bnd))?;
                bnd.with_pushed(arg.id.to_string(), |bnd| {
                    Ok(Term::Prod(arg, tm.scoper(syms, bnd)?))
                })
            }
            Self::Abst(arg, tm) => {
                let arg = arg.map_ty_res(|o| o.map(|ty| ty.scoper(syms, bnd)).transpose())?;
                bnd.with_pushed(arg.id.to_string(), |bnd| {
                    Ok(Term::Abst(arg, tm.scoper(syms, bnd)?))
                })
            }
        }
    }

    /// Scope a closed term.
    ///
    /// ~~~
    /// # use kontroli::error::{Error, ScopeError};
    /// # use kontroli::parse::{self, parse};
    /// # use kontroli::scope::{Symbols, Term};
    /// let syms: Symbols = vec!["A"].into_iter().collect();
    /// let tm = parse::<parse::Term>(r"_ : A => _.\n")?.scope(&syms);
    /// assert_eq!(tm, Err(ScopeError::Underscore));
    /// # Ok::<_, Error>(())
    /// ~~~
    pub fn scope<'s>(self, syms: &Symbols<'s>) -> Result<Term<'s>, Error> {
        self.scopen(syms, &mut Stack::new())
    }
}

impl parse::Pattern {
    /// Scope an open prepattern using supplied bound variables.
    fn scopen<'s>(self, syms: &Symbols<'s>, mvar: &Bound) -> Result<Pattern<'s>, Error> {
        let Self(s, args) = self;

        let scope = |args: Vec<Self>| -> Result<_, _> {
            args.into_iter().map(|a| a.scopen(syms, mvar)).collect()
        };

        if s.path.is_empty() {
            if s.name == "_" {
                if !args.is_empty() {
                    return Err(Error::PatternArguments);
                }
                Ok(Pattern::Joker)
            } else if let Some(idx) = mvar.iter().position(|id| *id == *s.name) {
                if !args.is_empty() {
                    return Err(Error::PatternArguments);
                }
                Ok(Pattern::MVar(idx))
            } else {
                Ok(Pattern::Symb(s.scope(syms)?, scope(args)?))
            }
        } else if s.name == "_" {
            Err(Error::Underscore)
        } else {
            Ok(Pattern::Symb(s.scope(syms)?, scope(args)?))
        }
    }
}

impl parse::Rule {
    pub fn scope<'s>(self, syms: &Symbols<'s>) -> Result<Rule<'s>, Error> {
        let ctx: Vec<_> = self.ctx.into_iter().map(|arg| arg.id).collect();
        let mut ctxs = Stack::from(ctx.clone());
        let pre = parse::Pattern::try_from(self.lhs).map_err(|_| Error::NoPrepattern)?;
        let pat = pre.scopen(syms, &ctxs)?;
        let lhs = TopPattern::try_from(pat).map_err(|_| Error::NoTopPattern)?;
        let rhs = self.rhs.scoper(syms, &mut ctxs)?;
        Ok(Rule { ctx, lhs, rhs })
    }
}

impl parse::Intro {
    pub fn scope<'s>(self, syms: &Symbols<'s>) -> Result<Intro<'s>, Error> {
        let mut bnd = Stack::new();
        self.map_type_err(|tm| tm.scoper(syms, &mut bnd))?
            .map_term_err(|tm| tm.scoper(syms, &mut bnd))
    }
}

impl parse::Command {
    pub fn scope<'s>(self, syms: &Symbols<'s>) -> Result<Command<'s, String>, Error> {
        match self {
            Self::Intro(id, it) => Ok(Command::Intro(id, it.scope(syms)?)),
            Self::Rules(rules) => {
                let rules: Result<_, _> = rules.into_iter().map(|r| r.scope(syms)).collect();
                Ok(Command::Rules(rules?))
            }
        }
    }
}

impl<'s> Command<'s, alloc::string::String> {
    /// Parse a command and scope it. Used for testing.
    pub fn parse(i: &str, syms: &Symbols<'s>) -> Result<Self, KoError> {
        Ok(parse::<parse::Command>(i)?.scope(&syms)?)
    }
}

impl<'s> Term<'s> {
    /// Parse a term and scope it. Used for testing.
    pub fn parse(i: &str, syms: &Symbols<'s>) -> Result<Self, KoError> {
        Ok(parse::<parse::Term>(i)?.scope(&syms)?)
    }
}

impl<'s> Rule<'s> {
    /// Parse a rule and scope it. Used for testing.
    pub fn parse(i: &str, syms: &Symbols<'s>) -> Result<Self, KoError> {
        Ok(parse::<parse::Rule>(i)?.scope(&syms)?)
    }
}