units.pl 733 Bytes
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
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
:- module(units,
  [
    infer/2
  ]
).

infer(A + B, AUnit) :-
  infer(A, AUnit),
  infer(B, BUnit),
  check_homogeneous(AUnit, BUnit).

infer(A - B, AUnit) :-
  infer(A, AUnit),
  infer(B, BUnit),
  check_homogeneous(AUnit, BUnit).

infer(A * B, Unit) :-
  infer(A, AUnit),
  infer(B, BUnit),
  product_unit(AUnit, BUnit, Unit).

infer(A / B, Unit) :-
  infer(A, AUnit),
  infer(B, BUnit),
  inverse_unit(BUnit, BUnitInv),
  product_unit(AUnit, BUnitInv, Unit).

infer(A ^ B, Unit) :-
  infer(A, AUnit),
  infer(B, BUnit),
  inverse_unit(BUnit, BUnitInv),
  product_unit(AUnit, BUnitInv, Unit).


check_homogeneous(AUnit, BUnit) :-
  (
    AUnit = BUnit
  ->
    true
  ;
    throw(error(not_homogeneous(A, AUnit, B, BUnit)))
  ).