Skip to content

Commit 6bebb38

Browse files
committed
Move TypeName conversion to jet module
1 parent 3312b08 commit 6bebb38

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

src/core/types.rs

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::{cell::RefCell, cmp, fmt, mem, rc::Rc, sync::Arc};
55

66
use crate::core::{Term, TypedNode, TypedProgram};
7-
use crate::jet::type_name::TypeName;
87
use crate::jet::Application;
98
use crate::merkle::cmr;
109
use crate::merkle::common::{MerkleRoot, TypeMerkleRoot};
@@ -14,14 +13,14 @@ use crate::Error;
1413
use super::term::UntypedProgram;
1514

1615
#[derive(Clone, Debug)]
17-
enum Type {
16+
pub(crate) enum Type {
1817
Unit,
1918
Sum(RcVar, RcVar),
2019
Product(RcVar, RcVar),
2120
}
2221

2322
impl Type {
24-
fn into_rcvar(self) -> RcVar {
23+
pub(crate) fn into_rcvar(self) -> RcVar {
2524
Rc::new(RefCell::new(UnificationVar::concrete(self)))
2625
}
2726
}
@@ -218,7 +217,7 @@ impl FinalType {
218217
}
219218

220219
#[derive(Clone)]
221-
enum Variable {
220+
pub(crate) enum Variable {
222221
/// Free variable
223222
Free,
224223
/// Bound to some type (which may itself contain other free variables,
@@ -243,7 +242,7 @@ impl fmt::Debug for Variable {
243242
}
244243
}
245244

246-
struct UnificationVar {
245+
pub(crate) struct UnificationVar {
247246
var: Variable,
248247
rank: usize,
249248
}
@@ -254,7 +253,7 @@ impl fmt::Debug for UnificationVar {
254253
}
255254
}
256255

257-
type RcVar = Rc<RefCell<UnificationVar>>;
256+
pub(crate) type RcVar = Rc<RefCell<UnificationVar>>;
258257

259258
impl UnificationVar {
260259
fn free() -> UnificationVar {
@@ -375,49 +374,6 @@ struct UnificationArrow {
375374
target: Rc<RefCell<UnificationVar>>,
376375
}
377376

378-
// b'1' = 49
379-
// b'2' = 50
380-
// b'i' = 105
381-
// b'l' = 108
382-
// b'h' = 104
383-
// b'+' = 43
384-
// b'*' = 42
385-
/// Convert a [`TypeName`] into a [`Type`]
386-
fn type_from_name(name: &TypeName, pow2s: &[RcVar]) -> Type {
387-
let it = name.0.iter().rev();
388-
let mut stack = Vec::new();
389-
390-
for c in it {
391-
match c {
392-
b'1' => stack.push(Type::Unit),
393-
b'2' => {
394-
let unit = Type::Unit.into_rcvar();
395-
stack.push(Type::Sum(unit.clone(), unit))
396-
}
397-
b'i' => stack.push(Type::Product(pow2s[4].clone(), pow2s[4].clone())),
398-
b'l' => stack.push(Type::Product(pow2s[5].clone(), pow2s[5].clone())),
399-
b'h' => stack.push(Type::Product(pow2s[7].clone(), pow2s[7].clone())),
400-
b'+' | b'*' => {
401-
let left = stack.pop().expect("Illegal type name syntax!").into_rcvar();
402-
let right = stack.pop().expect("Illegal type name syntax!").into_rcvar();
403-
404-
match c {
405-
b'+' => stack.push(Type::Sum(left, right)),
406-
b'*' => stack.push(Type::Product(left, right)),
407-
_ => unreachable!(),
408-
}
409-
}
410-
_ => panic!("Illegal type name syntax!"),
411-
}
412-
}
413-
414-
if stack.len() == 1 {
415-
stack.pop().unwrap()
416-
} else {
417-
panic!("Illegal type name syntax!")
418-
}
419-
}
420-
421377
/// Attach types to all nodes in a program
422378
pub(crate) fn type_check<Witness, App: Application>(
423379
program: UntypedProgram<Witness, App>,
@@ -557,9 +513,9 @@ pub(crate) fn type_check<Witness, App: Application>(
557513
unify(rcs[j].target.clone(), var_d)?;
558514
}
559515
Term::Jet(jet) => {
560-
bind(&node.source, type_from_name(&jet.source_ty, &pow2s[..]))?;
516+
bind(&node.source, jet.source_ty.to_type(&pow2s[..]))?;
561517

562-
bind(&node.target, type_from_name(&jet.target_ty, &pow2s[..]))?;
518+
bind(&node.target, jet.target_ty.to_type(&pow2s[..]))?;
563519
}
564520
Term::Witness(..) | Term::Hidden(..) | Term::Fail(..) => {
565521
// No type constraints

src/jet/type_name.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
//!
1717
//! Source and target types of jet nodes need to be specified manually.
1818
19+
use crate::core::types::RcVar;
20+
use crate::core::types::Type;
21+
1922
/// Byte-based specification of a Simplicity type.
2023
///
2124
/// Because jets are black boxes, the type inference engine has no access to their
@@ -35,3 +38,48 @@
3538
///
3639
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
3740
pub struct TypeName(pub(crate) &'static [u8]);
41+
42+
impl TypeName {
43+
// b'1' = 49
44+
// b'2' = 50
45+
// b'i' = 105
46+
// b'l' = 108
47+
// b'h' = 104
48+
// b'+' = 43
49+
// b'*' = 42
50+
/// Convert the [`TypeName`] into a [`Type`]
51+
pub(crate) fn to_type(&self, pow2s: &[RcVar]) -> Type {
52+
let it = self.0.iter().rev();
53+
let mut stack = Vec::new();
54+
55+
for c in it {
56+
match c {
57+
b'1' => stack.push(Type::Unit),
58+
b'2' => {
59+
let unit = Type::Unit.into_rcvar();
60+
stack.push(Type::Sum(unit.clone(), unit))
61+
}
62+
b'i' => stack.push(Type::Product(pow2s[4].clone(), pow2s[4].clone())),
63+
b'l' => stack.push(Type::Product(pow2s[5].clone(), pow2s[5].clone())),
64+
b'h' => stack.push(Type::Product(pow2s[7].clone(), pow2s[7].clone())),
65+
b'+' | b'*' => {
66+
let left = stack.pop().expect("Illegal type name syntax!").into_rcvar();
67+
let right = stack.pop().expect("Illegal type name syntax!").into_rcvar();
68+
69+
match c {
70+
b'+' => stack.push(Type::Sum(left, right)),
71+
b'*' => stack.push(Type::Product(left, right)),
72+
_ => unreachable!(),
73+
}
74+
}
75+
_ => panic!("Illegal type name syntax!"),
76+
}
77+
}
78+
79+
if stack.len() == 1 {
80+
stack.pop().unwrap()
81+
} else {
82+
panic!("Illegal type name syntax!")
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)