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 ]).
31:- use_module(library(lists)). 32:- use_module(library(pairs)). 33:- use_module(library(error)).
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.mb_per_day(MB) your own figure, for example from last month's usageUses 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).
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).
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).
120cheapest_plan(Need, Plans, Row) :-
121 compare_plans(Need, Plans, [Row|_]).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 )
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:
Prices are plain numbers in whatever currency you like. GB values may be integers or floats. Costs are rounded to cents.