(* Generated by coq-of-rust *)
Require Import CoqOfRust.CoqOfRust.

(* StructRecord
  {
    name := "Person";
    const_params := [];
    ty_params := [];
    fields :=
      [ ("id", Ty.path "u32"); ("name", Ty.path "alloc::string::String"); ("phone", Ty.path "u64")
      ];
  } *)

Module Impl_core_hash_Hash_for_hash_Person.
  Definition Self : Ty.t := Ty.path "hash::Person".
  
  (* Hash *)
  Definition hash (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [ __H ], [ self; state ] =>
      ltac:(M.monadic
        (let self := M.alloc (| self |) in
        let state := M.alloc (| state |) in
        M.read (|
          let~ _ :=
            M.alloc (|
              M.call_closure (|
                M.get_trait_method (| "core::hash::Hash", Ty.path "u32", [], "hash", [ __H ] |),
                [
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "hash::Person",
                    "id"
                  |);
                  M.read (| state |)
                ]
              |)
            |) in
          let~ _ :=
            M.alloc (|
              M.call_closure (|
                M.get_trait_method (|
                  "core::hash::Hash",
                  Ty.path "alloc::string::String",
                  [],
                  "hash",
                  [ __H ]
                |),
                [
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "hash::Person",
                    "name"
                  |);
                  M.read (| state |)
                ]
              |)
            |) in
          M.alloc (|
            M.call_closure (|
              M.get_trait_method (| "core::hash::Hash", Ty.path "u64", [], "hash", [ __H ] |),
              [
                M.SubPointer.get_struct_record_field (|
                  M.read (| self |),
                  "hash::Person",
                  "phone"
                |);
                M.read (| state |)
              ]
            |)
          |)
        |)))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom Implements :
    M.IsTraitInstance
      "core::hash::Hash"
      Self
      (* Trait polymorphic types *) []
      (* Instance *) [ ("hash", InstanceField.Method hash) ].
End Impl_core_hash_Hash_for_hash_Person.

(*
fn calculate_hash<T: Hash>(t: &T) -> u64 {
    let mut s = DefaultHasher::new();
    t.hash(&mut s);
    s.finish()
}
*)
Definition calculate_hash (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [ T ], [ t ] =>
    ltac:(M.monadic
      (let t := M.alloc (| t |) in
      M.read (|
        let~ s :=
          M.alloc (|
            M.call_closure (|
              M.get_associated_function (| Ty.path "std::hash::random::DefaultHasher", "new", [] |),
              []
            |)
          |) in
        let~ _ :=
          M.alloc (|
            M.call_closure (|
              M.get_trait_method (|
                "core::hash::Hash",
                T,
                [],
                "hash",
                [ Ty.path "std::hash::random::DefaultHasher" ]
              |),
              [ M.read (| t |); s ]
            |)
          |) in
        M.alloc (|
          M.call_closure (|
            M.get_trait_method (|
              "core::hash::Hasher",
              Ty.path "std::hash::random::DefaultHasher",
              [],
              "finish",
              []
            |),
            [ s ]
          |)
        |)
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_calculate_hash : M.IsFunction "hash::calculate_hash" calculate_hash.

(*
fn main() {
    let person1 = Person {
        id: 5,
        name: "Janet".to_string(),
        phone: 555_666_7777,
    };
    let person2 = Person {
        id: 5,
        name: "Bob".to_string(),
        phone: 555_666_7777,
    };

    assert!(calculate_hash(&person1) != calculate_hash(&person2));
}
*)
Definition main (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [], [] =>
    ltac:(M.monadic
      (M.read (|
        let~ person1 :=
          M.alloc (|
            Value.StructRecord
              "hash::Person"
              [
                ("id", Value.Integer IntegerKind.U32 5);
                ("name",
                  M.call_closure (|
                    M.get_trait_method (|
                      "alloc::string::ToString",
                      Ty.path "str",
                      [],
                      "to_string",
                      []
                    |),
                    [ M.read (| Value.String "Janet" |) ]
                  |));
                ("phone", Value.Integer IntegerKind.U64 5556667777)
              ]
          |) in
        let~ person2 :=
          M.alloc (|
            Value.StructRecord
              "hash::Person"
              [
                ("id", Value.Integer IntegerKind.U32 5);
                ("name",
                  M.call_closure (|
                    M.get_trait_method (|
                      "alloc::string::ToString",
                      Ty.path "str",
                      [],
                      "to_string",
                      []
                    |),
                    [ M.read (| Value.String "Bob" |) ]
                  |));
                ("phone", Value.Integer IntegerKind.U64 5556667777)
              ]
          |) in
        let~ _ :=
          M.match_operator (|
            M.alloc (| Value.Tuple [] |),
            [
              fun γ =>
                ltac:(M.monadic
                  (let γ :=
                    M.use
                      (M.alloc (|
                        UnOp.not (|
                          BinOp.ne (|
                            M.call_closure (|
                              M.get_function (|
                                "hash::calculate_hash",
                                [ Ty.path "hash::Person" ]
                              |),
                              [ person1 ]
                            |),
                            M.call_closure (|
                              M.get_function (|
                                "hash::calculate_hash",
                                [ Ty.path "hash::Person" ]
                              |),
                              [ person2 ]
                            |)
                          |)
                        |)
                      |)) in
                  let _ := M.is_constant_or_break_match (| M.read (| γ |), Value.Bool true |) in
                  M.alloc (|
                    M.never_to_any (|
                      M.call_closure (|
                        M.get_function (| "core::panicking::panic", [] |),
                        [
                          M.read (|
                            Value.String
                              "assertion failed: calculate_hash(&person1) != calculate_hash(&person2)"
                          |)
                        ]
                      |)
                    |)
                  |)));
              fun γ => ltac:(M.monadic (M.alloc (| Value.Tuple [] |)))
            ]
          |) in
        M.alloc (| Value.Tuple [] |)
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_main : M.IsFunction "hash::main" main.
