Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ enum_dispatch = "0.3.11"
html-escape = "0.2.13"
itertools = "0.14.0"
jsonschema = "0.29.1"
lazy_static = "1.4.0"
num-rational = "0.4.1"
paste = "1.0"
proptest = "1.7.0"
Expand Down
1 change: 0 additions & 1 deletion hugr-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ fxhash.workspace = true
html-escape = { workspace = true }
indexmap.workspace = true
itertools = { workspace = true }
lazy_static = { workspace = true }
paste = { workspace = true }
petgraph = { workspace = true }
portgraph = { workspace = true, features = ["serde", "petgraph"] }
Expand Down
200 changes: 108 additions & 92 deletions hugr-core/src/extension/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Prelude extension - available in all contexts, defining common types,
//! operations and constants.
use std::str::FromStr;
use std::sync::{Arc, Weak};
use std::sync::{Arc, LazyLock, Weak};

use itertools::Itertools;
use lazy_static::lazy_static;

use crate::extension::const_fold::fold_out_row;
use crate::extension::simple_op::{
Expand Down Expand Up @@ -40,110 +39,127 @@ pub mod generic;
pub const PRELUDE_ID: ExtensionId = ExtensionId::new_unchecked("prelude");
/// Extension version.
pub const VERSION: semver::Version = semver::Version::new(0, 2, 1);
lazy_static! {
/// Prelude extension, containing common types and operations.
pub static ref PRELUDE: Arc<Extension> = {
Extension::new_arc(PRELUDE_ID, VERSION, |prelude, extension_ref| {

// Construct the list and error types using the passed extension
// reference.
//
// If we tried to use `string_type()` or `error_type()` directly it
// would try to access the `PRELUDE` lazy static recursively,
// causing a deadlock.
let string_type: Type = string_custom_type(extension_ref).into();
let usize_type: Type = usize_custom_t(extension_ref).into();
let error_type: CustomType = error_custom_type(extension_ref);

prelude
.add_type(
TypeName::new_inline("usize"),
vec![],
"usize".into(),
TypeDefBound::copyable(),
extension_ref,
)
.unwrap();
prelude.add_type(
STRING_TYPE_NAME,
vec![],
"string".into(),
TypeDefBound::copyable(),
extension_ref,
)
.unwrap();
prelude.add_op(
PRINT_OP_ID,
"Print the string to standard output".to_string(),
Signature::new(vec![string_type.clone()], type_row![]),
extension_ref,
)
.unwrap();
prelude
.add_type(
TypeName::new_inline("qubit"),
vec![],
"qubit".into(),
TypeDefBound::any(),
extension_ref,
)
.unwrap();
prelude
.add_type(
ERROR_TYPE_NAME,
vec![],
"Simple opaque error type.".into(),
TypeDefBound::copyable(),
extension_ref,
)
.unwrap();
prelude
.add_op(
MAKE_ERROR_OP_ID,
"Create an error value".to_string(),
Signature::new(vec![usize_type, string_type], vec![error_type.clone().into()]),
extension_ref,
)
.unwrap();
prelude
.add_op(
PANIC_OP_ID,
"Panic with input error".to_string(),
PolyFuncTypeRV::new(
[TypeParam::new_list_type(TypeBound::Linear), TypeParam::new_list_type(TypeBound::Linear)],
FuncValueType::new(
vec![TypeRV::new_extension(error_type.clone()), TypeRV::new_row_var_use(0, TypeBound::Linear)],
vec![TypeRV::new_row_var_use(1, TypeBound::Linear)],
),
/// Prelude extension, containing common types and operations.
pub static PRELUDE: LazyLock<Arc<Extension>> = LazyLock::new(|| {
Extension::new_arc(PRELUDE_ID, VERSION, |prelude, extension_ref| {
// Construct the list and error types using the passed extension
// reference.
//
// If we tried to use `string_type()` or `error_type()` directly it
// would try to access the `PRELUDE` lazy static recursively,
// causing a deadlock.
let string_type: Type = string_custom_type(extension_ref).into();
let usize_type: Type = usize_custom_t(extension_ref).into();
let error_type: CustomType = error_custom_type(extension_ref);

prelude
.add_type(
TypeName::new_inline("usize"),
vec![],
"usize".into(),
TypeDefBound::copyable(),
extension_ref,
)
.unwrap();
prelude
.add_type(
STRING_TYPE_NAME,
vec![],
"string".into(),
TypeDefBound::copyable(),
extension_ref,
)
.unwrap();
prelude
.add_op(
PRINT_OP_ID,
"Print the string to standard output".to_string(),
Signature::new(vec![string_type.clone()], type_row![]),
extension_ref,
)
.unwrap();
prelude
.add_type(
TypeName::new_inline("qubit"),
vec![],
"qubit".into(),
TypeDefBound::any(),
extension_ref,
)
.unwrap();
prelude
.add_type(
ERROR_TYPE_NAME,
vec![],
"Simple opaque error type.".into(),
TypeDefBound::copyable(),
extension_ref,
)
.unwrap();
prelude
.add_op(
MAKE_ERROR_OP_ID,
"Create an error value".to_string(),
Signature::new(
vec![usize_type, string_type],
vec![error_type.clone().into()],
),
extension_ref,
)
.unwrap();
prelude
.add_op(
PANIC_OP_ID,
"Panic with input error".to_string(),
PolyFuncTypeRV::new(
[
TypeParam::new_list_type(TypeBound::Linear),
TypeParam::new_list_type(TypeBound::Linear),
],
FuncValueType::new(
vec![
TypeRV::new_extension(error_type.clone()),
TypeRV::new_row_var_use(0, TypeBound::Linear),
],
vec![TypeRV::new_row_var_use(1, TypeBound::Linear)],
),
extension_ref,
)
.unwrap();
prelude
),
extension_ref,
)
.unwrap();
prelude
.add_op(
EXIT_OP_ID,
"Exit with input error".to_string(),
PolyFuncTypeRV::new(
[TypeParam::new_list_type(TypeBound::Linear), TypeParam::new_list_type(TypeBound::Linear)],
[
TypeParam::new_list_type(TypeBound::Linear),
TypeParam::new_list_type(TypeBound::Linear),
],
FuncValueType::new(
vec![TypeRV::new_extension(error_type), TypeRV::new_row_var_use(0, TypeBound::Linear)],
vec![
TypeRV::new_extension(error_type),
TypeRV::new_row_var_use(0, TypeBound::Linear),
],
vec![TypeRV::new_row_var_use(1, TypeBound::Linear)],
),
),
extension_ref,
)
.unwrap();

TupleOpDef::load_all_ops(prelude, extension_ref).unwrap();
NoopDef.add_to_extension(prelude, extension_ref).unwrap();
BarrierDef.add_to_extension(prelude, extension_ref).unwrap();
generic::LoadNatDef.add_to_extension(prelude, extension_ref).unwrap();
})
};
TupleOpDef::load_all_ops(prelude, extension_ref).unwrap();
NoopDef.add_to_extension(prelude, extension_ref).unwrap();
BarrierDef.add_to_extension(prelude, extension_ref).unwrap();
generic::LoadNatDef
.add_to_extension(prelude, extension_ref)
.unwrap();
})
});

/// An extension registry containing only the prelude
pub static ref PRELUDE_REGISTRY: ExtensionRegistry = ExtensionRegistry::new([PRELUDE.clone()]);
}
/// An extension registry containing only the prelude
pub static PRELUDE_REGISTRY: LazyLock<ExtensionRegistry> =
LazyLock::new(|| ExtensionRegistry::new([PRELUDE.clone()]));

pub(crate) fn usize_custom_t(extension_ref: &Weak<Extension>) -> CustomType {
CustomType::new(
Expand Down
20 changes: 9 additions & 11 deletions hugr-core/src/extension/simple_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,14 @@ impl<T: MakeRegisteredOp> From<T> for OpType {

#[cfg(test)]
mod test {
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use crate::{
const_extension_ids, type_row,
types::{Signature, Term},
};

use super::*;
use lazy_static::lazy_static;
use strum::{EnumIter, EnumString, IntoStaticStr};

#[derive(Clone, Debug, Hash, PartialEq, Eq, EnumIter, IntoStaticStr, EnumString)]
Expand Down Expand Up @@ -359,15 +358,14 @@ mod test {
const EXT_ID: ExtensionId = "DummyExt";
}

lazy_static! {
static ref EXT: Arc<Extension> = {
Extension::new_test_arc(EXT_ID.clone(), |ext, extension_ref| {
DummyEnum::Dumb
.add_to_extension(ext, extension_ref)
.unwrap();
})
};
}
static EXT: LazyLock<Arc<Extension>> = LazyLock::new(|| {
Extension::new_test_arc(EXT_ID.clone(), |ext, extension_ref| {
DummyEnum::Dumb
.add_to_extension(ext, extension_ref)
.unwrap();
})
});

impl MakeRegisteredOp for DummyEnum {
fn extension_id(&self) -> ExtensionId {
EXT_ID.clone()
Expand Down
11 changes: 5 additions & 6 deletions hugr-core/src/hugr/ident.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use std::borrow::Borrow;
use std::{borrow::Borrow, sync::LazyLock};

use derive_more::Display;
use lazy_static::lazy_static;
use regex::Regex;
use smol_str::SmolStr;
use thiserror::Error;

pub static PATH_COMPONENT_REGEX_STR: &str = r"[\w--\d]\w*";
lazy_static! {
pub static ref PATH_REGEX: Regex = Regex::new(&format!(
pub static PATH_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(&format!(
r"^{PATH_COMPONENT_REGEX_STR}(\.{PATH_COMPONENT_REGEX_STR})*$"
))
.unwrap();
}
.unwrap()
});

#[derive(
Clone,
Expand Down
10 changes: 5 additions & 5 deletions hugr-core/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ use crate::types::{
TypeRV,
};
use crate::{OutgoingPort, Visibility, type_row};
use std::sync::LazyLock;

use itertools::Itertools;
use jsonschema::{Draft, Validator};
use lazy_static::lazy_static;
use portgraph::{Hierarchy, LinkMut, PortMut, UnmanagedDenseMap, multiportgraph::MultiPortGraph};
use rstest::rstest;

Expand Down Expand Up @@ -90,8 +90,8 @@ impl NamedSchema {

macro_rules! include_schema {
($name:ident, $path:literal) => {
lazy_static! {
static ref $name: NamedSchema =
static $name: LazyLock<NamedSchema> =
LazyLock::new(|| {
NamedSchema::new(stringify!($name), {
let schema_val: serde_json::Value = serde_json::from_str(include_str!(
concat!("../../../../specification/schema/", $path, "_live.json")
Expand All @@ -101,8 +101,8 @@ macro_rules! include_schema {
.with_draft(Draft::Draft7)
.build(&schema_val)
.expect("Schema is invalid.")
});
}
})
});
};
}

Expand Down
Loading
Loading