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

(* StructTuple
  {
    name := "Years";
    const_params := [];
    ty_params := [];
    fields := [ Ty.path "i64" ];
  } *)

(* StructTuple
  {
    name := "Days";
    const_params := [];
    ty_params := [];
    fields := [ Ty.path "i64" ];
  } *)

Module Impl_generics_new_type_idiom_Years.
  Definition Self : Ty.t := Ty.path "generics_new_type_idiom::Years".
  
  (*
      pub fn to_days(&self) -> Days {
          Days(self.0 * 365)
      }
  *)
  Definition to_days (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ self ] =>
      ltac:(M.monadic
        (let self := M.alloc (| self |) in
        Value.StructTuple
          "generics_new_type_idiom::Days"
          [
            BinOp.Wrap.mul (|
              M.read (|
                M.SubPointer.get_struct_tuple_field (|
                  M.read (| self |),
                  "generics_new_type_idiom::Years",
                  0
                |)
              |),
              Value.Integer IntegerKind.I64 365
            |)
          ]))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom AssociatedFunction_to_days : M.IsAssociatedFunction Self "to_days" to_days.
End Impl_generics_new_type_idiom_Years.

Module Impl_generics_new_type_idiom_Days.
  Definition Self : Ty.t := Ty.path "generics_new_type_idiom::Days".
  
  (*
      pub fn to_years(&self) -> Years {
          Years(self.0 / 365)
      }
  *)
  Definition to_years (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ self ] =>
      ltac:(M.monadic
        (let self := M.alloc (| self |) in
        Value.StructTuple
          "generics_new_type_idiom::Years"
          [
            BinOp.Wrap.div (|
              M.read (|
                M.SubPointer.get_struct_tuple_field (|
                  M.read (| self |),
                  "generics_new_type_idiom::Days",
                  0
                |)
              |),
              Value.Integer IntegerKind.I64 365
            |)
          ]))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom AssociatedFunction_to_years : M.IsAssociatedFunction Self "to_years" to_years.
End Impl_generics_new_type_idiom_Days.

(*
fn old_enough(age: &Years) -> bool {
    age.0 >= 18
}
*)
Definition old_enough (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [], [ age ] =>
    ltac:(M.monadic
      (let age := M.alloc (| age |) in
      BinOp.ge (|
        M.read (|
          M.SubPointer.get_struct_tuple_field (|
            M.read (| age |),
            "generics_new_type_idiom::Years",
            0
          |)
        |),
        Value.Integer IntegerKind.I64 18
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_old_enough : M.IsFunction "generics_new_type_idiom::old_enough" old_enough.

(*
fn main() {
    let age = Years(5);
    let age_days = age.to_days();
    println!("Old enough {}", old_enough(&age));
    println!("Old enough {}", old_enough(&age_days.to_years()));
    // println!("Old enough {}", old_enough(&age_days));
}
*)
Definition main (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [], [] =>
    ltac:(M.monadic
      (M.read (|
        let~ age :=
          M.alloc (|
            Value.StructTuple "generics_new_type_idiom::Years" [ Value.Integer IntegerKind.I64 5 ]
          |) in
        let~ age_days :=
          M.alloc (|
            M.call_closure (|
              M.get_associated_function (|
                Ty.path "generics_new_type_idiom::Years",
                "to_days",
                []
              |),
              [ age ]
            |)
          |) in
        let~ _ :=
          let~ _ :=
            M.alloc (|
              M.call_closure (|
                M.get_function (| "std::io::stdio::_print", [] |),
                [
                  M.call_closure (|
                    M.get_associated_function (| Ty.path "core::fmt::Arguments", "new_v1", [] |),
                    [
                      M.alloc (|
                        Value.Array
                          [ M.read (| Value.String "Old enough " |); M.read (| Value.String "
" |) ]
                      |);
                      M.alloc (|
                        Value.Array
                          [
                            M.call_closure (|
                              M.get_associated_function (|
                                Ty.path "core::fmt::rt::Argument",
                                "new_display",
                                [ Ty.path "bool" ]
                              |),
                              [
                                M.alloc (|
                                  M.call_closure (|
                                    M.get_function (| "generics_new_type_idiom::old_enough", [] |),
                                    [ age ]
                                  |)
                                |)
                              ]
                            |)
                          ]
                      |)
                    ]
                  |)
                ]
              |)
            |) in
          M.alloc (| Value.Tuple [] |) in
        let~ _ :=
          let~ _ :=
            M.alloc (|
              M.call_closure (|
                M.get_function (| "std::io::stdio::_print", [] |),
                [
                  M.call_closure (|
                    M.get_associated_function (| Ty.path "core::fmt::Arguments", "new_v1", [] |),
                    [
                      M.alloc (|
                        Value.Array
                          [ M.read (| Value.String "Old enough " |); M.read (| Value.String "
" |) ]
                      |);
                      M.alloc (|
                        Value.Array
                          [
                            M.call_closure (|
                              M.get_associated_function (|
                                Ty.path "core::fmt::rt::Argument",
                                "new_display",
                                [ Ty.path "bool" ]
                              |),
                              [
                                M.alloc (|
                                  M.call_closure (|
                                    M.get_function (| "generics_new_type_idiom::old_enough", [] |),
                                    [
                                      M.alloc (|
                                        M.call_closure (|
                                          M.get_associated_function (|
                                            Ty.path "generics_new_type_idiom::Days",
                                            "to_years",
                                            []
                                          |),
                                          [ age_days ]
                                        |)
                                      |)
                                    ]
                                  |)
                                |)
                              ]
                            |)
                          ]
                      |)
                    ]
                  |)
                ]
              |)
            |) in
          M.alloc (| Value.Tuple [] |) in
        M.alloc (| Value.Tuple [] |)
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

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