feat: unify tuples and stdlib updates
This commit is contained in:
parent
3f47a1f4b8
commit
45990f1f84
|
@ -856,9 +856,17 @@ impl<'comments> Formatter<'comments> {
|
||||||
match args {
|
match args {
|
||||||
[arg] if is_breakable_expr(&arg.value) => self
|
[arg] if is_breakable_expr(&arg.value) => self
|
||||||
.expr(fun)
|
.expr(fun)
|
||||||
.append(if needs_curly { "{" } else { "(" })
|
.append(if needs_curly {
|
||||||
|
break_(" {", " { ")
|
||||||
|
} else {
|
||||||
|
break_("(", "(")
|
||||||
|
})
|
||||||
.append(self.call_arg(arg, needs_curly))
|
.append(self.call_arg(arg, needs_curly))
|
||||||
.append(if needs_curly { "}" } else { ")" })
|
.append(if needs_curly {
|
||||||
|
break_("}", " }")
|
||||||
|
} else {
|
||||||
|
break_(")", ")")
|
||||||
|
})
|
||||||
.group(),
|
.group(),
|
||||||
|
|
||||||
_ => self
|
_ => self
|
||||||
|
|
|
@ -1200,6 +1200,19 @@ impl<'a> Environment<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(Type::Tuple { elems: elems1, .. }, Type::Tuple { elems: elems2, .. })
|
||||||
|
if elems1.len() == elems2.len() =>
|
||||||
|
{
|
||||||
|
for (a, b) in elems1.iter().zip(elems2) {
|
||||||
|
unify_enclosed_type(
|
||||||
|
t1.clone(),
|
||||||
|
t2.clone(),
|
||||||
|
self.unify(a.clone(), b.clone(), location),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
(
|
(
|
||||||
Type::Fn {
|
Type::Fn {
|
||||||
args: args1,
|
args: args1,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use aiken/map.{Map}
|
||||||
|
|
||||||
pub type ScriptContext(purpose) {
|
pub type ScriptContext(purpose) {
|
||||||
transaction: Transaction,
|
transaction: Transaction,
|
||||||
purpose: purpose,
|
purpose: purpose,
|
||||||
|
@ -39,11 +41,11 @@ pub type Transaction {
|
||||||
fee: Value,
|
fee: Value,
|
||||||
mint: Value,
|
mint: Value,
|
||||||
certificates: List(Certificate),
|
certificates: List(Certificate),
|
||||||
withdrawals: List(Pair(StakeCredential, Int)),
|
withdrawals: Map(StakeCredential, Int),
|
||||||
validity_range: Interval(Int),
|
validity_range: Interval(Int),
|
||||||
extra_signatories: List(PublicKeyHash),
|
extra_signatories: List(PublicKeyHash),
|
||||||
redeemers: List(Pair(ScriptPurpose, Redeemer)),
|
redeemers: Map(ScriptPurpose, Redeemer),
|
||||||
datums: List(Pair(Hash(Data), Data)),
|
datums: Map(Hash(Data), Data),
|
||||||
id: TransactionId,
|
id: TransactionId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,11 +109,8 @@ pub type DatumOption {
|
||||||
pub type AssetName =
|
pub type AssetName =
|
||||||
ByteArray
|
ByteArray
|
||||||
|
|
||||||
pub type Pair(a, b) =
|
|
||||||
Nil
|
|
||||||
|
|
||||||
pub type Value =
|
pub type Value =
|
||||||
List(Pair(PolicyId, List(Pair(AssetName, Int))))
|
Map(PolicyId, Map(AssetName, Int))
|
||||||
|
|
||||||
pub type Certificate {
|
pub type Certificate {
|
||||||
CredentialRegistration { delegator: StakeCredential }
|
CredentialRegistration { delegator: StakeCredential }
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
pub opaque type Map(key, value) {
|
||||||
|
inner: List(#(key, value)),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new map
|
||||||
|
pub fn new() {
|
||||||
|
Map { inner: [] }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the inner list holding the map data
|
||||||
|
pub fn to_list(m: Map(key, value)) -> List(#(key, value)) {
|
||||||
|
m.inner
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a value in the map by a key
|
||||||
|
///
|
||||||
|
/// ```aiken
|
||||||
|
/// use aiken/map
|
||||||
|
///
|
||||||
|
/// let info = map.new() |> map.insert(key: "name", value: "Aiken")
|
||||||
|
///
|
||||||
|
/// asset Some(x) = map.get(in: info, by: "key")
|
||||||
|
/// ```
|
||||||
|
pub fn get(in m: Map(key, value), by k: key) -> Option(value) {
|
||||||
|
do_get(m.inner, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn do_get(elems: List(#(key, value)), k: key) -> Option(value) {
|
||||||
|
when elems is {
|
||||||
|
[] -> None
|
||||||
|
[#(first, second), ..xs] ->
|
||||||
|
if first == k {
|
||||||
|
Some(second)
|
||||||
|
} else {
|
||||||
|
do_get(xs, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Insert a value in the map by a key
|
||||||
|
///
|
||||||
|
/// ```aiken
|
||||||
|
/// use aiken/map
|
||||||
|
///
|
||||||
|
/// map.new() |> map.insert(key: "name", value: "Aiken")
|
||||||
|
/// ```
|
||||||
|
pub fn insert(
|
||||||
|
in m: Map(key, value),
|
||||||
|
key k: key,
|
||||||
|
value v: value,
|
||||||
|
) -> Map(key, value) {
|
||||||
|
if contains(m, k) {
|
||||||
|
m
|
||||||
|
} else {
|
||||||
|
Map { inner: [#(k, v), ..m.inner] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if a key exists in the map
|
||||||
|
///
|
||||||
|
/// ```aiken
|
||||||
|
/// use aiken/map
|
||||||
|
///
|
||||||
|
/// let info = map.new() |> map.insert(key: "name", value: "Aiken")
|
||||||
|
///
|
||||||
|
/// asset Some(x) = map.contains(in: info, key: "key")
|
||||||
|
/// ```
|
||||||
|
pub fn contains(in m: Map(key, value), key k: key) -> Bool {
|
||||||
|
do_contains(m.inner, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn do_contains(elems: List(#(key, value)), k: key) -> Bool {
|
||||||
|
when elems is {
|
||||||
|
[] -> False
|
||||||
|
[#(first, _), ..xs] ->
|
||||||
|
if first == k {
|
||||||
|
True
|
||||||
|
} else {
|
||||||
|
do_contains(xs, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue