qleisli 0.2.2

Experimental quantum language with linear ownership, exact contracts and independent finite IR verification
Documentation
//! Syntax tree for the provisional `.qli` surface grammar.
//!
//! Every span is a half-open UTF-8 byte range in the original source. The
//! parser deliberately records syntax only; an AST is not a type or resource
//! safety certificate.

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Span {
    pub start: usize,
    pub end: usize,
}

impl Span {
    pub const fn new(start: usize, end: usize) -> Self {
        Self { start, end }
    }

    pub const fn cover(self, other: Self) -> Self {
        Self::new(self.start, other.end)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ident {
    pub text: String,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Module {
    pub uses: Vec<UseDecl>,
    pub decls: Vec<Decl>,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UseDecl {
    /// The final segment is the imported name; preceding segments name its module.
    pub path: Vec<Ident>,
    pub span: Span,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FnKind {
    /// A finite mathematical target, never a callable runtime function.
    Meaning,
    Basis,
    Iso,
    Unitary,
    Observe,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Decl {
    pub public: bool,
    pub kind: FnKind,
    pub name: Ident,
    pub static_params: Vec<StaticParam>,
    pub requires: Vec<AccessConstraint>,
    pub params: Vec<Param>,
    pub return_type: Type,
    pub body: FnBody,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Param {
    /// Basis functions accept name/wildcard/product patterns. Ordinary
    /// function parameters remain names. Tuple nodes retain their immediate arity.
    pub pattern: Pattern,
    pub ty: Type,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Type {
    pub kind: TypeKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TypeKind {
    Unit,
    Bit,
    CBit,
    Q(Box<Type>),
    Tuple(Vec<Type>),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FnBody {
    Meaning { permutation: bool, function: Ident },
    Basis(BasisExpr),
    Quantum(Block),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Block {
    pub statements: Vec<Stmt>,
    pub result: Box<Expr>,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Stmt {
    pub kind: StmtKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StmtKind {
    Let { pattern: Pattern, value: Expr },
    Expr(Expr),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Pattern {
    pub kind: PatternKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PatternKind {
    Name(Ident),
    Wildcard,
    Tuple(Vec<Pattern>),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Expr {
    pub kind: ExprKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExprKind {
    ApplyContract {
        implementation: Ident,
        specification: Ident,
        input: Box<Expr>,
    },
    Adjoint {
        function: Ident,
        input: Box<Expr>,
    },
    RepeatStatic {
        count: u16,
        function: Ident,
        input: Box<Expr>,
    },
    QuantumIf {
        control: Box<Expr>,
        target: Box<Expr>,
        zero: Ident,
        one: Ident,
    },
    Name(Ident),
    CBit(bool),
    Unit,
    Tuple(Vec<Expr>),
    Not(Box<Expr>),
    And(Box<Expr>, Box<Expr>),
    Xor(Box<Expr>, Box<Expr>),
    Call {
        callee: Ident,
        static_args: Vec<StaticOp>,
        args: Vec<Expr>,
    },
    If {
        condition: Box<Expr>,
        then_branch: Block,
        else_branch: Block,
    },
    CoherentLift {
        binder: Pattern,
        input: Box<Expr>,
        basis: BasisExpr,
    },
    WithComputed {
        source: Box<Expr>,
        function: Ident,
        binder: Ident,
        body: Block,
    },
    CertifiedComputed {
        source: Box<Expr>,
        function: Ident,
        // Keep the expression enum small: the parser bounds nesting, and
        // adding several inline identifiers would inflate every stack frame.
        logical: Box<Ident>,
        data_binder: Box<Ident>,
        ancilla_binder: Box<Ident>,
        body: Block,
    },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Access {
    Apply,
    Adjoint,
    Controlled,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AccessConstraint {
    pub access: Access,
    pub name: Ident,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StaticParam {
    pub name: Ident,
    pub basis: Type,
    pub meaning: Option<Ident>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StaticOp {
    pub kind: StaticOpKind,
    pub span: Span,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StaticOpKind {
    Name(Ident),
    Bind {
        implementation: Ident,
        meaning: Ident,
    },
    Inverse(Box<StaticOp>),
    Then(Box<StaticOp>, Box<StaticOp>),
    Tensor(Box<StaticOp>, Box<StaticOp>),
    Controlled(Box<StaticOp>),
    Repeat(u16, Box<StaticOp>),
    Conjugate(Box<StaticOp>, Box<StaticOp>),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BasisExpr {
    pub kind: BasisExprKind,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BasisExprKind {
    Name(Ident),
    Bit(bool),
    Unit,
    Tuple(Vec<BasisExpr>),
    Call { callee: Ident, args: Vec<BasisExpr> },
    Not(Box<BasisExpr>),
    Xor(Box<BasisExpr>, Box<BasisExpr>),
    And(Box<BasisExpr>, Box<BasisExpr>),
}