Skip to content

Commit 1dbd497

Browse files
Merge pull request #370 from marvin-hansen/008-hkt-uncertain-specs
Added deep_causality_ast
2 parents b788c30 + d07e795 commit 1dbd497

File tree

43 files changed

+2451
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2451
-10
lines changed

AGENTS.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ It uses three main components:
1717

1818
The project is a monorepo containing several sub-crates, including:
1919

20-
* `deep_causality`: The main crate.
21-
* `deep_causality_algorithms`: Provides algorithms for the library.
22-
* `deep_causality_data_structures`: Provides data structures for the library.
20+
* `deep_causality`: The main DeepCausality library.
21+
* `deep_causality_algorithms`: Provides algorithms for the DeepCausality library.
22+
* `deep_causality_ast`: Provides utils to work on AST for the DeepCausality library.
23+
* `deep_causality_data_structures`: Provides data structures for the DeepCausality library.
2324
* `deep_causality_discovery`: A custom DSL for causal discovery.
24-
* `deep_causality_macros`: Provides macros for the library.
25-
* `deep_causality_num`: Numerical traits and utils used in other crates.
25+
* `deep_causality_haft`: Higher-Order Abstract Functional Traits a.k.a HKT.
26+
* `deep_causality_macros`: Provides macros for the DeepCausality library (_deprecated_).
27+
* `deep_causality_num`: Numerical traits and utils used across the other crates.
2628
* `deep_causality_rand`: Random number generator and statistical distributions used in deep_causality_tensor and other
27-
* `deep_causality_tensor`: A custom tensor type used in deep_causality_algorithms and deep_causality_discovery
28-
* `deep_causality_uncertain`: Provides functionality for handling uncertainty.
29+
* `deep_causality_tensor`: A custom tensor type used in deep_causality_algorithms and deep_causality_discovery crates.
30+
* `deep_causality_uncertain`: Provides a custom crate for handling uncertainty.
2931
* `examples`: A collection of example code.
30-
* `ultragraph`: A graph library used as a backend.
32+
* `ultragraph`: A hyper-graph library used as a backend in the deep_causality library.
3133

3234

