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

Module iter.
  Module adapters.
    Module filter.
      (* StructRecord
        {
          name := "Filter";
          const_params := [];
          ty_params := [ "I"; "P" ];
          fields := [ ("iter", I); ("predicate", P) ];
        } *)
      
      Module Impl_core_clone_Clone_where_core_clone_Clone_I_where_core_clone_Clone_P_for_core_iter_adapters_filter_Filter_I_P.
        Definition Self (I P : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        (* Clone *)
        Definition clone (I P : Ty.t) (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              Value.StructRecord
                "core::iter::adapters::filter::Filter"
                [
                  ("iter",
                    M.call_closure (|
                      M.get_trait_method (| "core::clone::Clone", I, [], "clone", [] |),
                      [
                        M.SubPointer.get_struct_record_field (|
                          M.read (| self |),
                          "core::iter::adapters::filter::Filter",
                          "iter"
                        |)
                      ]
                    |));
                  ("predicate",
                    M.call_closure (|
                      M.get_trait_method (| "core::clone::Clone", P, [], "clone", [] |),
                      [
                        M.SubPointer.get_struct_record_field (|
                          M.read (| self |),
                          "core::iter::adapters::filter::Filter",
                          "predicate"
                        |)
                      ]
                    |))
                ]))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        Axiom Implements :
          forall (I P : Ty.t),
          M.IsTraitInstance
            "core::clone::Clone"
            (Self I P)
            (* Trait polymorphic types *) []
            (* Instance *) [ ("clone", InstanceField.Method (clone I P)) ].
      End Impl_core_clone_Clone_where_core_clone_Clone_I_where_core_clone_Clone_P_for_core_iter_adapters_filter_Filter_I_P.
      
      Module Impl_core_iter_adapters_filter_Filter_I_P.
        Definition Self (I P : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        (*
            pub(in crate::iter) fn new(iter: I, predicate: P) -> Filter<I, P> {
                Filter { iter, predicate }
            }
        *)
        Definition new (I P : Ty.t) (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [], [ iter; predicate ] =>
            ltac:(M.monadic
              (let iter := M.alloc (| iter |) in
              let predicate := M.alloc (| predicate |) in
              Value.StructRecord
                "core::iter::adapters::filter::Filter"
                [ ("iter", M.read (| iter |)); ("predicate", M.read (| predicate |)) ]))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        Axiom AssociatedFunction_new :
          forall (I P : Ty.t),
          M.IsAssociatedFunction (Self I P) "new" (new I P).
        (*
            fn next_chunk_dropless<const N: usize>(
                &mut self,
            ) -> Result<[I::Item; N], array::IntoIter<I::Item, N>> {
                let mut array: [MaybeUninit<I::Item>; N] = [const { MaybeUninit::uninit() }; N];
                let mut initialized = 0;
        
                let result = self.iter.try_for_each(|element| {
                    let idx = initialized;
                    // branchless index update combined with unconditionally copying the value even when
                    // it is filtered reduces branching and dependencies in the loop.
                    initialized = idx + (self.predicate)(&element) as usize;
                    // SAFETY: Loop conditions ensure the index is in bounds.
                    unsafe { array.get_unchecked_mut(idx) }.write(element);
        
                    if initialized < N { ControlFlow::Continue(()) } else { ControlFlow::Break(()) }
                });
        
                match result {
                    ControlFlow::Break(()) => {
                        // SAFETY: The loop above is only explicitly broken when the array has been fully initialized
                        Ok(unsafe { MaybeUninit::array_assume_init(array) })
                    }
                    ControlFlow::Continue(()) => {
                        // SAFETY: The range is in bounds since the loop breaks when reaching N elements.
                        Err(unsafe { array::IntoIter::new_unchecked(array, 0..initialized) })
                    }
                }
            }
        *)
        Definition next_chunk_dropless
            (I P : Ty.t)
            (ε : list Value.t)
            (τ : list Ty.t)
            (α : list Value.t)
            : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [ N ], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              M.read (|
                let~ array :=
                  M.alloc (|
                    repeat (|
                      M.read (|
                        M.get_constant (|
                          "core::iter::adapters::filter::next_chunk_dropless_discriminant"
                        |)
                      |),
                      N
                    |)
                  |) in
                let~ initialized := M.alloc (| Value.Integer IntegerKind.Usize 0 |) in
                let~ result :=
                  M.alloc (|
                    M.call_closure (|
                      M.get_trait_method (|
                        "core::iter::traits::iterator::Iterator",
                        I,
                        [],
                        "try_for_each",
                        [
                          Ty.function
                            [ Ty.tuple [ Ty.associated ] ]
                            (Ty.apply
                              (Ty.path "core::ops::control_flow::ControlFlow")
                              []
                              [ Ty.tuple []; Ty.tuple [] ]);
                          Ty.apply
                            (Ty.path "core::ops::control_flow::ControlFlow")
                            []
                            [ Ty.tuple []; Ty.tuple [] ]
                        ]
                      |),
                      [
                        M.SubPointer.get_struct_record_field (|
                          M.read (| self |),
                          "core::iter::adapters::filter::Filter",
                          "iter"
                        |);
                        M.closure
                          (fun γ =>
                            ltac:(M.monadic
                              match γ with
                              | [ α0 ] =>
                                ltac:(M.monadic
                                  (M.match_operator (|
                                    M.alloc (| α0 |),
                                    [
                                      fun γ =>
                                        ltac:(M.monadic
                                          (let element := M.copy (| γ |) in
                                          M.read (|
                                            let~ idx := M.copy (| initialized |) in
                                            let~ _ :=
                                              M.write (|
                                                initialized,
                                                BinOp.Wrap.add (|
                                                  M.read (| idx |),
                                                  M.rust_cast
                                                    (M.call_closure (|
                                                      M.get_trait_method (|
                                                        "core::ops::function::FnMut",
                                                        P,
                                                        [
                                                          Ty.tuple
                                                            [
                                                              Ty.apply
                                                                (Ty.path "&")
                                                                []
                                                                [ Ty.associated ]
                                                            ]
                                                        ],
                                                        "call_mut",
                                                        []
                                                      |),
                                                      [
                                                        M.SubPointer.get_struct_record_field (|
                                                          M.read (| self |),
                                                          "core::iter::adapters::filter::Filter",
                                                          "predicate"
                                                        |);
                                                        Value.Tuple [ element ]
                                                      ]
                                                    |))
                                                |)
                                              |) in
                                            let~ _ :=
                                              M.alloc (|
                                                M.call_closure (|
                                                  M.get_associated_function (|
                                                    Ty.apply
                                                      (Ty.path
                                                        "core::mem::maybe_uninit::MaybeUninit")
                                                      []
                                                      [ Ty.associated ],
                                                    "write",
                                                    []
                                                  |),
                                                  [
                                                    M.call_closure (|
                                                      M.get_associated_function (|
                                                        Ty.apply
                                                          (Ty.path "slice")
                                                          []
                                                          [
                                                            Ty.apply
                                                              (Ty.path
                                                                "core::mem::maybe_uninit::MaybeUninit")
                                                              []
                                                              [ Ty.associated ]
                                                          ],
                                                        "get_unchecked_mut",
                                                        [ Ty.path "usize" ]
                                                      |),
                                                      [ array; M.read (| idx |) ]
                                                    |);
                                                    M.read (| element |)
                                                  ]
                                                |)
                                              |) in
                                            M.match_operator (|
                                              M.alloc (| Value.Tuple [] |),
                                              [
                                                fun γ =>
                                                  ltac:(M.monadic
                                                    (let γ :=
                                                      M.use
                                                        (M.alloc (|
                                                          BinOp.lt (|
                                                            M.read (| initialized |),
                                                            M.read (|
                                                              M.get_constant (|
                                                                "core::iter::adapters::filter::next_chunk_dropless::N"
                                                              |)
                                                            |)
                                                          |)
                                                        |)) in
                                                    let _ :=
                                                      M.is_constant_or_break_match (|
                                                        M.read (| γ |),
                                                        Value.Bool true
                                                      |) in
                                                    M.alloc (|
                                                      Value.StructTuple
                                                        "core::ops::control_flow::ControlFlow::Continue"
                                                        [ Value.Tuple [] ]
                                                    |)));
                                                fun γ =>
                                                  ltac:(M.monadic
                                                    (M.alloc (|
                                                      Value.StructTuple
                                                        "core::ops::control_flow::ControlFlow::Break"
                                                        [ Value.Tuple [] ]
                                                    |)))
                                              ]
                                            |)
                                          |)))
                                    ]
                                  |)))
                              | _ => M.impossible "wrong number of arguments"
                              end))
                      ]
                    |)
                  |) in
                M.match_operator (|
                  result,
                  [
                    fun γ =>
                      ltac:(M.monadic
                        (let γ0_0 :=
                          M.SubPointer.get_struct_tuple_field (|
                            γ,
                            "core::ops::control_flow::ControlFlow::Break",
                            0
                          |) in
                        M.alloc (|
                          Value.StructTuple
                            "core::result::Result::Ok"
                            [
                              M.call_closure (|
                                M.get_associated_function (|
                                  Ty.apply
                                    (Ty.path "core::mem::maybe_uninit::MaybeUninit")
                                    []
                                    [ Ty.associated ],
                                  "array_assume_init",
                                  []
                                |),
                                [ M.read (| array |) ]
                              |)
                            ]
                        |)));
                    fun γ =>
                      ltac:(M.monadic
                        (let γ0_0 :=
                          M.SubPointer.get_struct_tuple_field (|
                            γ,
                            "core::ops::control_flow::ControlFlow::Continue",
                            0
                          |) in
                        M.alloc (|
                          Value.StructTuple
                            "core::result::Result::Err"
                            [
                              M.call_closure (|
                                M.get_associated_function (|
                                  Ty.apply
                                    (Ty.path "core::array::iter::IntoIter")
                                    [ N ]
                                    [ Ty.associated ],
                                  "new_unchecked",
                                  []
                                |),
                                [
                                  M.read (| array |);
                                  Value.StructRecord
                                    "core::ops::range::Range"
                                    [
                                      ("start", Value.Integer IntegerKind.Usize 0);
                                      ("end_", M.read (| initialized |))
                                    ]
                                ]
                              |)
                            ]
                        |)))
                  ]
                |)
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        Axiom AssociatedFunction_next_chunk_dropless :
          forall (I P : Ty.t),
          M.IsAssociatedFunction (Self I P) "next_chunk_dropless" (next_chunk_dropless I P).
      End Impl_core_iter_adapters_filter_Filter_I_P.
      
      
      Module Impl_core_fmt_Debug_where_core_fmt_Debug_I_for_core_iter_adapters_filter_Filter_I_P.
        Definition Self (I P : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        (*
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_struct("Filter").field("iter", &self.iter).finish()
            }
        *)
        Definition fmt (I P : Ty.t) (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [], [ self; f ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              let f := M.alloc (| f |) in
              M.call_closure (|
                M.get_associated_function (|
                  Ty.path "core::fmt::builders::DebugStruct",
                  "finish",
                  []
                |),
                [
                  M.call_closure (|
                    M.get_associated_function (|
                      Ty.path "core::fmt::builders::DebugStruct",
                      "field",
                      []
                    |),
                    [
                      M.alloc (|
                        M.call_closure (|
                          M.get_associated_function (|
                            Ty.path "core::fmt::Formatter",
                            "debug_struct",
                            []
                          |),
                          [ M.read (| f |); M.read (| Value.String "Filter" |) ]
                        |)
                      |);
                      M.read (| Value.String "iter" |);
                      M.SubPointer.get_struct_record_field (|
                        M.read (| self |),
                        "core::iter::adapters::filter::Filter",
                        "iter"
                      |)
                    ]
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        Axiom Implements :
          forall (I P : Ty.t),
          M.IsTraitInstance
            "core::fmt::Debug"
            (Self I P)
            (* Trait polymorphic types *) []
            (* Instance *) [ ("fmt", InstanceField.Method (fmt I P)) ].
      End Impl_core_fmt_Debug_where_core_fmt_Debug_I_for_core_iter_adapters_filter_Filter_I_P.
      
      (*
      fn filter_fold<T, Acc>(
          mut predicate: impl FnMut(&T) -> bool,
          mut fold: impl FnMut(Acc, T) -> Acc,
      ) -> impl FnMut(Acc, T) -> Acc {
          move |acc, item| if predicate(&item) { fold(acc, item) } else { acc }
      }
      *)
      Definition filter_fold (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
        match ε, τ, α with
        | [],
            [ T; Acc; impl_FnMut__T__arrow_bool; impl_FnMut_Acc__T__arrow_Acc ],
            [ predicate; fold ] =>
          ltac:(M.monadic
            (let predicate := M.alloc (| predicate |) in
            let fold := M.alloc (| fold |) in
            M.closure
              (fun γ =>
                ltac:(M.monadic
                  match γ with
                  | [ α0; α1 ] =>
                    ltac:(M.monadic
                      (M.match_operator (|
                        M.alloc (| α0 |),
                        [
                          fun γ =>
                            ltac:(M.monadic
                              (let acc := M.copy (| γ |) in
                              M.match_operator (|
                                M.alloc (| α1 |),
                                [
                                  fun γ =>
                                    ltac:(M.monadic
                                      (let item := M.copy (| γ |) in
                                      M.read (|
                                        M.match_operator (|
                                          M.alloc (| Value.Tuple [] |),
                                          [
                                            fun γ =>
                                              ltac:(M.monadic
                                                (let γ :=
                                                  M.use
                                                    (M.alloc (|
                                                      M.call_closure (|
                                                        M.get_trait_method (|
                                                          "core::ops::function::FnMut",
                                                          impl_FnMut__T__arrow_bool,
                                                          [
                                                            Ty.tuple
                                                              [ Ty.apply (Ty.path "&") [] [ T ] ]
                                                          ],
                                                          "call_mut",
                                                          []
                                                        |),
                                                        [ predicate; Value.Tuple [ item ] ]
                                                      |)
                                                    |)) in
                                                let _ :=
                                                  M.is_constant_or_break_match (|
                                                    M.read (| γ |),
                                                    Value.Bool true
                                                  |) in
                                                M.alloc (|
                                                  M.call_closure (|
                                                    M.get_trait_method (|
                                                      "core::ops::function::FnMut",
                                                      impl_FnMut_Acc__T__arrow_Acc,
                                                      [ Ty.tuple [ Acc; T ] ],
                                                      "call_mut",
                                                      []
                                                    |),
                                                    [
                                                      fold;
                                                      Value.Tuple
                                                        [ M.read (| acc |); M.read (| item |) ]
                                                    ]
                                                  |)
                                                |)));
                                            fun γ => ltac:(M.monadic acc)
                                          ]
                                        |)
                                      |)))
                                ]
                              |)))
                        ]
                      |)))
                  | _ => M.impossible "wrong number of arguments"
                  end))))
        | _, _, _ => M.impossible "wrong number of arguments"
        end.
      
      Axiom Function_filter_fold :
        M.IsFunction "core::iter::adapters::filter::filter_fold" filter_fold.
      
      Module filter_fold.
        (* Error OpaqueTy *)
      End filter_fold.
      
      (*
      fn filter_try_fold<'a, T, Acc, R: Try<Output = Acc>>(
          predicate: &'a mut impl FnMut(&T) -> bool,
          mut fold: impl FnMut(Acc, T) -> R + 'a,
      ) -> impl FnMut(Acc, T) -> R + 'a {
          move |acc, item| if predicate(&item) { fold(acc, item) } else { try { acc } }
      }
      *)
      Definition filter_try_fold (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
        match ε, τ, α with
        | [],
            [ T; Acc; R; impl_FnMut__T__arrow_bool; impl_FnMut_Acc__T__arrow_R__plus__'a ],
            [ predicate; fold ] =>
          ltac:(M.monadic
            (let predicate := M.alloc (| predicate |) in
            let fold := M.alloc (| fold |) in
            M.closure
              (fun γ =>
                ltac:(M.monadic
                  match γ with
                  | [ α0; α1 ] =>
                    ltac:(M.monadic
                      (M.match_operator (|
                        M.alloc (| α0 |),
                        [
                          fun γ =>
                            ltac:(M.monadic
                              (let acc := M.copy (| γ |) in
                              M.match_operator (|
                                M.alloc (| α1 |),
                                [
                                  fun γ =>
                                    ltac:(M.monadic
                                      (let item := M.copy (| γ |) in
                                      M.read (|
                                        M.match_operator (|
                                          M.alloc (| Value.Tuple [] |),
                                          [
                                            fun γ =>
                                              ltac:(M.monadic
                                                (let γ :=
                                                  M.use
                                                    (M.alloc (|
                                                      M.call_closure (|
                                                        M.get_trait_method (|
                                                          "core::ops::function::FnMut",
                                                          impl_FnMut__T__arrow_bool,
                                                          [
                                                            Ty.tuple
                                                              [ Ty.apply (Ty.path "&") [] [ T ] ]
                                                          ],
                                                          "call_mut",
                                                          []
                                                        |),
                                                        [
                                                          M.read (| predicate |);
                                                          Value.Tuple [ item ]
                                                        ]
                                                      |)
                                                    |)) in
                                                let _ :=
                                                  M.is_constant_or_break_match (|
                                                    M.read (| γ |),
                                                    Value.Bool true
                                                  |) in
                                                M.alloc (|
                                                  M.call_closure (|
                                                    M.get_trait_method (|
                                                      "core::ops::function::FnMut",
                                                      impl_FnMut_Acc__T__arrow_R__plus__'a,
                                                      [ Ty.tuple [ Acc; T ] ],
                                                      "call_mut",
                                                      []
                                                    |),
                                                    [
                                                      fold;
                                                      Value.Tuple
                                                        [ M.read (| acc |); M.read (| item |) ]
                                                    ]
                                                  |)
                                                |)));
                                            fun γ =>
                                              ltac:(M.monadic
                                                (M.alloc (|
                                                  M.call_closure (|
                                                    M.get_trait_method (|
                                                      "core::ops::try_trait::Try",
                                                      R,
                                                      [],
                                                      "from_output",
                                                      []
                                                    |),
                                                    [ M.read (| acc |) ]
                                                  |)
                                                |)))
                                          ]
                                        |)
                                      |)))
                                ]
                              |)))
                        ]
                      |)))
                  | _ => M.impossible "wrong number of arguments"
                  end))))
        | _, _, _ => M.impossible "wrong number of arguments"
        end.
      
      Axiom Function_filter_try_fold :
        M.IsFunction "core::iter::adapters::filter::filter_try_fold" filter_try_fold.
      
      Module filter_try_fold.
        (* Error OpaqueTy *)
      End filter_try_fold.
      
      Module Impl_core_iter_traits_iterator_Iterator_where_core_iter_traits_iterator_Iterator_I_where_core_ops_function_FnMut_P_Tuple_ref__associated_type__for_core_iter_adapters_filter_Filter_I_P.
        Definition Self (I P : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        (*     type Item = I::Item; *)
        Definition _Item (I P : Ty.t) : Ty.t := Ty.associated.
        
        (*
            fn next(&mut self) -> Option<I::Item> {
                self.iter.find(&mut self.predicate)
            }
        *)
        Definition next (I P : Ty.t) (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              M.call_closure (|
                M.get_trait_method (|
                  "core::iter::traits::iterator::Iterator",
                  I,
                  [],
                  "find",
                  [ Ty.apply (Ty.path "&mut") [] [ P ] ]
                |),
                [
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "core::iter::adapters::filter::Filter",
                    "iter"
                  |);
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "core::iter::adapters::filter::Filter",
                    "predicate"
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        (*
            fn next_chunk<const N: usize>(
                &mut self,
            ) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>> {
                // avoid codegen for the dead branch
                let fun = const {
                    if crate::mem::needs_drop::<I::Item>() {
                        array::iter_next_chunk::<I::Item, N>
                    } else {
                        Self::next_chunk_dropless::<N>
                    }
                };
        
                fun(self)
            }
        *)
        Definition next_chunk
            (I P : Ty.t)
            (ε : list Value.t)
            (τ : list Ty.t)
            (α : list Value.t)
            : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [ N ], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              M.read (|
                let~ fun_ :=
                  M.copy (|
                    M.get_constant (| "core::iter::adapters::filter::next_chunk_discriminant" |)
                  |) in
                M.alloc (| M.call_closure (| M.read (| fun_ |), [ M.read (| self |) ] |) |)
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        (*
            fn size_hint(&self) -> (usize, Option<usize>) {
                let (_, upper) = self.iter.size_hint();
                (0, upper) // can't know a lower bound, due to the predicate
            }
        *)
        Definition size_hint
            (I P : Ty.t)
            (ε : list Value.t)
            (τ : list Ty.t)
            (α : list Value.t)
            : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              M.read (|
                M.match_operator (|
                  M.alloc (|
                    M.call_closure (|
                      M.get_trait_method (|
                        "core::iter::traits::iterator::Iterator",
                        I,
                        [],
                        "size_hint",
                        []
                      |),
                      [
                        M.SubPointer.get_struct_record_field (|
                          M.read (| self |),
                          "core::iter::adapters::filter::Filter",
                          "iter"
                        |)
                      ]
                    |)
                  |),
                  [
                    fun γ =>
                      ltac:(M.monadic
                        (let γ0_0 := M.SubPointer.get_tuple_field (| γ, 0 |) in
                        let γ0_1 := M.SubPointer.get_tuple_field (| γ, 1 |) in
                        let upper := M.copy (| γ0_1 |) in
                        M.alloc (|
                          Value.Tuple [ Value.Integer IntegerKind.Usize 0; M.read (| upper |) ]
                        |)))
                  ]
                |)
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        (*
            fn count(self) -> usize {
                #[inline]
                fn to_usize<T>(mut predicate: impl FnMut(&T) -> bool) -> impl FnMut(T) -> usize {
                    move |x| predicate(&x) as usize
                }
        
                self.iter.map(to_usize(self.predicate)).sum()
            }
        *)
        Definition count (I P : Ty.t) (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              M.call_closure (|
                M.get_trait_method (|
                  "core::iter::traits::iterator::Iterator",
                  Ty.apply (Ty.path "core::iter::adapters::map::Map") [] [ I; Ty.associated ],
                  [],
                  "sum",
                  [ Ty.path "usize" ]
                |),
                [
                  M.call_closure (|
                    M.get_trait_method (|
                      "core::iter::traits::iterator::Iterator",
                      I,
                      [],
                      "map",
                      [ Ty.path "usize"; Ty.associated ]
                    |),
                    [
                      M.read (|
                        M.SubPointer.get_struct_record_field (|
                          self,
                          "core::iter::adapters::filter::Filter",
                          "iter"
                        |)
                      |);
                      M.call_closure (|
                        M.get_associated_function (| Self, "to_usize.count", [] |),
                        [
                          M.read (|
                            M.SubPointer.get_struct_record_field (|
                              self,
                              "core::iter::adapters::filter::Filter",
                              "predicate"
                            |)
                          |)
                        ]
                      |)
                    ]
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        (*
            fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
            where
                Self: Sized,
                Fold: FnMut(Acc, Self::Item) -> R,
                R: Try<Output = Acc>,
            {
                self.iter.try_fold(init, filter_try_fold(&mut self.predicate, fold))
            }
        *)
        Definition try_fold
            (I P : Ty.t)
            (ε : list Value.t)
            (τ : list Ty.t)
            (α : list Value.t)
            : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [ Acc; Fold; R ], [ self; init; fold ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              let init := M.alloc (| init |) in
              let fold := M.alloc (| fold |) in
              M.call_closure (|
                M.get_trait_method (|
                  "core::iter::traits::iterator::Iterator",
                  I,
                  [],
                  "try_fold",
                  [ Acc; Ty.associated; R ]
                |),
                [
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "core::iter::adapters::filter::Filter",
                    "iter"
                  |);
                  M.read (| init |);
                  M.call_closure (|
                    M.get_function (|
                      "core::iter::adapters::filter::filter_try_fold",
                      [ Ty.associated; Acc; R; P; Fold ]
                    |),
                    [
                      M.SubPointer.get_struct_record_field (|
                        M.read (| self |),
                        "core::iter::adapters::filter::Filter",
                        "predicate"
                      |);
                      M.read (| fold |)
                    ]
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        (*
            fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
            where
                Fold: FnMut(Acc, Self::Item) -> Acc,
            {
                self.iter.fold(init, filter_fold(self.predicate, fold))
            }
        *)
        Definition fold (I P : Ty.t) (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [ Acc; Fold ], [ self; init; fold ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              let init := M.alloc (| init |) in
              let fold := M.alloc (| fold |) in
              M.call_closure (|
                M.get_trait_method (|
                  "core::iter::traits::iterator::Iterator",
                  I,
                  [],
                  "fold",
                  [ Acc; Ty.associated ]
                |),
                [
                  M.read (|
                    M.SubPointer.get_struct_record_field (|
                      self,
                      "core::iter::adapters::filter::Filter",
                      "iter"
                    |)
                  |);
                  M.read (| init |);
                  M.call_closure (|
                    M.get_function (|
                      "core::iter::adapters::filter::filter_fold",
                      [ Ty.associated; Acc; P; Fold ]
                    |),
                    [
                      M.read (|
                        M.SubPointer.get_struct_record_field (|
                          self,
                          "core::iter::adapters::filter::Filter",
                          "predicate"
                        |)
                      |);
                      M.read (| fold |)
                    ]
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        Axiom Implements :
          forall (I P : Ty.t),
          M.IsTraitInstance
            "core::iter::traits::iterator::Iterator"
            (Self I P)
            (* Trait polymorphic types *) []
            (* Instance *)
            [
              ("Item", InstanceField.Ty (_Item I P));
              ("next", InstanceField.Method (next I P));
              ("next_chunk", InstanceField.Method (next_chunk I P));
              ("size_hint", InstanceField.Method (size_hint I P));
              ("count", InstanceField.Method (count I P));
              ("try_fold", InstanceField.Method (try_fold I P));
              ("fold", InstanceField.Method (fold I P))
            ].
      End Impl_core_iter_traits_iterator_Iterator_where_core_iter_traits_iterator_Iterator_I_where_core_ops_function_FnMut_P_Tuple_ref__associated_type__for_core_iter_adapters_filter_Filter_I_P.
      
      Module Impl_core_iter_traits_double_ended_DoubleEndedIterator_where_core_iter_traits_double_ended_DoubleEndedIterator_I_where_core_ops_function_FnMut_P_Tuple_ref__associated_type__for_core_iter_adapters_filter_Filter_I_P.
        Definition Self (I P : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        (*
            fn next_back(&mut self) -> Option<I::Item> {
                self.iter.rfind(&mut self.predicate)
            }
        *)
        Definition next_back
            (I P : Ty.t)
            (ε : list Value.t)
            (τ : list Ty.t)
            (α : list Value.t)
            : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              M.call_closure (|
                M.get_trait_method (|
                  "core::iter::traits::double_ended::DoubleEndedIterator",
                  I,
                  [],
                  "rfind",
                  [ Ty.apply (Ty.path "&mut") [] [ P ] ]
                |),
                [
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "core::iter::adapters::filter::Filter",
                    "iter"
                  |);
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "core::iter::adapters::filter::Filter",
                    "predicate"
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        (*
            fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
            where
                Self: Sized,
                Fold: FnMut(Acc, Self::Item) -> R,
                R: Try<Output = Acc>,
            {
                self.iter.try_rfold(init, filter_try_fold(&mut self.predicate, fold))
            }
        *)
        Definition try_rfold
            (I P : Ty.t)
            (ε : list Value.t)
            (τ : list Ty.t)
            (α : list Value.t)
            : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [ Acc; Fold; R ], [ self; init; fold ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              let init := M.alloc (| init |) in
              let fold := M.alloc (| fold |) in
              M.call_closure (|
                M.get_trait_method (|
                  "core::iter::traits::double_ended::DoubleEndedIterator",
                  I,
                  [],
                  "try_rfold",
                  [ Acc; Ty.associated; R ]
                |),
                [
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "core::iter::adapters::filter::Filter",
                    "iter"
                  |);
                  M.read (| init |);
                  M.call_closure (|
                    M.get_function (|
                      "core::iter::adapters::filter::filter_try_fold",
                      [ Ty.associated; Acc; R; P; Fold ]
                    |),
                    [
                      M.SubPointer.get_struct_record_field (|
                        M.read (| self |),
                        "core::iter::adapters::filter::Filter",
                        "predicate"
                      |);
                      M.read (| fold |)
                    ]
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        (*
            fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
            where
                Fold: FnMut(Acc, Self::Item) -> Acc,
            {
                self.iter.rfold(init, filter_fold(self.predicate, fold))
            }
        *)
        Definition rfold (I P : Ty.t) (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
          let Self : Ty.t := Self I P in
          match ε, τ, α with
          | [], [ Acc; Fold ], [ self; init; fold ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              let init := M.alloc (| init |) in
              let fold := M.alloc (| fold |) in
              M.call_closure (|
                M.get_trait_method (|
                  "core::iter::traits::double_ended::DoubleEndedIterator",
                  I,
                  [],
                  "rfold",
                  [ Acc; Ty.associated ]
                |),
                [
                  M.read (|
                    M.SubPointer.get_struct_record_field (|
                      self,
                      "core::iter::adapters::filter::Filter",
                      "iter"
                    |)
                  |);
                  M.read (| init |);
                  M.call_closure (|
                    M.get_function (|
                      "core::iter::adapters::filter::filter_fold",
                      [ Ty.associated; Acc; P; Fold ]
                    |),
                    [
                      M.read (|
                        M.SubPointer.get_struct_record_field (|
                          self,
                          "core::iter::adapters::filter::Filter",
                          "predicate"
                        |)
                      |);
                      M.read (| fold |)
                    ]
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        Axiom Implements :
          forall (I P : Ty.t),
          M.IsTraitInstance
            "core::iter::traits::double_ended::DoubleEndedIterator"
            (Self I P)
            (* Trait polymorphic types *) []
            (* Instance *)
            [
              ("next_back", InstanceField.Method (next_back I P));
              ("try_rfold", InstanceField.Method (try_rfold I P));
              ("rfold", InstanceField.Method (rfold I P))
            ].
      End Impl_core_iter_traits_double_ended_DoubleEndedIterator_where_core_iter_traits_double_ended_DoubleEndedIterator_I_where_core_ops_function_FnMut_P_Tuple_ref__associated_type__for_core_iter_adapters_filter_Filter_I_P.
      
      Module Impl_core_iter_traits_marker_FusedIterator_where_core_iter_traits_marker_FusedIterator_I_where_core_ops_function_FnMut_P_Tuple_ref__associated_type__for_core_iter_adapters_filter_Filter_I_P.
        Definition Self (I P : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        Axiom Implements :
          forall (I P : Ty.t),
          M.IsTraitInstance
            "core::iter::traits::marker::FusedIterator"
            (Self I P)
            (* Trait polymorphic types *) []
            (* Instance *) [].
      End Impl_core_iter_traits_marker_FusedIterator_where_core_iter_traits_marker_FusedIterator_I_where_core_ops_function_FnMut_P_Tuple_ref__associated_type__for_core_iter_adapters_filter_Filter_I_P.
      
      Module Impl_core_iter_traits_marker_TrustedFused_where_core_iter_traits_marker_TrustedFused_I_for_core_iter_adapters_filter_Filter_I_F.
        Definition Self (I F : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; F ].
        
        Axiom Implements :
          forall (I F : Ty.t),
          M.IsTraitInstance
            "core::iter::traits::marker::TrustedFused"
            (Self I F)
            (* Trait polymorphic types *) []
            (* Instance *) [].
      End Impl_core_iter_traits_marker_TrustedFused_where_core_iter_traits_marker_TrustedFused_I_for_core_iter_adapters_filter_Filter_I_F.
      
      Module Impl_core_iter_adapters_SourceIter_where_core_iter_adapters_SourceIter_I_for_core_iter_adapters_filter_Filter_I_P.
        Definition Self (P I : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        (*     type Source = I::Source; *)
        Definition _Source (P I : Ty.t) : Ty.t := Ty.associated.
        
        (*
            unsafe fn as_inner(&mut self) -> &mut I::Source {
                // SAFETY: unsafe function forwarding to unsafe function with the same requirements
                unsafe { SourceIter::as_inner(&mut self.iter) }
            }
        *)
        Definition as_inner
            (P I : Ty.t)
            (ε : list Value.t)
            (τ : list Ty.t)
            (α : list Value.t)
            : M :=
          let Self : Ty.t := Self P I in
          match ε, τ, α with
          | [], [], [ self ] =>
            ltac:(M.monadic
              (let self := M.alloc (| self |) in
              M.call_closure (|
                M.get_trait_method (| "core::iter::adapters::SourceIter", I, [], "as_inner", [] |),
                [
                  M.SubPointer.get_struct_record_field (|
                    M.read (| self |),
                    "core::iter::adapters::filter::Filter",
                    "iter"
                  |)
                ]
              |)))
          | _, _, _ => M.impossible "wrong number of arguments"
          end.
        
        Axiom Implements :
          forall (P I : Ty.t),
          M.IsTraitInstance
            "core::iter::adapters::SourceIter"
            (Self P I)
            (* Trait polymorphic types *) []
            (* Instance *)
            [
              ("Source", InstanceField.Ty (_Source P I));
              ("as_inner", InstanceField.Method (as_inner P I))
            ].
      End Impl_core_iter_adapters_SourceIter_where_core_iter_adapters_SourceIter_I_for_core_iter_adapters_filter_Filter_I_P.
      
      Module Impl_core_iter_traits_marker_InPlaceIterable_where_core_iter_traits_marker_InPlaceIterable_I_for_core_iter_adapters_filter_Filter_I_P.
        Definition Self (I P : Ty.t) : Ty.t :=
          Ty.apply (Ty.path "core::iter::adapters::filter::Filter") [] [ I; P ].
        
        (*     const EXPAND_BY: Option<NonZero<usize>> = I::EXPAND_BY; *)
        (* Ty.apply
          (Ty.path "core::option::Option")
          []
          [ Ty.apply (Ty.path "core::num::nonzero::NonZero") [] [ Ty.path "usize" ] ] *)
        Definition value_EXPAND_BY (I P : Ty.t) : Value.t :=
          let Self : Ty.t := Self I P in
          M.run
            ltac:(M.monadic
              (M.get_constant (| "core::iter::traits::marker::InPlaceIterable::EXPAND_BY" |))).
        
        (*     const MERGE_BY: Option<NonZero<usize>> = I::MERGE_BY; *)
        (* Ty.apply
          (Ty.path "core::option::Option")
          []
          [ Ty.apply (Ty.path "core::num::nonzero::NonZero") [] [ Ty.path "usize" ] ] *)
        Definition value_MERGE_BY (I P : Ty.t) : Value.t :=
          let Self : Ty.t := Self I P in
          M.run
            ltac:(M.monadic
              (M.get_constant (| "core::iter::traits::marker::InPlaceIterable::MERGE_BY" |))).
        
        Axiom Implements :
          forall (I P : Ty.t),
          M.IsTraitInstance
            "core::iter::traits::marker::InPlaceIterable"
            (Self I P)
            (* Trait polymorphic types *) []
            (* Instance *)
            [
              ("value_EXPAND_BY", InstanceField.Constant (value_EXPAND_BY I P));
              ("value_MERGE_BY", InstanceField.Constant (value_MERGE_BY I P))
            ].
      End Impl_core_iter_traits_marker_InPlaceIterable_where_core_iter_traits_marker_InPlaceIterable_I_for_core_iter_adapters_filter_Filter_I_P.
    End filter.
  End adapters.
End iter.
