<commit_before>
(*
Zipperposition: a functional superposition prover for prototyping
Copyright (c) 2013, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.  Redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)

(** {1 View a Literal as an arithmetic Literal}. *)

open Logtk

module T = FOTerm
module AT = ArithTerm
module S = Symbol
module M = Monome
module TO = Theories.TotalOrder
module Literals = Literal.Arr

let prof_arith_extract = Util.mk_profiler "arith.extract"

let is_arith lit = match lit with
| Literal.Equation (l, r, _, _) -> AT.is_arith l || AT.is_arith r
| Literal.Prop (p, _) -> AT.is_arith p
| Literal.True
| Literal.False -> false

let mk_less t1 t2 =
  Literal.mk_true (AT.mk_less t1 t2)

let mk_lesseq t1 t2 =
  Literal.mk_true (AT.mk_lesseq t1 t2)

let mk_eq ~ord t1 t2 =
  if AT.is_arith_const t1 && AT.is_arith_const t2
    then if T.eq t1 t2
      then Literal.mk_tauto
      else Literal.mk_absurd
    else Literal.mk_eq ~ord t1 t2

let mk_neq ~ord t1 t2 =
  if AT.is_arith_const t1 && AT.is_arith_const t2
    then if T.eq t1 t2
      then Literal.mk_absurd
      else Literal.mk_tauto
    else Literal.mk_neq ~ord t1 t2

let _uminus = S.Arith.Op.uminus

(** Comparison operator *)
type op =
  | Eq
  | Neq
  | Lt
  | Leq

let _pp_op buf op = match op with
  | Eq -> Buffer.add_string buf "="
  | Neq -> Buffer.add_string buf "≠"
  | Lt -> Buffer.add_string buf "<"
  | Leq -> Buffer.add_string buf "≤"

let _mk_op ~ord op l r = match op with
  | Eq -> mk_eq ~ord l r
  | Neq -> mk_neq ~ord l r
  | Lt -> mk_less l r
  | Leq -> mk_lesseq l r

(** Side of a particular term w.r.t the comparison operator *)
type side =
  | Left
  | Right

let flip = function
  | Left -> Right
  | Right -> Left

(** {3 Canonical Representation} *)

module Canonical = struct
  type t =
  | True
  | False
  | Compare of op * Monome.t * Monome.t

  let monomes = function
    | True
    | False -> raise (Invalid_argument "Canonical.monomes")
    | Compare (_, m1, m2) -> m1, m2

  let op = function
    | True | False -> raise (Invalid_argument "Canonical.op")
    | Compare (op, _, _) -> op

  let pp buf lit = match lit with
  | True -> Buffer.add_string buf "true"
  | False -> Buffer.add_string buf "false"
  | Compare (op, m1, m2) ->
    Printf.bprintf buf "%a %a %a" M.pp m1 _pp_op op M.pp m2

  let to_string = Util.on_buffer pp
  let fmt fmt t = Format.pp_print_string fmt (to_string t)

  let of_monome op m =
    if M.is_const m
    then match op with
      | Eq -> if M.sign m = 0 then True else False
      | Neq -> if M.sign m = 0 then False else True
      | Lt -> if M.sign m < 0 then True else False
      | Leq -> if M.sign m <= 0 then True else False
    else
      let m = M.normalize_wrt_zero m in
      let m, op = match op with
        (*
        | Lt when Type.eq Type.int (M.ty m) ->
          (* m < 0 ---> m+1 <= 0 *)
          M.succ m, Leq
        *)
        | Leq when Type.eq Type.int (M.ty m) ->
          (* m <= 0 ---> m-1 < 0 *)
          M.pred m, Lt
        | _ -> m, op
      in
      let m1, m2 = M.split m in
      Compare(op, m1, m2)

  let _extract lit =
    (* extract literal from (l=r | l!=r) *)
    let extract_eqn l r sign =
      let m1 = M.of_term l in
      let m2 = M.of_term r in
      let m = M.difference m1 m2 in
      of_monome (if sign then Eq else Neq) m
    (* extract lit from (l <= r | l < r) *)
    and extract_less ~strict l r =
      let m1 = M.of_term l in
      let m2 = M.of_term r in
      let m = M.difference m1 m2 in
      of_monome (if strict then Lt else Leq) m
    in
    let extract_le a b = extract_less ~strict:false a b in
    let extract_lt a b = extract_less ~strict:true a b in
    let ans = match lit with
    | Literal.True -> True
    | Literal.False -> False
    | Literal.Equation (l, r, sign, _) ->
      if AT.is_arith l || AT.is_arith r
        then extract_eqn l r sign
        else raise M.NotLinear<commit_msg>rollback bda75bedfe1955fb4286324537fdd44c7239009e: <= better than < for ints<commit_after>
(*
Zipperposition: a functional superposition prover for prototyping
Copyright (c) 2013, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.  Redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)

