
(** OCaml driver with Why3 type int being mapped to OCaml type int.

    This is of course unsafe, yet useful to run your code when you
    have an independent argument for the absence of arithmetic
    overflows. *)

printer "ocaml-unsafe-int"

theory BuiltIn
  syntax type int "int"
  syntax predicate  (=)   "(%1 = %2)"
end

(* import "ocaml-no-arith.drv" *)

(* int *)

theory int.Int
  syntax constant zero "0"
  syntax constant one  "1"

  syntax predicate (<)  "(%1 < %2)"
  syntax predicate (<=) "(%1 <= %2)"
  syntax predicate (>)  "(%1 > %2)"
  syntax predicate (>=) "(%1 >= %2)"

  syntax function (+)   "(%1 + %2)"
  syntax function (-)   "(%1 - %2)"
  syntax function ( * ) "(%1 * %2)"
  syntax function (-_)  "(- %1)"
end

theory int.ComputerDivision
  syntax function div "(%1 / %2)"
  syntax function mod "(%1 mod %2)"
end

(* FIXME: avoid Pervasives using a black list of reserved OCaml names *)

theory int.Abs
  syntax function abs "Pervasives.abs"
end

theory int.MinMax
  syntax function min "Pervasives.min"
  syntax function max "Pervasives.max"
end

(* TODO
   - int.EuclideanDivision
   - number.Gcd
*)

theory int.Power
  prelude "let rec power x n = if n = 0 then 1 else x * power x (n-1)"
  syntax function power "power"
end

theory int.Fact
  prelude "let rec fact n = if n <= 1 then 1 else n * fact (n-1)"
  syntax function fact "fact"
end

theory int.Fibonacci
  prelude "let rec fib n = if n <= 1 then n else fib (n-2) + fib (n-1)"
  syntax function fib "(fib %1)"
end

(* WhyML *)

module Ref
  syntax type     ref      "%1 ref"
  syntax function contents "!%1"
  syntax val      ref      "ref"
end

module ref.Ref
  syntax val      (!_)     "!%1"
  syntax val      (:=)     "%1 := %2"
end

module stack.Stack
  syntax type      t        "(%1 Stack.t)"
  syntax val       create   "Stack.create"
  syntax val       push     "Stack.push"
  syntax exception Empty    "Stack.Empty"
  syntax val       pop      "Stack.pop"
  syntax val       top      "Stack.top"
  syntax val       safe_pop "Stack.pop"
  syntax val       safe_top "Stack.top"
  syntax val       clear    "Stack.clear"
  syntax val       copy     "Stack.copy"
  syntax val       is_empty "Stack.is_empty"
  syntax val       length   "Stack.length"
end

module queue.Queue
  syntax type      t         "(%1 Queue.t)"
  syntax val       create    "Queue.create"
  syntax val       push      "Queue.push"
  syntax exception Empty     "Queue.Empty"
  syntax val       pop       "Queue.pop"
  syntax val       peek      "Queue.peek"
  syntax val       safe_pop  "Queue.pop"
  syntax val       safe_peek "Queue.peek"
  syntax val       clear     "Queue.clear"
  syntax val       copy      "Queue.copy"
  syntax val       is_empty  "Queue.is_empty"
  syntax val       length    "Queue.length"
  syntax val       transfer  "Queue.transfer"
end

module array.Array
  syntax type array "(%1 array)"

  syntax function ([]) "(%1.(%2))"

  syntax exception OutOfBounds "(Invalid_argument \"index out of bounds\")"

  syntax val ([])          "Array.unsafe_get"
  syntax val ([]<-)        "Array.unsafe_set"
  syntax val length        "Array.length"
  syntax val defensive_get "Array.get"
  syntax val defensive_set "Array.set"
  syntax val make          "Array.make"
  syntax val empty         "(%1; [||])"
  syntax val append        "Array.append"
  syntax val sub           "Array.sub"
  syntax val copy          "Array.copy"
  syntax val fill          "Array.fill"
  syntax val blit          "Array.blit"
end

