use crate::{PrimitiveOp, QuantumLevel, VerificationDomain, ViolationKind};
mod sealed {
pub trait Sealed {}
impl Sealed for super::GroundedCoord {}
impl<const N: usize> Sealed for super::GroundedTuple<N> {}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::large_enum_variant, dead_code)]
pub(crate) enum DatumInner {
Q0([u8; 1]),
Q1([u8; 2]),
Q3([u8; 4]),
Q7([u8; 8]),
Q511([u8; 512]),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Datum {
inner: DatumInner,
}
impl Datum {
#[inline]
#[must_use]
pub const fn level(&self) -> QuantumLevel {
match self.inner {
DatumInner::Q0(_) => QuantumLevel::Q0,
DatumInner::Q1(_) => QuantumLevel::Q1,
DatumInner::Q3(_) => QuantumLevel::new(3),
DatumInner::Q7(_) => QuantumLevel::new(7),
DatumInner::Q511(_) => QuantumLevel::new(511),
}
}
#[inline]
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
match &self.inner {
DatumInner::Q0(b) => b,
DatumInner::Q1(b) => b,
DatumInner::Q3(b) => b,
DatumInner::Q7(b) => b,
DatumInner::Q511(b) => b,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::large_enum_variant, dead_code)]
pub(crate) enum GroundedCoordInner {
Q0([u8; 1]),
Q1([u8; 2]),
Q3([u8; 4]),
Q7([u8; 8]),
Q511([u8; 512]),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroundedCoord {
pub(crate) inner: GroundedCoordInner,
}
impl GroundedCoord {
#[inline]
#[must_use]
pub const fn q0(value: u8) -> Self {
Self {
inner: GroundedCoordInner::Q0([value]),
}
}
#[inline]
#[must_use]
pub const fn q1(value: u16) -> Self {
Self {
inner: GroundedCoordInner::Q1(value.to_le_bytes()),
}
}
#[inline]
#[must_use]
pub const fn q3(value: u32) -> Self {
Self {
inner: GroundedCoordInner::Q3(value.to_le_bytes()),
}
}
#[inline]
#[must_use]
pub const fn q7(value: u64) -> Self {
Self {
inner: GroundedCoordInner::Q7(value.to_le_bytes()),
}
}
#[inline]
#[must_use]
pub const fn q511(bytes: [u8; 512]) -> Self {
Self {
inner: GroundedCoordInner::Q511(bytes),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroundedTuple<const N: usize> {
pub(crate) coords: [GroundedCoord; N],
}
impl<const N: usize> GroundedTuple<N> {
#[inline]
#[must_use]
pub const fn new(coords: [GroundedCoord; N]) -> Self {
Self { coords }
}
}
pub trait GroundedValue: sealed::Sealed {}
impl GroundedValue for GroundedCoord {}
impl<const N: usize> GroundedValue for GroundedTuple<N> {}
pub trait Grounding {
type Output: GroundedValue;
fn ground(&self, external: &[u8]) -> Option<Self::Output>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Validated<T> {
inner: T,
_sealed: (),
}
impl<T> Validated<T> {
#[inline]
#[must_use]
pub fn inner(&self) -> &T {
&self.inner
}
#[inline]
#[allow(dead_code)]
pub(crate) const fn new(inner: T) -> Self {
Self { inner, _sealed: () }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Derivation {
step_count: u32,
root_address: u64,
}
impl Derivation {
#[inline]
#[must_use]
pub const fn step_count(&self) -> u32 {
self.step_count
}
#[inline]
#[must_use]
pub const fn root_address(&self) -> u64 {
self.root_address
}
#[inline]
#[allow(dead_code)]
pub(crate) const fn new(step_count: u32, root_address: u64) -> Self {
Self {
step_count,
root_address,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FiberBudget {
total: u32,
pinned: u32,
}
impl FiberBudget {
#[inline]
#[must_use]
pub const fn total(&self) -> u32 {
self.total
}
#[inline]
#[must_use]
pub const fn pinned(&self) -> u32 {
self.pinned
}
#[inline]
#[must_use]
pub const fn remaining(&self) -> u32 {
self.total - self.pinned
}
#[inline]
#[allow(dead_code)]
pub(crate) const fn new(total: u32, pinned: u32) -> Self {
Self { total, pinned }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TermList {
pub start: u32,
pub len: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TermArena<const CAP: usize> {
nodes: [Option<Term>; CAP],
len: u32,
}
impl<const CAP: usize> TermArena<CAP> {
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
nodes: core::array::from_fn(|_| None),
len: 0,
}
}
#[must_use]
pub fn push(&mut self, term: Term) -> Option<u32> {
let idx = self.len;
if (idx as usize) >= CAP {
return None;
}
self.nodes[idx as usize] = Some(term);
self.len = idx + 1;
Some(idx)
}
#[inline]
#[must_use]
pub fn get(&self, index: u32) -> Option<&Term> {
self.nodes
.get(index as usize)
.and_then(|slot| slot.as_ref())
}
#[inline]
#[must_use]
pub const fn len(&self) -> u32 {
self.len
}
#[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
}
impl<const CAP: usize> Default for TermArena<CAP> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Term {
Literal {
value: u64,
level: QuantumLevel,
},
Variable {
name_index: u32,
},
Application {
operator: PrimitiveOp,
args: TermList,
},
Lift {
operand_index: u32,
target: QuantumLevel,
},
Project {
operand_index: u32,
target: QuantumLevel,
},
Match {
scrutinee_index: u32,
arms: TermList,
},
Recurse {
measure_index: u32,
base_index: u32,
step_index: u32,
},
Unfold {
seed_index: u32,
step_index: u32,
},
Try {
body_index: u32,
handler_index: u32,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeDeclaration {
pub name_index: u32,
pub constraints: TermList,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Binding {
pub name_index: u32,
pub type_index: u32,
pub value_index: u32,
pub surface: &'static str,
pub content_address: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Assertion {
pub lhs_index: u32,
pub rhs_index: u32,
pub surface: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceDeclaration {
pub name_index: u32,
pub type_index: u32,
pub grounding_name_index: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SinkDeclaration {
pub name_index: u32,
pub type_index: u32,
pub projection_name_index: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShapeViolation {
pub shape_iri: &'static str,
pub constraint_iri: &'static str,
pub property_iri: &'static str,
pub expected_range: &'static str,
pub min_count: u32,
pub max_count: u32,
pub kind: ViolationKind,
}
#[derive(Debug, Clone)]
pub struct CompileUnitBuilder<'a> {
root_term: Option<&'a [Term]>,
quantum_level_ceiling: Option<QuantumLevel>,
thermodynamic_budget: Option<u64>,
target_domains: Option<&'a [VerificationDomain]>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompileUnit {
level: QuantumLevel,
budget: u64,
}
impl<'a> CompileUnitBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
root_term: None,
quantum_level_ceiling: None,
thermodynamic_budget: None,
target_domains: None,
}
}
#[must_use]
pub const fn root_term(mut self, terms: &'a [Term]) -> Self {
self.root_term = Some(terms);
self
}
#[must_use]
pub const fn quantum_level_ceiling(mut self, level: QuantumLevel) -> Self {
self.quantum_level_ceiling = Some(level);
self
}
#[must_use]
pub const fn thermodynamic_budget(mut self, budget: u64) -> Self {
self.thermodynamic_budget = Some(budget);
self
}
#[must_use]
pub const fn target_domains(mut self, domains: &'a [VerificationDomain]) -> Self {
self.target_domains = Some(domains);
self
}
pub fn validate(self) -> Result<Validated<CompileUnit>, ShapeViolation> {
if self.root_term.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/CompileUnitShape",
constraint_iri:
"https://uor.foundation/conformance/compileUnit_rootTerm_constraint",
property_iri: "https://uor.foundation/cascade/rootTerm",
expected_range: "https://uor.foundation/schema/Term",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
let level = match self.quantum_level_ceiling {
Some(l) => l,
None => {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/CompileUnitShape",
constraint_iri:
"https://uor.foundation/conformance/compileUnit_unitQuantumLevel_constraint",
property_iri: "https://uor.foundation/cascade/unitQuantumLevel",
expected_range: "https://uor.foundation/schema/QuantumLevel",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
})
}
};
let budget = match self.thermodynamic_budget {
Some(b) => b,
None => return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/CompileUnitShape",
constraint_iri:
"https://uor.foundation/conformance/compileUnit_thermodynamicBudget_constraint",
property_iri: "https://uor.foundation/cascade/thermodynamicBudget",
expected_range: "http://www.w3.org/2001/XMLSchema#decimal",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
}),
};
match self.target_domains {
Some(d) if !d.is_empty() => {}
_ => {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/CompileUnitShape",
constraint_iri:
"https://uor.foundation/conformance/compileUnit_targetDomains_constraint",
property_iri: "https://uor.foundation/cascade/targetDomains",
expected_range: "https://uor.foundation/op/VerificationDomain",
min_count: 1,
max_count: 0,
kind: ViolationKind::Missing,
})
}
}
Ok(Validated::new(CompileUnit { level, budget }))
}
}
impl<'a> Default for CompileUnitBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct EffectDeclarationBuilder<'a> {
name: Option<&'a str>,
target_fibers: Option<&'a [u32]>,
budget_delta: Option<i64>,
commutes: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EffectDeclaration {
pub shape_iri: &'static str,
}
impl<'a> EffectDeclarationBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
name: None,
target_fibers: None,
budget_delta: None,
commutes: None,
}
}
#[must_use]
pub const fn name(mut self, value: &'a str) -> Self {
self.name = Some(value);
self
}
#[must_use]
pub const fn target_fibers(mut self, value: &'a [u32]) -> Self {
self.target_fibers = Some(value);
self
}
#[must_use]
pub const fn budget_delta(mut self, value: i64) -> Self {
self.budget_delta = Some(value);
self
}
#[must_use]
pub const fn commutes(mut self, value: bool) -> Self {
self.commutes = Some(value);
self
}
pub fn validate(self) -> Result<Validated<EffectDeclaration>, ShapeViolation> {
if self.name.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/EffectShape",
constraint_iri: "https://uor.foundation/conformance/EffectShape",
property_iri: "https://uor.foundation/conformance/name",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.target_fibers.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/EffectShape",
constraint_iri: "https://uor.foundation/conformance/EffectShape",
property_iri: "https://uor.foundation/conformance/target_fibers",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.budget_delta.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/EffectShape",
constraint_iri: "https://uor.foundation/conformance/EffectShape",
property_iri: "https://uor.foundation/conformance/budget_delta",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.commutes.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/EffectShape",
constraint_iri: "https://uor.foundation/conformance/EffectShape",
property_iri: "https://uor.foundation/conformance/commutes",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
Ok(Validated::new(EffectDeclaration {
shape_iri: "https://uor.foundation/conformance/EffectShape",
}))
}
}
impl<'a> Default for EffectDeclarationBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct GroundingDeclarationBuilder<'a> {
source_type: Option<&'a str>,
ring_mapping: Option<&'a str>,
invertibility: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroundingDeclaration {
pub shape_iri: &'static str,
}
impl<'a> GroundingDeclarationBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
source_type: None,
ring_mapping: None,
invertibility: None,
}
}
#[must_use]
pub const fn source_type(mut self, value: &'a str) -> Self {
self.source_type = Some(value);
self
}
#[must_use]
pub const fn ring_mapping(mut self, value: &'a str) -> Self {
self.ring_mapping = Some(value);
self
}
#[must_use]
pub const fn invertibility(mut self, value: bool) -> Self {
self.invertibility = Some(value);
self
}
pub fn validate(self) -> Result<Validated<GroundingDeclaration>, ShapeViolation> {
if self.source_type.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/GroundingShape",
constraint_iri: "https://uor.foundation/conformance/GroundingShape",
property_iri: "https://uor.foundation/conformance/source_type",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.ring_mapping.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/GroundingShape",
constraint_iri: "https://uor.foundation/conformance/GroundingShape",
property_iri: "https://uor.foundation/conformance/ring_mapping",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.invertibility.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/GroundingShape",
constraint_iri: "https://uor.foundation/conformance/GroundingShape",
property_iri: "https://uor.foundation/conformance/invertibility",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
Ok(Validated::new(GroundingDeclaration {
shape_iri: "https://uor.foundation/conformance/GroundingShape",
}))
}
}
impl<'a> Default for GroundingDeclarationBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct DispatchDeclarationBuilder<'a> {
predicate: Option<&'a [Term]>,
target_resolver: Option<&'a str>,
priority: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DispatchDeclaration {
pub shape_iri: &'static str,
}
impl<'a> DispatchDeclarationBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
predicate: None,
target_resolver: None,
priority: None,
}
}
#[must_use]
pub const fn predicate(mut self, value: &'a [Term]) -> Self {
self.predicate = Some(value);
self
}
#[must_use]
pub const fn target_resolver(mut self, value: &'a str) -> Self {
self.target_resolver = Some(value);
self
}
#[must_use]
pub const fn priority(mut self, value: u32) -> Self {
self.priority = Some(value);
self
}
pub fn validate(self) -> Result<Validated<DispatchDeclaration>, ShapeViolation> {
if self.predicate.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/DispatchShape",
constraint_iri: "https://uor.foundation/conformance/DispatchShape",
property_iri: "https://uor.foundation/conformance/predicate",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.target_resolver.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/DispatchShape",
constraint_iri: "https://uor.foundation/conformance/DispatchShape",
property_iri: "https://uor.foundation/conformance/target_resolver",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.priority.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/DispatchShape",
constraint_iri: "https://uor.foundation/conformance/DispatchShape",
property_iri: "https://uor.foundation/conformance/priority",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
Ok(Validated::new(DispatchDeclaration {
shape_iri: "https://uor.foundation/conformance/DispatchShape",
}))
}
}
impl<'a> Default for DispatchDeclarationBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct LeaseDeclarationBuilder<'a> {
linear_fiber: Option<u32>,
scope: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeaseDeclaration {
pub shape_iri: &'static str,
}
impl<'a> LeaseDeclarationBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
linear_fiber: None,
scope: None,
}
}
#[must_use]
pub const fn linear_fiber(mut self, value: u32) -> Self {
self.linear_fiber = Some(value);
self
}
#[must_use]
pub const fn scope(mut self, value: &'a str) -> Self {
self.scope = Some(value);
self
}
pub fn validate(self) -> Result<Validated<LeaseDeclaration>, ShapeViolation> {
if self.linear_fiber.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/LeaseShape",
constraint_iri: "https://uor.foundation/conformance/LeaseShape",
property_iri: "https://uor.foundation/conformance/linear_fiber",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.scope.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/LeaseShape",
constraint_iri: "https://uor.foundation/conformance/LeaseShape",
property_iri: "https://uor.foundation/conformance/scope",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
Ok(Validated::new(LeaseDeclaration {
shape_iri: "https://uor.foundation/conformance/LeaseShape",
}))
}
}
impl<'a> Default for LeaseDeclarationBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct StreamDeclarationBuilder<'a> {
seed: Option<&'a [Term]>,
step: Option<&'a [Term]>,
productivity_witness: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamDeclaration {
pub shape_iri: &'static str,
}
impl<'a> StreamDeclarationBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
seed: None,
step: None,
productivity_witness: None,
}
}
#[must_use]
pub const fn seed(mut self, value: &'a [Term]) -> Self {
self.seed = Some(value);
self
}
#[must_use]
pub const fn step(mut self, value: &'a [Term]) -> Self {
self.step = Some(value);
self
}
#[must_use]
pub const fn productivity_witness(mut self, value: &'a str) -> Self {
self.productivity_witness = Some(value);
self
}
pub fn validate(self) -> Result<Validated<StreamDeclaration>, ShapeViolation> {
if self.seed.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/StreamShape",
constraint_iri: "https://uor.foundation/conformance/StreamShape",
property_iri: "https://uor.foundation/conformance/seed",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.step.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/StreamShape",
constraint_iri: "https://uor.foundation/conformance/StreamShape",
property_iri: "https://uor.foundation/conformance/step",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.productivity_witness.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/StreamShape",
constraint_iri: "https://uor.foundation/conformance/StreamShape",
property_iri: "https://uor.foundation/conformance/productivity_witness",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
Ok(Validated::new(StreamDeclaration {
shape_iri: "https://uor.foundation/conformance/StreamShape",
}))
}
}
impl<'a> Default for StreamDeclarationBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct PredicateDeclarationBuilder<'a> {
input_type: Option<&'a str>,
evaluator: Option<&'a [Term]>,
termination_witness: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PredicateDeclaration {
pub shape_iri: &'static str,
}
impl<'a> PredicateDeclarationBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
input_type: None,
evaluator: None,
termination_witness: None,
}
}
#[must_use]
pub const fn input_type(mut self, value: &'a str) -> Self {
self.input_type = Some(value);
self
}
#[must_use]
pub const fn evaluator(mut self, value: &'a [Term]) -> Self {
self.evaluator = Some(value);
self
}
#[must_use]
pub const fn termination_witness(mut self, value: &'a str) -> Self {
self.termination_witness = Some(value);
self
}
pub fn validate(self) -> Result<Validated<PredicateDeclaration>, ShapeViolation> {
if self.input_type.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/PredicateShape",
constraint_iri: "https://uor.foundation/conformance/PredicateShape",
property_iri: "https://uor.foundation/conformance/input_type",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.evaluator.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/PredicateShape",
constraint_iri: "https://uor.foundation/conformance/PredicateShape",
property_iri: "https://uor.foundation/conformance/evaluator",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.termination_witness.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/PredicateShape",
constraint_iri: "https://uor.foundation/conformance/PredicateShape",
property_iri: "https://uor.foundation/conformance/termination_witness",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
Ok(Validated::new(PredicateDeclaration {
shape_iri: "https://uor.foundation/conformance/PredicateShape",
}))
}
}
impl<'a> Default for PredicateDeclarationBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct ParallelDeclarationBuilder<'a> {
fiber_partition: Option<&'a [u32]>,
disjointness_witness: Option<&'a str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParallelDeclaration {
pub shape_iri: &'static str,
}
impl<'a> ParallelDeclarationBuilder<'a> {
#[must_use]
pub const fn new() -> Self {
Self {
fiber_partition: None,
disjointness_witness: None,
}
}
#[must_use]
pub const fn fiber_partition(mut self, value: &'a [u32]) -> Self {
self.fiber_partition = Some(value);
self
}
#[must_use]
pub const fn disjointness_witness(mut self, value: &'a str) -> Self {
self.disjointness_witness = Some(value);
self
}
pub fn validate(self) -> Result<Validated<ParallelDeclaration>, ShapeViolation> {
if self.fiber_partition.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/ParallelShape",
constraint_iri: "https://uor.foundation/conformance/ParallelShape",
property_iri: "https://uor.foundation/conformance/fiber_partition",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
if self.disjointness_witness.is_none() {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/ParallelShape",
constraint_iri: "https://uor.foundation/conformance/ParallelShape",
property_iri: "https://uor.foundation/conformance/disjointness_witness",
expected_range: "http://www.w3.org/2002/07/owl#Thing",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
});
}
Ok(Validated::new(ParallelDeclaration {
shape_iri: "https://uor.foundation/conformance/ParallelShape",
}))
}
}
impl<'a> Default for ParallelDeclarationBuilder<'a> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct QuantumLevelDeclarationBuilder {
bit_width: Option<u32>,
cycle_size: Option<u128>,
predecessor: Option<QuantumLevel>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuantumLevelDeclaration {
pub bit_width: u32,
pub predecessor: QuantumLevel,
}
impl QuantumLevelDeclarationBuilder {
#[must_use]
pub const fn new() -> Self {
Self {
bit_width: None,
cycle_size: None,
predecessor: None,
}
}
#[must_use]
pub const fn bit_width(mut self, w: u32) -> Self {
self.bit_width = Some(w);
self
}
#[must_use]
pub const fn cycle_size(mut self, s: u128) -> Self {
self.cycle_size = Some(s);
self
}
#[must_use]
pub const fn predecessor(mut self, level: QuantumLevel) -> Self {
self.predecessor = Some(level);
self
}
pub fn validate(self) -> Result<Validated<QuantumLevelDeclaration>, ShapeViolation> {
let bw = match self.bit_width {
Some(w) => w,
None => {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/QuantumLevelShape",
constraint_iri: "https://uor.foundation/conformance/QuantumLevelShape",
property_iri: "https://uor.foundation/conformance/declaredBitWidth",
expected_range: "http://www.w3.org/2001/XMLSchema#positiveInteger",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
})
}
};
let pred = match self.predecessor {
Some(p) => p,
None => {
return Err(ShapeViolation {
shape_iri: "https://uor.foundation/conformance/QuantumLevelShape",
constraint_iri: "https://uor.foundation/conformance/QuantumLevelShape",
property_iri: "https://uor.foundation/conformance/predecessorLevel",
expected_range: "https://uor.foundation/schema/QuantumLevel",
min_count: 1,
max_count: 1,
kind: ViolationKind::Missing,
})
}
};
Ok(Validated::new(QuantumLevelDeclaration {
bit_width: bw,
predecessor: pred,
}))
}
}
impl Default for QuantumLevelDeclarationBuilder {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BoundarySession {
crossing_count: u32,
is_idempotent: bool,
}
impl BoundarySession {
#[inline]
#[allow(dead_code)]
pub(crate) const fn new(is_idempotent: bool) -> Self {
Self {
crossing_count: 0,
is_idempotent,
}
}
#[inline]
#[must_use]
pub const fn crossing_count(&self) -> u32 {
self.crossing_count
}
#[inline]
#[must_use]
pub const fn is_idempotent(&self) -> bool {
self.is_idempotent
}
}
#[allow(dead_code)]
pub(crate) fn validate_and_mint_coord(
grounded: GroundedCoord,
shape: &Validated<GroundingDeclaration>,
session: &mut BoundarySession,
) -> Result<Datum, ShapeViolation> {
let _ = shape; session.crossing_count += 1;
let inner = match grounded.inner {
GroundedCoordInner::Q0(b) => DatumInner::Q0(b),
GroundedCoordInner::Q1(b) => DatumInner::Q1(b),
GroundedCoordInner::Q3(b) => DatumInner::Q3(b),
GroundedCoordInner::Q7(b) => DatumInner::Q7(b),
GroundedCoordInner::Q511(b) => DatumInner::Q511(b),
};
Ok(Datum { inner })
}
#[allow(dead_code)]
pub(crate) fn validate_and_mint_tuple<const N: usize>(
grounded: GroundedTuple<N>,
shape: &Validated<GroundingDeclaration>,
session: &mut BoundarySession,
) -> Result<Datum, ShapeViolation> {
if N == 0 {
return Err(ShapeViolation {
shape_iri: shape.inner().shape_iri,
constraint_iri: shape.inner().shape_iri,
property_iri: "https://uor.foundation/conformance/groundingSourceType",
expected_range: "https://uor.foundation/type/TypeDefinition",
min_count: 1,
max_count: 0,
kind: ViolationKind::CardinalityViolation,
});
}
validate_and_mint_coord(grounded.coords[0].clone(), shape, session)
}
#[inline]
#[must_use]
pub const fn const_ring_eval_q0(op: PrimitiveOp, a: u8, b: u8) -> u8 {
match op {
PrimitiveOp::Add => a.wrapping_add(b),
PrimitiveOp::Sub => a.wrapping_sub(b),
PrimitiveOp::Mul => a.wrapping_mul(b),
PrimitiveOp::Xor => a ^ b,
PrimitiveOp::And => a & b,
PrimitiveOp::Or => a | b,
_ => 0,
}
}
#[inline]
#[must_use]
pub const fn const_ring_eval_unary_q0(op: PrimitiveOp, a: u8) -> u8 {
match op {
PrimitiveOp::Neg => 0u8.wrapping_sub(a),
PrimitiveOp::Bnot => !a,
PrimitiveOp::Succ => a.wrapping_add(1),
PrimitiveOp::Pred => a.wrapping_sub(1),
_ => 0,
}
}
#[inline]
#[must_use]
pub const fn const_ring_eval_q1(op: PrimitiveOp, a: u16, b: u16) -> u16 {
match op {
PrimitiveOp::Add => a.wrapping_add(b),
PrimitiveOp::Sub => a.wrapping_sub(b),
PrimitiveOp::Mul => a.wrapping_mul(b),
PrimitiveOp::Xor => a ^ b,
PrimitiveOp::And => a & b,
PrimitiveOp::Or => a | b,
_ => 0,
}
}
#[inline]
#[must_use]
pub const fn const_ring_eval_unary_q1(op: PrimitiveOp, a: u16) -> u16 {
match op {
PrimitiveOp::Neg => 0u16.wrapping_sub(a),
PrimitiveOp::Bnot => !a,
PrimitiveOp::Succ => a.wrapping_add(1),
PrimitiveOp::Pred => a.wrapping_sub(1),
_ => 0,
}
}
#[inline]
#[must_use]
pub const fn const_ring_eval_q3(op: PrimitiveOp, a: u32, b: u32) -> u32 {
match op {
PrimitiveOp::Add => a.wrapping_add(b),
PrimitiveOp::Sub => a.wrapping_sub(b),
PrimitiveOp::Mul => a.wrapping_mul(b),
PrimitiveOp::Xor => a ^ b,
PrimitiveOp::And => a & b,
PrimitiveOp::Or => a | b,
_ => 0,
}
}
#[inline]
#[must_use]
pub const fn const_ring_eval_unary_q3(op: PrimitiveOp, a: u32) -> u32 {
match op {
PrimitiveOp::Neg => 0u32.wrapping_sub(a),
PrimitiveOp::Bnot => !a,
PrimitiveOp::Succ => a.wrapping_add(1),
PrimitiveOp::Pred => a.wrapping_sub(1),
_ => 0,
}
}
#[inline]
#[must_use]
pub const fn const_ring_eval_q7(op: PrimitiveOp, a: u64, b: u64) -> u64 {
match op {
PrimitiveOp::Add => a.wrapping_add(b),
PrimitiveOp::Sub => a.wrapping_sub(b),
PrimitiveOp::Mul => a.wrapping_mul(b),
PrimitiveOp::Xor => a ^ b,
PrimitiveOp::And => a & b,
PrimitiveOp::Or => a | b,
_ => 0,
}
}
#[inline]
#[must_use]
pub const fn const_ring_eval_unary_q7(op: PrimitiveOp, a: u64) -> u64 {
match op {
PrimitiveOp::Neg => 0u64.wrapping_sub(a),
PrimitiveOp::Bnot => !a,
PrimitiveOp::Succ => a.wrapping_add(1),
PrimitiveOp::Pred => a.wrapping_sub(1),
_ => 0,
}
}