1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
------------------------------------------------------------------------------
--- Library for constraint programming with arithmetic constraints over reals.
---
--- @category general
------------------------------------------------------------------------------

module CLPR((+.),(-.),(*.),(/.),(<.),(>.),(<=.),(>=.),i2f,
            minimumFor,minimize,maximumFor,maximize) where

-- The operator declarations are similar to the standard arithmetic operators.

infixl 7 *., /.
infixl 6 +., -.
infix  4 <., >., <=., >=.


--- Addition on floats in arithmetic constraints.

(+.)   :: Float -> Float -> Float
x +. y = (prim_CLPR_plus $! y) $! x

prim_CLPR_plus :: Float -> Float -> Float
prim_CLPR_plus external

--- Subtraction on floats in arithmetic constraints.

(-.)   :: Float -> Float -> Float
x -. y = (prim_CLPR_minus $! y) $! x

prim_CLPR_minus :: Float -> Float -> Float
prim_CLPR_minus external

--- Multiplication on floats in arithmetic constraints.

(*.)   :: Float -> Float -> Float
x *. y = (prim_CLPR_times $! y) $! x

prim_CLPR_times :: Float -> Float -> Float
prim_CLPR_times external

--- Division on floats in arithmetic constraints.

(/.)   :: Float -> Float -> Float
x /. y = (prim_CLPR_div $! y) $! x

prim_CLPR_div :: Float -> Float -> Float
prim_CLPR_div external

--- "Less than" constraint on floats.

(<.)   :: Float -> Float -> Bool
x <. y = (prim_CLPR_le $! y) $! x

prim_CLPR_le :: Float -> Float -> Bool
prim_CLPR_le external

--- "Greater than" constraint on floats.

(>.)   :: Float -> Float -> Bool
x >. y = (prim_CLPR_ge $! y) $! x

prim_CLPR_ge :: Float -> Float -> Bool
prim_CLPR_ge external

--- "Less than or equal" constraint on floats.

(<=.)  :: Float -> Float -> Bool
x <=. y = (prim_CLPR_leq $! y) $! x

prim_CLPR_leq :: Float -> Float -> Bool
prim_CLPR_leq external

--- "Greater than or equal" constraint on floats.

(>=.)  :: Float -> Float -> Bool
x >=. y = (prim_CLPR_geq $! y) $! x

prim_CLPR_geq :: Float -> Float -> Bool
prim_CLPR_geq external

--- Conversion function from integers to floats.
--- Rigid in the first argument, i.e., suspends until the first argument
--- is ground.

i2f    :: Int -> Float
i2f x = prim_CLPR_i2f $# x

prim_CLPR_i2f :: Int -> Float
prim_CLPR_i2f external


--- Computes the minimum with respect to a given constraint.
--- (minimumFor g f) evaluates to x if (g x) is satisfied and
--- (f x) is minimal. The evaluation fails if such a minimal value
--- does not exist. The evaluation suspends if it contains
--- unbound non-local variables.

minimumFor :: (a -> Bool) -> (a -> Float) -> a
minimumFor external

--- Minimization constraint.
--- (minimize g f x) is satisfied if (g x) is satisfied and
--- (f x) is minimal. The evaluation suspends if it contains
--- unbound non-local variables.

minimize :: (a -> Bool) -> (a -> Float) -> a -> Bool
minimize g f x = minimumFor g f =:= x

--- Computes the maximum with respect to a given constraint.
--- (maximumFor g f) evaluates to x if (g x) is satisfied and
--- (f x) is maximal. The evaluation fails if such a maximal value
--- does not exist. The evaluation suspends if it contains
--- unbound non-local variables.

maximumFor :: (a -> Bool) -> (a -> Float) -> a
maximumFor external

--- Maximization constraint.
--- (maximize g f x) is satisfied if (g x) is satisfied and
--- (f x) is maximal. The evaluation suspends if it contains
--- unbound non-local variables.

maximize :: (a -> Bool) -> (a -> Float) -> a -> Bool
maximize g f x = maximumFor g f =:= x


-- end of CLPR