3335
## Building and Running

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ resolver = "2"
66
members = [
77
"deep_causality",
88
"deep_causality_algorithms",
9+
"deep_causality_ast",
910
"deep_causality_data_structures",
1011
"deep_causality_discovery",
1112
"deep_causality_haft",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ make example
164164

165165
* `deep_causality`: The main DeepCausality library.
166166
* `deep_causality_algorithms`: Provides algorithms for the DeepCausality library.
167+
* `deep_causality_ast`: Provides utils to work on AST for the DeepCausality library.
167168
* `deep_causality_data_structures`: Provides data structures for the DeepCausality library.
168169
* `deep_causality_discovery`: A custom DSL for causal discovery.
169170
* `deep_causality_haft`: Higher-Order Abstract Functional Traits a.k.a HKT.

build/scripts/sbom.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set -o pipefail
1010
CRATES=(
1111
"deep_causality"
1212
"deep_causality_algorithms"
13+
"deep_causality_ast"
1314
"deep_causality_data_structures"
1415
"deep_causality_discovery"
1516
"deep_causality_haft"
@@ -37,5 +38,3 @@ for CRATE_NAME in "${CRATES[@]}"; do
3738
done
3839

3940
echo "SBOM generation complete."
40-
41-

deep_causality_ast/BUILD.bazel

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_doc", "rust_doc_test", "rust_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
rust_library(
6+
name = "deep_causality_ast",
7+
srcs = glob(["src/**/*.rs"]),
8+
crate_root = "src/lib.rs",
9+
tags = [
10+
"data_structures",
11+
],
12+
deps = [
13+
],
14+
)
15+
16+
rust_doc(
17+
name = "doc",
18+
crate = ":deep_causality_ast",
19+
tags = ["doc"],
20+
visibility = ["//visibility:public"],
21+
)
22+
23+
rust_doc_test(
24+
name = "doc_test",
25+
crate = ":deep_causality_ast",
26+
tags = ["doc-test"],
27+
visibility = ["//visibility:public"],
28+
)

deep_causality_ast/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "deep_causality_ast"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
license.workspace = true
7+
8+
repository = "https://github.com/deepcausality/deep_causality.rs"
9+
authors = ["Marvin Hansen <[email protected]>", ]
10+
description = "AST data structure for deep_causality crate."
11+
documentation = "https://docs.rs/deep_causality"
12+
categories = ["data-structures", "science"]
13+
keywords = ["data-structures", "ast"]
14+
# Exclude all bazel files as these conflict with Bazel workspace when vendored.
15+
exclude = ["*.bazel", "*/*.bazel", "*.bazel.*", "BUILD", "BUILD.bazel", "MODULE.bazel", ".bazelignore",".bazelrc", "tests/**/*"]
16+

deep_causality_ast/LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SPDX-License-Identifier: MIT
2+
3+
Copyright (c) 2023 - The DeepCausality Authors. All Rights Reserved.
4+
5+
MIT License
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.

deep_causality_ast/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# `deep_causality_ast`
2+
3+
[![Crates.io](https://img.shields.io/crates/v/deep_causality_ast.svg)](https://crates.io/crates/deep_causality_ast)
4+
[![Docs.rs](https://docs.rs/deep_causality_ast/badge.svg)](https://docs.rs/deep_causality_ast)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6+
7+
A persistent, immutable, thread-safe tree data structure for the `deep_causality` project.
8+
9+
## Overview
10+
11+
This crate provides `ConstTree<T>`, a foundational Abstract Syntax Tree (AST) structure designed for efficiency and safety in concurrent environments. It is a persistent data structure, meaning that all modifications are non-destructive and return a new instance of the tree, sharing as much of the underlying data as possible with the original.
12+
13+
This copy-on-write behavior makes it highly efficient to pass trees around and create modified versions without incurring the cost of deep copies.
14+
15+
## Core Features
16+
17+
* **Persistent & Immutable**: Operations that "modify" the tree are non-destructive. They return a new, modified `ConstTree`, leaving the original unchanged.
18+
* **Efficient Cloning**: `ConstTree` is built on `std::sync::Arc`. Cloning a tree is a cheap, constant-time operation that simply increments a reference count.
19+
* **Thread-Safe**: It is `Send` and `Sync` (if `T` is `Send` and `Sync`), allowing it to be safely shared across threads without locks.
20+
* **Rich API**: Includes a comprehensive API for construction, traversal, searching, and functional mapping.
21+
* Multiple iteration strategies (pre-order, post-order, level-order, consuming).
22+
* Consuming (`into_map`) and non-consuming (`map`) mapping methods.
23+
* Monadic `join` method to flatten a `ConstTree<ConstTree<T>>`.
24+
25+
## Usage
26+
27+
Here is a basic example of how to create and interact with a `ConstTree`.
28+
29+
```rust
30+
use deep_causality_ast::ConstTree;
31+
32+
fn main() {
33+
// Create a tree with a root value and some children.
34+
let leaf1 = ConstTree::new(10);
35+
let leaf2 = ConstTree::from(20); // `From<T>` is implemented.
36+
let tree = ConstTree::with_children(5, vec![leaf1, leaf2]);
37+
38+
// --- Inspection ---
39+
assert_eq!(*tree.value(), 5);
40+
assert_eq!(tree.children().len(), 2);
41+
assert_eq!(tree.depth(), 2);
42+
assert_eq!(tree.size(), 3);
43+
44+
// --- Iteration ---
45+
// The tree can be iterated over in several ways.
46+
let values: Vec<i32> = tree.iter_pre_order().copied().collect();
47+
assert_eq!(values, vec![5, 10, 20]);
48+
49+
// --- Modification ---
50+
// `add_child` returns a new tree; the original is unchanged.
51+
let leaf3 = ConstTree::new(30);
52+
let modified_tree = tree.add_child(leaf3);
53+
54+
assert_eq!(tree.children().len(), 2); // Original is unaffected.
55+
assert_eq!(modified_tree.children().len(), 3);
56+
assert_eq!(*modified_tree.children()[2].value(), 30);
57+
58+
// --- Mapping ---
59+
// `map` creates a new tree with a different value type.
60+
let string_tree = modified_tree.map(&mut |v| format!("val: {}", v));
61+
assert_eq!(*string_tree.value(), "val: 5");
62+
assert_eq!(*string_tree.children()[0].value(), "val: 10");
63+
64+
// `into_map` consumes the tree.
65+
let owned_string_tree = modified_tree.into_map(|v| format!("owned: {}", v));
66+
assert_eq!(*owned_string_tree.value(), "owned: 5");
67+
}
68+
```
69+
70+
## 👨‍💻👩‍💻 Contribution
71+
72+
Contributions are welcomed especially related to documentation, example code, and fixes.
73+
If unsure where to start, just open an issue and ask.
74+
75+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in deep_causality by you,
76+
shall be licensed under the MIT licence, without any additional terms or conditions.
77+
78+
## 📜 Licence
79+
80+
This project is licensed under the [MIT license](LICENSE).
81+
82+
## 👮️ Security
83+
84+
For details about security, please read
85+
the [security policy](https://github.com/deepcausality-rs/deep_causality/blob/main/SECURITY.md).

deep_causality_ast/sbom.spdx.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"SPDXID": "SPDXRef-DOCUMENT",
3+
"creationInfo": {
4+
"created": "2025-11-02T05:24:54Z",
5+
"creators": [
6+
"Tool: cargo-sbom-v0.10.0"
7+
]
8+
},
9+
"dataLicense": "CC0-1.0",
10+
"documentNamespace": "https://spdx.org/spdxdocs/deep_causality_ast-143b8854-8261-4829-8353-3c7b513554b5",
11+
"files": [
12+
{
13+
"SPDXID": "SPDXRef-File-deep_causality_ast",
14+
"checksums": [
15+
{
16+
"algorithm": "SHA1",
17+
"checksumValue": "41606406cba51bcf7112fc6a62fe740ec2889181"
18+
}
19+
],
20+
"fileName": "./Cargo.lock",
21+
"fileTypes": [
22+
"SOURCE",
23+
"TEXT"
24+
]
25+
}
26+
],
27+
"name": "deep_causality_ast",
28+
"packages": [
29+
{
30+
"SPDXID": "SPDXRef-Package-deep--causality--ast-0.1.0",
31+
"description": "AST data structure for for deep_causality crate.",
32+
"downloadLocation": "NONE",
33+
"licenseConcluded": "MIT",
34+
"licenseDeclared": "MIT",
35+
"name": "deep_causality_ast",
36+
"versionInfo": "0.1.0"
37+
}
38+
],
39+
"relationships": [
40+
{
41+
"relatedSpdxElement": "SPDXRef-Package-deep--causality--ast-0.1.0",
42+
"relationshipType": "GENERATED_FROM",
43+
"spdxElementId": "SPDXRef-File-deep_causality_ast"
44+
},
45+
{
46+
"relatedSpdxElement": "SPDXRef-File-deep_causality_ast",
47+
"relationshipType": "DESCRIBES",
48+
"spdxElementId": "SPDXRef-DOCUMENT"
49+
}
50+
],
51+
"spdxVersion": "SPDX-2.3"
52+
}

0 commit comments

Comments
 (0)