(** {1 View a Literal as an arithmetic Literal}. *)

open Logtk

module T = FOTerm
module AT = ArithTerm
module S = Symbol
module M = Monome
module TO = Theories.TotalOrder
module Literals = Literal.Arr

let prof_arith_extract = Util.mk_profiler "arith.extract"

let is_arith lit = match lit with
| Literal.Equation (l, r, _, _) -> AT.is_arith l || AT.is_arith r
| Literal.Prop (p, _) -> AT.is_arith p
| Literal.True
| Literal.False -> false

let mk_less t1 t2 =
  Literal.mk_true (AT.mk_less t1 t2)

let mk_lesseq t1 t2 =
  Literal.mk_true (AT.mk_lesseq t1 t2)

let mk_eq ~ord t1 t2 =
  if AT.is_arith_const t1 && AT.is_arith_const t2
    then if T.eq t1 t2
      then Literal.mk_tauto
      else Literal.mk_absurd
    else Literal.mk_eq ~ord t1 t2

let mk_neq ~ord t1 t2 =
  if AT.is_arith_const t1 && AT.is_arith_const t2
    then if T.eq t1 t2
      then Literal.mk_absurd
      else Literal.mk_tauto
    else Literal.mk_neq ~ord t1 t2

let _uminus = S.Arith.Op.uminus

(** Comparison operator *)
type op =
  | Eq
  | Neq
  | Lt
  | Leq

let _pp_op buf op = match op with
  | Eq -> Buffer.add_string buf "="
  | Neq -> Buffer.add_string buf "≠"
  | Lt -> Buffer.add_string buf "<"
  | Leq -> Buffer.add_string buf "≤"

let _mk_op ~ord op l r = match op with
  | Eq -> mk_eq ~ord l r
  | Neq -> mk_neq ~ord l r
  | Lt -> mk_less l r
  | Leq -> mk_lesseq l r

(** Side of a particular term w.r.t the comparison operator *)
type side =
  | Left
  | Right

let flip = function
  | Left -> Right
  | Right -> Left

(** {3 Canonical Representation} *)

module Canonical = struct
  type t =
  | True
  | False
  | Compare of op * Monome.t * Monome.t

  let monomes = function
    | True
    | False -> raise (Invalid_argument "Canonical.monomes")
    | Compare (_, m1, m2) -> m1, m2

  let op = function
    | True | False -> raise (Invalid_argument "Canonical.op")
    | Compare (op, _, _) -> op

  let pp buf lit = match lit with
  | True -> Buffer.add_string buf "true"
  | False -> Buffer.add_string buf "false"
  | Compare (op, m1, m2) ->
    Printf.bprintf buf "%a %a %a" M.pp m1 _pp_op op M.pp m2

  let to_string = Util.on_buffer pp
  let fmt fmt t = Format.pp_print_string fmt (to_string t)

  let of_monome op m =
    if M.is_const m
    then match op with
      | Eq -> if M.sign m = 0 then True else False
      | Neq -> if M.sign m = 0 then False else True
      | Lt -> if M.sign m < 0 then True else False
      | Leq -> if M.sign m <= 0 then True else False
    else
      let m = M.normalize_wrt_zero m in
      let m, op = match op with
        | Lt when Type.eq Type.int (M.ty m) ->
          (* m < 0 ---> m+1 <= 0 *)
          M.succ m, Leq
        (*
        | Leq when Type.eq Type.int (M.ty m) ->
          (* m <= 0 ---> m-1 < 0 *)
          M.pred m, Lt
        *)
        | _ -> m, op
      in
      let m1, m2 = M.split m in
      let lit = Compare(op, m1, m2) in
      Util.debug 5 "canonical.of_monome %a %a 0 ---> %a" M.pp m _pp_op op pp lit;
      lit

  let _extract lit =
    (* extract literal from (l=r | l!=r) *)
    let extract_eqn l r sign =
      let m1 = M.of_term l in
      let m2 = M.of_term r in
      let m = M.difference m1 m2 in
      of_monome (if sign then Eq else Neq) m
    (* extract lit from (l <= r | l < r) *)
    and extract_less ~strict l r =
      let m1 = M.of_term l in
      let m2 = M.of_term r in
      let m = M.difference m1 m2 in
      of_monome (if strict then Lt else Leq) m
    in
    let extract_le a b = extract_less ~strict:false a b in
    let extract_lt a b = extract_less ~strict:true a b in
    let ans = match lit with
    | Literal.True -> True
    | Literal.False -> False
    | Literal.Equation (l, r, sign, _) ->
      if AT.is_arith l || AT.is_arith r
        then extract_eqn l r sign
        else raise M.NotLinear
{'fraction_non_alphanumeric': 0.08806983511154219, 'fraction_numerical': 0.01425800193986421, 'mean_word_length': 2.6550868486352357, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3659140', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3467, 'n_words': 1399}