module matrix.Matrix
  syntax type matrix "(%1 array array)"

  syntax exception OutOfBounds "(Invalid_argument \"index out of bounds\")"

  syntax function rows     "Array.length"
  syntax function columns  "(Array.length %1.(0))"
  syntax val rows          "Array.length"
  syntax val columns       "(fun m -> Array.length m.(0))"
  syntax val get           "(fun m i j -> m.(i).(j))"
  syntax val set           "(fun m i j v -> m.(i).(j) <- v)"
  syntax val defensive_get "(fun m i j -> m.(i).(j))"
  syntax val defensive_set "(fun m i j v -> m.(i).(j) <- v)"
  syntax val make          "Array.make_matrix"
  syntax val copy          "(Array.map Array.copy)"
end

module mach.int.Int31
  syntax val of_int   "%1"

  syntax function to_int "(%1)"

  syntax type     int31     "int"
  syntax val      ( + )     "( + )"
  syntax val      ( - )     "( - )"
  syntax val      (-_)      "( ~- )"
  syntax val      ( * )     "( * )"
  syntax val      ( / )     "( / )"
  syntax val      ( % )     "(mod)"
  syntax val      (=)       "(=)"
  syntax val      (<=)      "(<=)"
  syntax val      (<)       "(<)"
  syntax val      (>=)      "(>=)"
  syntax val      (>)       "(>)"
end
module mach.int.Int63
  syntax val      of_int   "%1"

  syntax function to_int "(%1)"

  syntax type     int63     "int"
  syntax val      ( + )     "( + )"
  syntax val      ( - )     "( - )"
  syntax val      (-_)      "( ~- )"
  syntax val      ( * )     "( * )"
  syntax val      ( / )     "( / )"
  syntax val      ( % )     "(mod)"
  syntax val      (=)       "(=)"
  syntax val      (<=)      "(<=)"
  syntax val      (<)       "(<)"
  syntax val      (>=)      "(>=)"
  syntax val      (>)       "(>)"
end
module mach.int.Refint63
  syntax val incr "Pervasives.incr"
  syntax val decr "Pervasives.decr"
  syntax val (+=) "(fun r v -> Pervasives.(:=) r (Pervasives.(!) r + v))"
  syntax val (-=) "(fun r v -> Pervasives.(:=) r (Pervasives.(!) r - v))"
  syntax val ( *= ) "(fun r v -> Pervasives.(:=) r (Pervasives.(!) r * v))"
end
module mach.int.MinMax63
  syntax val min "Pervasives.min"
  syntax val max "Pervasives.max"
end

(* TODO
   other mach.int.XXX modules *)

module mach.array.Array31
  syntax type array  "(%1 array)"

  syntax val  make   "Array.make"
  syntax val  ([])   "Array.get"
  syntax val  ([]<-) "Array.set"
  syntax val  length "Array.length"
  syntax val  append "Array.append"
  syntax val  sub    "Array.sub"
  syntax val  copy   "Array.copy"
  syntax val  fill   "Array.fill"
  syntax val  blit   "Array.blit"
  syntax val  self_blit "Array.blit"
end
module mach.array.Array63
  syntax type array   "(%1 array)"

  syntax val  make   "Array.make"
  syntax val  ([])   "Array.get"
  syntax val  ([]<-) "Array.set"
  syntax val  length "Array.length"
  syntax val  append "Array.append"
  syntax val  sub    "Array.sub"
  syntax val  copy   "Array.copy"
  syntax val  fill   "Array.fill"
  syntax val  blit   "Array.blit"
  syntax val  self_blit "Array.blit"
end
module mach.matrix.Matrix63
  syntax type matrix "(%1 array array)"

  syntax exception OutOfBounds "(Invalid_argument \"index out of bounds\")"

  syntax function rows     "Array.length"
  syntax function columns  "(Array.length %1.(0))"
  syntax val rows          "Array.length"
  syntax val columns       "(fun m -> Array.length m.(0))"
  syntax val get           "(fun m i j -> m.(i).(j))"
  syntax val set           "(fun m i j v -> m.(i).(j) <- v)"
  syntax val defensive_get "(fun m i j -> m.(i).(j))"
  syntax val defensive_set "(fun m i j v -> m.(i).(j) <- v)"
  syntax val make          "Array.make_matrix"
  syntax val copy          "(Array.map Array.copy)"
end
