aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-omegese-constraint / src / signature.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! The entry function's public signature, as the task gives it (the `entry`, `params` and `ret` |
| 17 | //! lines the user message already contains), and the Omegese header line built from it. |
| 18 | |
| 19 | /// Parameter names given to a signature that names none, in order (Omegese allows 6 parameters). |
| 20 | const PARAM_NAMES: [&str; 6] = ["a", "b", "c", "d", "e", "f"]; |
| 21 | |
| 22 | /// The entry function: its name, its parameters (name, Omegese type word) and its return type |
| 23 | /// word (`None`: void). |
| 24 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 25 | pub struct Signature { |
| 26 | pub entry: String, |
| 27 | pub params: Vec<(String, String)>, |
| 28 | pub ret: Option<String>, |
| 29 | } |
| 30 | |
| 31 | /// The IR type name the task writes, as the one-token Omegese word (OMEGESE.md section 3). |
| 32 | fn omegese_type(ir: &str) -> Option<&'static str> { |
| 33 | Some(match ir { |
| 34 | "bool" => "bool", |
| 35 | "u8" => "uchar", |
| 36 | "u16" => "ushort", |
| 37 | "u32" => "uint", |
| 38 | "u64" => "ulong", |
| 39 | "i8" => "byte", |
| 40 | "i16" => "short", |
| 41 | "i32" => "int", |
| 42 | "i64" => "long", |
| 43 | _ => return None, |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | impl Signature { |
| 48 | /// From the task's signature lines: `entry <name>`, `params <ir type>*`, `ret <ir type>` |
| 49 | /// (`ret` alone or `ret void`: void). Other lines are ignored. Parameters are named |
| 50 | /// `a`, `b`, ... in order. |
| 51 | pub fn from_requirement(text: &str) -> Result<Self, String> { |
| 52 | let (mut entry, mut params, mut ret) = (None, None, None); |
| 53 | for line in text.lines() { |
| 54 | let mut w = line.split_whitespace(); |
| 55 | match w.next() { |
| 56 | Some("entry") => entry = Some(w.collect::<Vec<_>>()), |
| 57 | Some("params") => params = Some(w.collect::<Vec<_>>()), |
| 58 | Some("ret") => ret = Some(w.collect::<Vec<_>>()), |
| 59 | _ => {} |
| 60 | } |
| 61 | } |
| 62 | let entry = match entry.as_deref() { |
| 63 | Some([name]) => name.to_string(), |
| 64 | _ => return Err("the signature needs one `entry <name>` line".into()), |
| 65 | }; |
| 66 | let params = params.ok_or("the signature has no `params` line")?; |
| 67 | if params.len() > PARAM_NAMES.len() { |
| 68 | return Err(format!("{} parameters; Omegese allows 6", params.len())); |
| 69 | } |
| 70 | let params = params |
| 71 | .iter() |
| 72 | .zip(PARAM_NAMES) |
| 73 | .map(|(t, n)| { |
| 74 | omegese_type(t) |
| 75 | .map(|t| (n.to_string(), t.to_string())) |
| 76 | .ok_or_else(|| format!("unknown parameter type {t:?}")) |
| 77 | }) |
| 78 | .collect::<Result<Vec<_>, _>>()?; |
| 79 | let ret = match ret.as_deref() { |
| 80 | Some([]) | Some(["void"]) => None, |
| 81 | Some([t]) => Some( |
| 82 | omegese_type(t) |
| 83 | .ok_or_else(|| format!("unknown return type {t:?}"))? |
| 84 | .to_string(), |
| 85 | ), |
| 86 | _ => return Err("the signature needs one `ret <type>` line".into()), |
| 87 | }; |
| 88 | if params.iter().any(|(n, _)| *n == entry) { |
| 89 | return Err(format!( |
| 90 | "the entry {entry:?} has the name of a parameter; name the parameters explicitly" |
| 91 | )); |
| 92 | } |
| 93 | Ok(Self { entry, params, ret }) |
| 94 | } |
| 95 | |
| 96 | /// From a written header line, `fn <name> (<param> <type>)* to <type>|void` (an array type is |
| 97 | /// `own|edit|view <type> <len>`), names and type words kept as written. |
| 98 | pub fn from_header(line: &str) -> Result<Self, String> { |
| 99 | let w: Vec<&str> = line.split_whitespace().collect(); |
| 100 | let bad = || format!("not a header line: {line:?}"); |
| 101 | let (Some(&"fn"), Some(entry)) = (w.first(), w.get(1)) else { |
| 102 | return Err(bad()); |
| 103 | }; |
| 104 | let to = w.iter().position(|&x| x == "to").ok_or_else(bad)?; |
| 105 | let (mut mid, tail) = (&w[2..to], &w[to + 1..]); |
| 106 | if tail.len() != 1 { |
| 107 | return Err(bad()); |
| 108 | } |
| 109 | let mut params = Vec::new(); |
| 110 | while let Some((name, rest)) = mid.split_first() { |
| 111 | // A type is one word, or `own|edit|view <type> <len>` for an array. |
| 112 | let n = match rest.first() { |
| 113 | Some(&"own" | &"edit" | &"view") => 3, |
| 114 | Some(_) => 1, |
| 115 | None => return Err(bad()), |
| 116 | }; |
| 117 | if rest.len() < n { |
| 118 | return Err(bad()); |
| 119 | } |
| 120 | params.push((name.to_string(), rest[..n].join(" "))); |
| 121 | mid = &rest[n..]; |
| 122 | } |
| 123 | Ok(Self { |
| 124 | entry: entry.to_string(), |
| 125 | params, |
| 126 | ret: (tail[0] != "void").then(|| tail[0].to_string()), |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | /// `fn <entry> <name> <type> ... to <ret>` and a newline. |
| 131 | pub fn header(&self) -> String { |
| 132 | let mut s = format!("fn {}", self.entry); |
| 133 | for (n, t) in &self.params { |
| 134 | s.push(' '); |
| 135 | s.push_str(n); |
| 136 | s.push(' '); |
| 137 | s.push_str(t); |
| 138 | } |
| 139 | s.push_str(" to "); |
| 140 | s.push_str(self.ret.as_deref().unwrap_or("void")); |
| 141 | s.push('\n'); |
| 142 | s |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | #[cfg(test)] |
| 147 | mod tests { |
| 148 | use super::*; |
| 149 | |
| 150 | #[test] |
| 151 | fn requirement_lines_make_the_header() { |
| 152 | let s = Signature::from_requirement("entry clamp_sub\nparams u32 u32\nret u32\n").unwrap(); |
| 153 | assert_eq!(s.header(), "fn clamp_sub a uint b uint to uint\n"); |
| 154 | let s = Signature::from_requirement("entry go\nparams\nret\n").unwrap(); |
| 155 | assert_eq!(s.header(), "fn go to void\n"); |
| 156 | let s = Signature::from_requirement("x\nentry t\nparams i16 u8 bool\nret i64\n").unwrap(); |
| 157 | assert_eq!(s.header(), "fn t a short b uchar c bool to long\n"); |
| 158 | for bad in [ |
| 159 | "params u32\nret u32", |
| 160 | "entry x\nret u32", |
| 161 | "entry x\nparams u33\nret u32", |
| 162 | "entry x\nparams u32\nret f32", |
| 163 | "entry b\nparams u32 u32\nret u32", |
| 164 | "entry x\nparams u8 u8 u8 u8 u8 u8 u8\nret u8", |
| 165 | ] { |
| 166 | assert!(Signature::from_requirement(bad).is_err(), "{bad:?}"); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn header_lines_round_trip() { |
| 172 | for h in [ |
| 173 | "fn gcd a uint b uint to uint\n", |
| 174 | "fn dot u view long 3 w view long 3 to long\n", |
| 175 | "fn go to void\n", |
| 176 | ] { |
| 177 | assert_eq!(Signature::from_header(h).unwrap().header(), h); |
| 178 | } |
| 179 | for bad in [ |
| 180 | "ret a", |
| 181 | "fn f a to uint", |
| 182 | "fn f a view uint to uint", |
| 183 | "fn f a uint to", |
| 184 | ] { |
| 185 | assert!(Signature::from_header(bad).is_err(), "{bad}"); |
| 186 | } |
| 187 | } |
| 188 | } |