This page requires javascript to work
  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
use voile_util::level::{Level, LevelType};
use voile_util::loc::*;
use voile_util::meta::MI;
use voile_util::tags::*;
use voile_util::uid::*;

pub type LabAbs = Labelled<Abs>;

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Abs {
    Type(Loc, Level),
    /// Local variable
    Var(Ident, UID, DBI),
    /// Global variable
    Ref(Ident, GI),
    /// Meta variable
    Meta(Ident, MI),
    /// Lift an expression many times
    Lift(Loc, LevelType, Box<Self>),
    /// Constructor call
    Cons(Ident),
    /// Record projection
    Proj(Loc, Box<Self>, Ident),
    /// Apply or Pipeline in surface
    App(Loc, Box<Self>, Plicit, Box<Self>),
    /// Dependent Type, `(a -> b -> c)` as `Dt(_, DtKind::Pi, _, _, a, Dt(_, DtKind::Pi, _, _, b, c))`
    Dt(Loc, PiSig, UID, Plicit, Box<Self>, Box<Self>),
    /// The first `Loc` is the syntax info of this whole lambda,
    /// while the second is about its parameter
    Lam(Loc, Ident, UID, Box<Self>),
    Pair(Loc, Box<Self>, Box<Self>),
    Fst(Loc, Box<Self>),
    Snd(Loc, Box<Self>),
    /// Row-polymorphic types, corresponds to [RowPoly](crate::syntax::surf::Expr::RowPoly)
    RowPoly(Loc, VarRec, Vec<LabAbs>, Option<Box<Self>>),
    /// Record literals
    Rec(Loc, Vec<LabAbs>, Option<Box<Self>>),
    /// Empty type eliminator,
    Whatever(Loc),
    /// Case-split expressions.
    CaseOr(Ident, Ident, UID, Box<Self>, Box<Self>),
    /// Row-polymorphic kinds, corresponds to [RowKind](crate::syntax::surf::Expr::RowKind)
    RowKind(Loc, VarRec, Vec<Ident>),
}

impl ToLoc for Abs {
    fn loc(&self) -> Loc {
        match self {
            Abs::Type(info, ..)
            | Abs::App(info, ..)
            | Abs::Dt(info, ..)
            | Abs::Pair(info, ..)
            | Abs::Fst(info, ..)
            | Abs::Snd(info, ..)
            | Abs::RowPoly(info, ..)
            | Abs::Rec(info, ..)
            | Abs::Proj(info, ..)
            | Abs::RowKind(info, ..)
            | Abs::Lift(info, ..)
            | Abs::Whatever(info)
            | Abs::Lam(info, ..) => (*info).clone(),
            Abs::CaseOr(ident, _, _, _, last) => merge_info(ident, &**last),
            Abs::Var(ident, ..) | Abs::Ref(ident, ..) | Abs::Meta(ident, ..) | Abs::Cons(ident) => {
                ident.loc.clone()
            }
        }
    }
}

impl Abs {
    pub fn dependent_type(
        info: Loc,
        kind: PiSig,
        name: UID,
        plicit: Plicit,
        a: Self,
        b: Self,
    ) -> Self {
        Abs::Dt(info, kind, name, plicit, Box::new(a), Box::new(b))
    }

    pub fn row_polymorphic_type(
        info: Loc,
        kind: VarRec,
        labels: Vec<LabAbs>,
        rest: Option<Self>,
    ) -> Self {
        Abs::RowPoly(info, kind, labels, rest.map(Box::new))
    }

    pub fn record(info: Loc, fields: Vec<LabAbs>, rest: Option<Self>) -> Self {
        Abs::Rec(info, fields, rest.map(Box::new))
    }

    pub fn app(info: Loc, function: Self, plicit: Plicit, argument: Self) -> Self {
        Abs::App(info, Box::new(function), plicit, Box::new(argument))
    }

    pub fn proj(info: Loc, record: Self, field: Ident) -> Self {
        Abs::Proj(info, Box::new(record), field)
    }

    pub fn fst(info: Loc, of: Self) -> Self {
        Abs::Fst(info, Box::new(of))
    }

    pub fn snd(info: Loc, of: Self) -> Self {
        Abs::Snd(info, Box::new(of))
    }

    pub fn lam(whole_info: Loc, param: Ident, name: UID, body: Self) -> Self {
        Abs::Lam(whole_info, param, name, Box::new(body))
    }

    pub fn pair(info: Loc, first: Self, second: Self) -> Self {
        Abs::Pair(info, Box::new(first), Box::new(second))
    }

    pub fn case_or(label: Ident, binding: Ident, uid: UID, clause: Self, or: Self) -> Self {
        Abs::CaseOr(label, binding, uid, Box::new(clause), Box::new(or))
    }

    pub fn lift(info: Loc, lift_count: LevelType, expr: Self) -> Self {
        Abs::Lift(info, lift_count, Box::new(expr))
    }

    pub fn pi(info: Loc, name: UID, plicit: Plicit, input: Self, output: Self) -> Self {
        Self::dependent_type(info, PiSig::Pi, name, plicit, input, output)
    }

    pub fn sig(info: Loc, name: UID, plicit: Plicit, first: Self, second: Self) -> Self {
        Self::dependent_type(info, PiSig::Sigma, name, plicit, first, second)
    }
}

/// Type signature and body implementation,
/// with abstract syntax.
#[derive(Debug, Clone)]
pub enum AbsDecl {
    /// Signature.
    Sign(Abs, GI),
    /// Function body without a signature.
    Decl(Abs),
    /// Function body with a signature.
    Impl(Abs, GI),
}

impl ToLoc for AbsDecl {
    fn loc(&self) -> Loc {
        use AbsDecl::*;
        match self {
            Sign(abs, ..) | Decl(abs) | Impl(abs, ..) => abs.loc(),
        }
    }
}