Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deterministic hashtables #1235

Merged
merged 1 commit into from
Apr 7, 2023
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ default = ["markdown", "repl", "doc"]
markdown = ["termimad"]
repl = ["rustyline", "rustyline-derive", "ansi_term"]
repl-wasm = ["wasm-bindgen", "js-sys", "serde_repr"]
doc = [ "comrak" ]
doc = ["comrak"]

[build-dependencies]
lalrpop = "0.19.9"
Expand Down Expand Up @@ -67,6 +67,7 @@ once_cell = "1.17.1"
typed-arena = "2.0.2"
malachite = {version = "0.3.2", features = ["enable_serde"] }
malachite-q = "0.3.2"
indexmap = {version = "1.9.3", features = ["serde"] }

[dev-dependencies]
pretty_assertions = "1.3.0"
Expand Down
4 changes: 2 additions & 2 deletions lsp/nls/src/linearization/building.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::debug;
use nickel_lang::{
cache::Cache,
identifier::Ident,
term::record::Field,
term::{record::Field, IndexMap},
typecheck::{linearization::LinearizationState, UnifType},
types::TypeF,
};
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'b> Building<'b> {
pub(super) fn register_fields(
&mut self,
current_file: FileId,
record_fields: &HashMap<Ident, Field>,
record_fields: &IndexMap<Ident, Field>,
record: ItemId,
env: &mut Environment,
) {
Expand Down
9 changes: 4 additions & 5 deletions src/deserialize.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Deserialization of an evaluated program to plain Rust types.

use malachite::{num::conversion::traits::RoundingFrom, rounding_modes::RoundingMode};
use std::collections::HashMap;
use std::iter::ExactSizeIterator;

use serde::de::{
Expand All @@ -12,7 +11,7 @@ use serde::de::{
use crate::identifier::Ident;
use crate::term::array::{self, Array};
use crate::term::record::Field;
use crate::term::{RichTerm, Term};
use crate::term::{IndexMap, RichTerm, Term};

macro_rules! deserialize_number {
($method:ident, $type:tt, $visit:ident) => {
Expand Down Expand Up @@ -387,12 +386,12 @@ where
}

struct RecordDeserializer {
iter: <HashMap<Ident, Field> as IntoIterator>::IntoIter,
iter: <IndexMap<Ident, Field> as IntoIterator>::IntoIter,
field: Option<Field>,
}

impl RecordDeserializer {
fn new(map: HashMap<Ident, Field>) -> Self {
fn new(map: IndexMap<Ident, Field>) -> Self {
RecordDeserializer {
iter: map.into_iter(),
field: None,
Expand Down Expand Up @@ -444,7 +443,7 @@ impl<'de> MapAccess<'de> for RecordDeserializer {
}

fn visit_record<'de, V>(
record: HashMap<Ident, Field>,
record: IndexMap<Ident, Field>,
visitor: V,
) -> Result<V::Value, RustDeserializationError>
where
Expand Down
43 changes: 21 additions & 22 deletions src/eval/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ use crate::label::{Label, MergeLabel};
use crate::position::TermPos;
use crate::term::{
record::{self, Field, FieldDeps, FieldMetadata, RecordAttrs, RecordData},
BinaryOp, RichTerm, Term, TypeAnnotation,
BinaryOp, IndexMap, RichTerm, Term, TypeAnnotation,
};
use crate::transform::Closurizable;
use std::collections::HashMap;

/// Merging mode. Merging is used both to combine standard data and to apply contracts defined as
/// records.
Expand Down Expand Up @@ -257,11 +256,11 @@ pub fn merge<C: Cache>(
});
}

let hashmap::SplitResult {
let split::SplitResult {
left,
center,
right,
} = hashmap::split(r1.fields, r2.fields);
} = split::split(r1.fields, r2.fields);

match mode {
MergeMode::Contract(label) if !r2.attrs.open && !left.is_empty() => {
Expand Down Expand Up @@ -301,7 +300,7 @@ Append `, ..` at the end of the record contract, as in `{some_field | SomeContra
.chain(right.keys())
.cloned()
.collect();
let mut m = HashMap::with_capacity(left.len() + center.len() + right.len());
let mut m = IndexMap::with_capacity(left.len() + center.len() + right.len());
let mut env = Environment::new();

// Merging recursive records is the one operation that may override recursive fields. To
Expand Down Expand Up @@ -671,24 +670,24 @@ impl RevertClosurize for Vec<PendingContract> {
}
}

pub mod hashmap {
use std::collections::HashMap;
pub mod split {
use crate::term::IndexMap;

pub struct SplitResult<K, V1, V2> {
pub left: HashMap<K, V1>,
pub center: HashMap<K, (V1, V2)>,
pub right: HashMap<K, V2>,
pub left: IndexMap<K, V1>,
pub center: IndexMap<K, (V1, V2)>,
pub right: IndexMap<K, V2>,
}

/// Split two hashmaps m1 and m2 in three parts (left,center,right), where left holds bindings
/// Split two maps m1 and m2 in three parts (left,center,right), where left holds bindings
/// `(key,value)` where key is not in `m2.keys()`, right is the dual (keys of m2 that are not
/// in m1), and center holds bindings for keys that are both in m1 and m2.
pub fn split<K, V1, V2>(m1: HashMap<K, V1>, m2: HashMap<K, V2>) -> SplitResult<K, V1, V2>
pub fn split<K, V1, V2>(m1: IndexMap<K, V1>, m2: IndexMap<K, V2>) -> SplitResult<K, V1, V2>
where
K: std::hash::Hash + Eq,
{
let mut left = HashMap::new();
let mut center = HashMap::new();
let mut left = IndexMap::new();
let mut center = IndexMap::new();
let mut right = m2;

for (key, value) in m1 {
Expand All @@ -712,8 +711,8 @@ pub mod hashmap {

#[test]
fn all_left() -> Result<(), String> {
let mut m1 = HashMap::new();
let m2 = HashMap::<isize, isize>::new();
let mut m1 = IndexMap::new();
let m2 = IndexMap::<isize, isize>::new();

m1.insert(1, 1);
let SplitResult {
Expand All @@ -735,8 +734,8 @@ pub mod hashmap {

#[test]
fn all_right() -> Result<(), String> {
let m1 = HashMap::<isize, isize>::new();
let mut m2 = HashMap::new();
let m1 = IndexMap::<isize, isize>::new();
let mut m2 = IndexMap::new();

m2.insert(1, 1);
let SplitResult {
Expand All @@ -760,8 +759,8 @@ pub mod hashmap {

#[test]
fn all_center() -> Result<(), String> {
let mut m1 = HashMap::new();
let mut m2 = HashMap::new();
let mut m1 = IndexMap::new();
let mut m2 = IndexMap::new();

m1.insert(1, 1);
m2.insert(1, 2);
Expand All @@ -786,8 +785,8 @@ pub mod hashmap {

#[test]
fn mixed() -> Result<(), String> {
let mut m1 = HashMap::new();
let mut m2 = HashMap::new();
let mut m1 = IndexMap::new();
let mut m2 = IndexMap::new();

m1.insert(1, 1);
m1.insert(2, 1);
Expand Down
12 changes: 6 additions & 6 deletions src/eval/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::{
make as mk_term,
record::{self, Field, FieldMetadata, RecordData},
string::NickelString,
BinaryOp, MergePriority, NAryOp, Number, PendingContract, RecordExtKind, RichTerm,
SharedTerm, StrChunk, Term, UnaryOp,
BinaryOp, IndexMap, MergePriority, NAryOp, Number, PendingContract, RecordExtKind,
RichTerm, SharedTerm, StrChunk, Term, UnaryOp,
},
transform::Closurizable,
};
Expand All @@ -47,7 +47,7 @@ use md5::digest::Digest;
use simple_counter::*;
use unicode_segmentation::UnicodeSegmentation;

use std::{collections::HashMap, convert::TryFrom, iter::Extend, rc::Rc};
use std::{convert::TryFrom, iter::Extend, rc::Rc};

generate_counter!(FreshVariableCounter, usize);

Expand Down Expand Up @@ -3484,11 +3484,11 @@ fn eq<C: Cache>(
(Term::SealingKey(s1), Term::SealingKey(s2)) => Ok(EqResult::Bool(s1 == s2)),
(Term::Enum(id1), Term::Enum(id2)) => Ok(EqResult::Bool(id1 == id2)),
(Term::Record(r1), Term::Record(r2)) => {
let merge::hashmap::SplitResult {
let merge::split::SplitResult {
left,
center,
right,
} = merge::hashmap::split(r1.fields, r2.fields);
} = merge::split::split(r1.fields, r2.fields);

// As for other record operations, we ignore optional fields without a definition.
if !left.values().all(Field::is_empty_optional)
Expand Down Expand Up @@ -3662,7 +3662,7 @@ impl RecordDataExt for RecordData {
where
F: FnMut(Ident, RichTerm) -> RichTerm,
{
let fields: Result<HashMap<_, _>, _> = self
let fields: Result<IndexMap<_, _>, _> = self
.fields
.into_iter()
.filter_map(|(id, field)| {
Expand Down
3 changes: 1 addition & 2 deletions src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
//! corresponding more precise return type. Other rules that produce or just
//! propagate general uniterms have to return a `UniTerm`.
use std::{
collections::HashMap,
ffi::OsString,
convert::TryFrom,
};
Expand Down Expand Up @@ -290,7 +289,7 @@ Applicative: UniTerm = {
NOpPre<AsTerm<RecordOperand>>,
RecordOperand,
"match" "{" <cases: (MatchCase ",")*> <last: MatchCase?> "}" => {
let mut acc = HashMap::with_capacity(cases.len());
let mut acc = IndexMap::with_capacity(cases.len());
let mut default = None;

for case in cases.into_iter().map(|x| x.0).chain(last.into_iter()) {
Expand Down
14 changes: 6 additions & 8 deletions src/parser/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Various helpers and companion code for the parser are put here to keep the grammar definition
//! uncluttered.
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use indexmap::map::Entry;
use std::fmt::Debug;
use std::rc::Rc;

Expand All @@ -19,8 +18,7 @@ use crate::{
term::{
make as mk_term,
record::{Field, FieldMetadata, RecordAttrs, RecordData},
BinaryOp, LabeledType, LetMetadata, Rational, RichTerm, StrChunk, Term, TypeAnnotation,
UnaryOp,
*,
},
types::{TypeF, Types},
};
Expand Down Expand Up @@ -146,7 +144,7 @@ impl FieldDef {

match path_elem {
FieldPathElem::Ident(id) => {
let mut fields = HashMap::new();
let mut fields = IndexMap::new();
fields.insert(id, acc);
Field::from(RichTerm::new(
Term::Record(RecordData {
Expand All @@ -161,7 +159,7 @@ impl FieldDef {

if let Some(static_access) = static_access {
let id = Ident::new_with_pos(static_access, exp.pos);
let mut fields = HashMap::new();
let mut fields = IndexMap::new();
fields.insert(id, acc);
Field::from(RichTerm::new(
Term::Record(RecordData {
Expand Down Expand Up @@ -446,10 +444,10 @@ pub fn build_record<I>(fields: I, attrs: RecordAttrs) -> Term
where
I: IntoIterator<Item = (FieldPathElem, Field)> + Debug,
{
let mut static_fields = HashMap::new();
let mut static_fields = IndexMap::new();
let mut dynamic_fields = Vec::new();

fn insert_static_field(static_fields: &mut HashMap<Ident, Field>, id: Ident, field: Field) {
fn insert_static_field(static_fields: &mut IndexMap<Ident, Field>, id: Ident, field: Field) {
match static_fields.entry(id) {
Entry::Occupied(mut occpd) => {
// temporarily putting an empty field in the entry to take the previous value.
Expand Down
14 changes: 8 additions & 6 deletions src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ use crate::parser::lexer::KEYWORDS;

use crate::term::{
record::{Field, FieldMetadata},
BinaryOp, MergePriority, Number, RichTerm, StrChunk, Term, TypeAnnotation, UnaryOp,
*,
};
use crate::types::{EnumRows, EnumRowsF, RecordRowF, RecordRows, RecordRowsF, TypeF, Types};
use crate::types::*;

use malachite::num::{basic::traits::Zero, conversion::traits::ToSci};
pub use pretty::{DocAllocator, DocBuilder, Pretty};
use regex::Regex;

use std::collections::HashMap;

#[derive(Clone, Copy, Eq, PartialEq)]
enum StringRenderStyle {
Monoline,
Expand All @@ -39,7 +37,7 @@ fn min_interpolate_sign(text: &str) -> usize {
.unwrap_or(1)
}

fn sorted_map<K: Ord, V>(m: &'_ HashMap<K, V>) -> Vec<(&'_ K, &'_ V)> {
fn sorted_map<K: Ord, V>(m: &'_ IndexMap<K, V>) -> Vec<(&'_ K, &'_ V)> {
let mut ret: Vec<(&K, &V)> = m.iter().collect();
ret.sort_by_key(|(k, _)| *k);
ret
Expand Down Expand Up @@ -229,7 +227,11 @@ where
.append(self.text(","))
}

fn fields(&'a self, fields: &HashMap<Ident, Field>, with_doc: bool) -> DocBuilder<'a, Self, A> {
fn fields(
&'a self,
fields: &IndexMap<Ident, Field>,
with_doc: bool,
) -> DocBuilder<'a, Self, A> {
self.intersperse(
sorted_map(fields)
.iter()
Expand Down
7 changes: 3 additions & 4 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
term::{
array::{Array, ArrayAttrs},
record::RecordData,
Number, RichTerm, Term, TypeAnnotation,
IndexMap, Number, RichTerm, Term, TypeAnnotation,
},
};

Expand All @@ -17,7 +17,7 @@ use serde::{

use malachite::num::conversion::traits::IsInteger;

use std::{collections::HashMap, fmt, io, rc::Rc, str::FromStr};
use std::{fmt, io, rc::Rc, str::FromStr};

/// Available export formats.
// If you add or remove variants, remember to update the CLI docs in `src/bin/nickel.rs'
Expand Down Expand Up @@ -117,7 +117,6 @@ where
}

/// Serializer for a record. Serialize fields in alphabetical order to get a deterministic output
/// (by default, `HashMap`'s randomness implies a randomized order of fields in the output).
pub fn serialize_record<S>(record: &RecordData, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down Expand Up @@ -147,7 +146,7 @@ pub fn deserialize_record<'de, D>(deserializer: D) -> Result<RecordData, D::Erro
where
D: Deserializer<'de>,
{
let fields = HashMap::deserialize(deserializer)?;
let fields = IndexMap::deserialize(deserializer)?;
Ok(RecordData::with_field_values(fields))
}

Expand Down
Loading