1:- module(esim_price_per_gb,
    2          [ per_gb/3,              % +Price, +GB, -PricePerGB
    3            trip_need_gb/3,        % +Days, +Usage, -GB
    4            plan_cost/4,           % +NeedGB, +Plan, -Cost, -WastedGB
    5            compare_plans/3,       % +NeedGB, +Plans, -Rows
    6            cheapest_plan/3,       % +NeedGB, +Plans, -Row
    7            break_even_gb/3        % +BundlePlan, +PaygPlan, -GB
    8          ]).

Compare mobile data plans by what you actually pay per GB

Travel eSIM plans are sold in very different shapes: fixed bundles (10 GB for 30 days), unlimited-but-throttled day passes, and plain pay-as-you-go balances. The headline price hides two things that matter: the real price per GB, and how much of a bundle you will throw away when the trip ends.

This library works both out. Plans are terms:

bundle(Name, GB, Price)
A fixed allowance. If you need more than GB you buy another copy.
payg(Name, PricePerGB)
Pay as you go, charged for what you use.
payg(Name, PricePerGB, StepGB)
Pay as you go, but topped up in StepGB blocks (for example 1 GB).

Prices are plain numbers in whatever currency you like. GB values may be integers or floats. Costs are rounded to cents.

   31:- use_module(library(lists)).   32:- use_module(library(pairs)).   33:- use_module(library(error)).
 per_gb(+Price, +GB, -PricePerGB) is det
Price of one GB, rounded to cents. per_gb(18, 10, X) gives X = 1.8.
   39per_gb(Price, GB, PerGB) :-
   40    must_be(number, Price),
   41    (   Price >= 0 -> true ; type_error(nonneg, Price) ),
   42    pos_number(GB),
   43    PerGB is round(Price / GB * 100) / 100.
 trip_need_gb(+Days, +Usage, -GB) is det
Rough data need for a trip. Usage is one of:

Uses 1 GB = 1024 MB, which is how most phones report usage. The result is rounded up to one decimal place so you never plan for slightly less than you need.

   58trip_need_gb(Days, Usage, GB) :-
   59    must_be(positive_integer, Days),
   60    usage_mb(Usage, MB),
   61    Raw is Days * MB / 1024,
   62    GB is ceiling(Raw * 10) / 10.
   63
   64usage_mb(light, 150) :- !.
   65usage_mb(standard, 500) :- !.
   66usage_mb(heavy, 1500) :- !.
   67usage_mb(mb_per_day(MB), MB) :- !, pos_number(MB).
   68usage_mb(Other, _) :-
   69    domain_error(usage(light;standard;heavy;mb_per_day(mb)), Other).
 plan_cost(+NeedGB, +Plan, -Cost, -WastedGB) is det
What Plan costs to cover NeedGB, and how many GB you pay for but do not use.
   76plan_cost(Need, bundle(_, GB, Price), Cost, Wasted) :- !,
   77    pos_number(GB),
   78    Copies is max(1, ceiling(Need / GB)),
   79    Cost is round(Copies * Price * 100) / 100,
   80    Wasted is round((Copies * GB - Need) * 100) / 100.
   81plan_cost(Need, payg(_, PerGB), Cost, 0) :- !,
   82    Cost is round(Need * PerGB * 100) / 100.
   83plan_cost(Need, payg(_, PerGB, Step), Cost, Wasted) :- !,
   84    pos_number(Step),
   85    Blocks is max(1, ceiling(Need / Step)),
   86    Bought is Blocks * Step,
   87    Cost is round(Bought * PerGB * 100) / 100,
   88    Wasted is round((Bought - Need) * 100) / 100.
   89plan_cost(_, Plan, _, _) :-
   90    domain_error(plan, Plan).
   91
   92plan_name(bundle(N, _, _), N).
   93plan_name(payg(N, _), N).
   94plan_name(payg(N, _, _), N).
 compare_plans(+NeedGB, +Plans, -Rows) is det
Rows is a list of row(Name, Cost, EffectivePerGB, WastedGB), cheapest first. EffectivePerGB is Cost divided by the GB you actually use, so leftover data counts against a plan. Ties on cost are broken by less waste.
  103compare_plans(Need, Plans, Rows) :-
  104    pos_number(Need),
  105    must_be(list, Plans),
  106    findall(Cost-Wasted-row(Name, Cost, Eff, Wasted),
  107            ( member(P, Plans),
  108              plan_cost(Need, P, Cost, Wasted),
  109              plan_name(P, Name),
  110              per_gb(Cost, Need, Eff)
  111            ),
  112            Keyed),
  113    msort(Keyed, Sorted),
  114    pairs_values(Sorted, Rows).
 cheapest_plan(+NeedGB, +Plans, -Row) is semidet
The first row of compare_plans/3. Fails on an empty plan list.
  120cheapest_plan(Need, Plans, Row) :-
  121    compare_plans(Need, Plans, [Row|_]).
 break_even_gb(+Bundle, +Payg, -GB) is det
Usage, in GB, at which a single copy of Bundle becomes cheaper than paying as you go. Below this, pay as you go wins. If the bundle is never cheaper within its own allowance, GB is never.
  129break_even_gb(bundle(_, Allow, Price), Payg, GB) :-
  130    payg_rate(Payg, Rate),
  131    Rate > 0,
  132    X is Price / Rate,
  133    (   X =< Allow
  134    ->  GB is round(X * 100) / 100
  135    ;   GB = never
  136    ).
  137
  138payg_rate(payg(_, R), R).
  139payg_rate(payg(_, R, _), R).
  140
  141pos_number(X) :-
  142    must_be(number, X),
  143    (   X > 0
  144    ->  true
  145    ;   type_error(positive_number, X)
  146    )