================================================================================
arrays.ncl
================================================================================

# Example array functions. This code is illustrative: prefer using the array 
# stdlib functions `array.map` and `array.fold` instead.
let my_array_lib = {
  map : forall a b. (a -> b) -> Array a -> Array b = fun f arr =>
    if arr == [] then
      []
    else
      let head = array.head arr in
      let tail = array.tail arr in
      [f head] @ map f tail,

  fold : forall a b. (a -> b -> b) -> Array a -> b -> b =
      fun f arr first =>
        if arr == [] then
          first
        else
          let head = array.head arr in
          let tail = array.tail arr in
          f head (fold f tail first),
} in
# Compute `7!`
let l = my_array_lib.map (fun x => x+1) [1, 2, 3, 4, 5, 6] in
my_array_lib.fold (fun x acc => x * acc) l 1

--------------------------------------------------------------------------------

(term
  (comment)
  (comment)
  (uni_term
    (let_expr
      (pattern
        (ident))
      (term
        (uni_term
          (infix_expr
            (applicative
              (record_operand
                (atom
                  (uni_record
                    (record_field
                      (field_path
                        (field_path_elem
                          (ident)))
                      (annot
                        (annot_atom
                          (types
                            (forall
                              (ident)
                              (ident)
                              (types
                                (infix_expr
                                  (infix_expr
                                    (applicative
                                      (record_operand
                                        (atom
                                          (uni_term
                                            (infix_expr
                                              (infix_expr
                                                (applicative
                                                  (record_operand
                                                    (atom
                                                      (ident)))))
                                              (infix_expr
                                                (applicative
                                                  (record_operand
                                                    (atom
                                                      (ident)))))))))))
                                  (infix_expr
                                    (infix_expr
                                      (applicative
                                        (type_array
                                          (record_operand
                                            (atom
                                              (ident))))))
                                    (infix_expr
                                      (applicative
                                        (type_array
                                          (record_operand
                                            (atom
                                              (ident)))))))))))))
                      (term
                        (uni_term
                          (fun_expr
                            (pattern
                              (ident))
                            (pattern
                              (ident))
                            (term
                              (uni_term
                                (ite_expr
                                  (term
                                    (uni_term
                                      (infix_expr
                                        (infix_expr
                                          (applicative
                                            (record_operand
                                              (atom
                                                (ident)))))
                                        (infix_b_op_8)
                                        (infix_expr
                                          (applicative
                                            (record_operand
                                              (atom)))))))
                                  (term
                                    (uni_term
                                      (infix_expr
                                        (applicative
                                          (record_operand
                                            (atom))))))
                                  (term
                                    (uni_term
                                      (let_expr
                                        (pattern
                                          (ident))
                                        (term
                                          (uni_term
                                            (infix_expr
                                              (applicative
                                                (applicative
                                                  (record_operand
                                                    (record_operation_chain
                                                      (record_operand
                                                        (atom
                                                          (ident)))
                                                      (ident))))
                                                (record_operand
                                                  (atom
                                                    (ident)))))))
                                        (term
                                          (uni_term
                                            (let_expr
                                              (pattern
                                                (ident))
                                              (term
                                                (uni_term
                                                  (infix_expr
                                                    (applicative
                                                      (applicative
                                                        (record_operand
                                                          (record_operation_chain
                                                            (record_operand
                                                              (atom
                                                                (ident)))
                                                            (ident))))
                                                      (record_operand
                                                        (atom
                                                          (ident)))))))
                                              (term
                                                (uni_term
                                                  (infix_expr
                                                    (infix_expr
                                                      (applicative
                                                        (record_operand
                                                          (atom
                                                            (term
                                                              (uni_term
                                                                (infix_expr
                                                                  (applicative
                                                                    (applicative
                                                                      (record_operand
                                                                        (atom
                                                                          (ident))))
                                                                    (record_operand
                                                                      (atom
                                                                        (ident)))))))))))
                                                    (infix_b_op_2)
                                                    (infix_expr
                                                      (applicative
                                                        (applicative
                                                          (applicative
                                                            (record_operand
                                                              (atom
                                                                (ident))))
                                                          (record_operand
                                                            (atom
                                                              (ident))))
                                                        (record_operand
                                                          (atom
                                                            (ident)))))))))))))))))))))
                    (record_field
                      (field_path
                        (field_path_elem
                          (ident)))
                      (annot
                        (annot_atom
                          (types
                            (forall
                              (ident)
                              (ident)
                              (types
                                (infix_expr
                                  (infix_expr
                                    (applicative
                                      (record_operand
                                        (atom
                                          (uni_term
                                            (infix_expr
                                              (infix_expr
                                                (applicative
                                                  (record_operand
                                                    (atom
                                                      (ident)))))
                                              (infix_expr
                                                (infix_expr
                                                  (applicative
                                                    (record_operand
                                                      (atom
                                                        (ident)))))
                                                (infix_expr
                                                  (applicative
                                                    (record_operand
                                                      (atom
                                                        (ident))))))))))))
                                  (infix_expr
                                    (infix_expr
                                      (applicative
                                        (type_array
                                          (record_operand
                                            (atom
                                              (ident))))))
                                    (infix_expr
                                      (infix_expr
                                        (applicative
                                          (record_operand
                                            (atom
                                              (ident)))))
                                      (infix_expr
                                        (applicative
                                          (record_operand
                                            (atom
                                              (ident)))))))))))))
                      (term
                        (uni_term
                          (fun_expr
                            (pattern
                              (ident))
                            (pattern
                              (ident))
                            (pattern
                              (ident))
                            (term
                              (uni_term
                                (ite_expr
                                  (term
                                    (uni_term
                                      (infix_expr
                                        (infix_expr
                                          (applicative
                                            (record_operand
                                              (atom
                                                (ident)))))
                                        (infix_b_op_8)
                                        (infix_expr
                                          (applicative
                                            (record_operand
                                              (atom)))))))
                                  (term
                                    (uni_term
                                      (infix_expr
                                        (applicative
                                          (record_operand
                                            (atom
                                              (ident)))))))
                                  (term
                                    (uni_term
                                      (let_expr
                                        (pattern
                                          (ident))
                                        (term
                                          (uni_term
                                            (infix_expr
                                              (applicative
                                                (applicative
                                                  (record_operand
                                                    (record_operation_chain
                                                      (record_operand
                                                        (atom
                                                          (ident)))
                                                      (ident))))
                                                (record_operand
                                                  (atom
                                                    (ident)))))))
                                        (term
                                          (uni_term
                                            (let_expr
                                              (pattern
                                                (ident))
                                              (term
                                                (uni_term
                                                  (infix_expr
                                                    (applicative
                                                      (applicative
                                                        (record_operand
                                                          (record_operation_chain
                                                            (record_operand
                                                              (atom
                                                                (ident)))
                                                            (ident))))
                                                      (record_operand
                                                        (atom
                                                          (ident)))))))
                                              (term
                                                (uni_term
                                                  (infix_expr
                                                    (applicative
                                                      (applicative
                                                        (applicative
                                                          (record_operand
                                                            (atom
                                                              (ident))))
                                                        (record_operand
                                                          (atom
                                                            (ident))))
                                                      (record_operand
                                                        (atom
                                                          (uni_term
                                                            (infix_expr
                                                              (applicative
                                                                (applicative
                                                                  (applicative
                                                                    (applicative
                                                                      (record_operand
                                                                        (atom
                                                                          (ident))))
                                                                    (record_operand
                                                                      (atom
                                                                        (ident))))
                                                                  (record_operand
                                                                    (atom
                                                                      (ident))))
                                                                (record_operand
                                                                  (atom
                                                                    (ident))))))))))))))))))))))))))))))))
      (comment)
      (term
        (uni_term
          (let_expr
            (pattern
              (ident))
            (term
              (uni_term
                (infix_expr
                  (applicative
                    (applicative
                      (applicative
                        (record_operand
                          (record_operation_chain
                            (record_operand
                              (atom
                                (ident)))
                            (ident))))
                      (record_operand
                        (atom
                          (uni_term
                            (fun_expr
                              (pattern
                                (ident))
                              (term
                                (uni_term
                                  (infix_expr
                                    (infix_expr
                                      (applicative
                                        (record_operand
                                          (atom
                                            (ident)))))
                                    (infix_b_op_4)
                                    (infix_expr
                                      (applicative
                                        (record_operand
                                          (atom
                                            (num_literal)))))))))))))
                    (record_operand
                      (atom
                        (term
                          (uni_term
                            (infix_expr
                              (applicative
                                (record_operand
                                  (atom
                                    (num_literal)))))))
                        (term
                          (uni_term
                            (infix_expr
                              (applicative
                                (record_operand
                                  (atom
                                    (num_literal)))))))
                        (term
                          (uni_term
                            (infix_expr
                              (applicative
                                (record_operand
                                  (atom
                                    (num_literal)))))))
                        (term
                          (uni_term
                            (infix_expr
                              (applicative
                                (record_operand
                                  (atom
                                    (num_literal)))))))
                        (term
                          (uni_term
                            (infix_expr
                              (applicative
                                (record_operand
                                  (atom
                                    (num_literal)))))))
                        (term
                          (uni_term
                            (infix_expr
                              (applicative
                                (record_operand
                                  (atom
                                    (num_literal)))))))))))))
            (term
              (uni_term
                (infix_expr
                  (applicative
                    (applicative
                      (applicative
                        (applicative
                          (record_operand
                            (record_operation_chain
                              (record_operand
                                (atom
                                  (ident)))
                              (ident))))
                        (record_operand
                          (atom
                            (uni_term
                              (fun_expr
                                (pattern
                                  (ident))
                                (pattern
                                  (ident))
                                (term
                                  (uni_term
                                    (infix_expr
                                      (infix_expr
                                        (applicative
                                          (record_operand
                                            (atom
                                              (ident)))))
                                      (infix_b_op_3)
                                      (infix_expr
                                        (applicative
                                          (record_operand
                                            (atom
                                              (ident)))))))))))))
                      (record_operand
                        (atom
                          (ident))))
                    (record_operand
                      (atom
                        (num_literal)))))))))))))
