Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 24,816 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/-
Copyright (c) 2020 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anatole Dedecker
-/
import analysis.calculus.mean_value

/-!
# L'HΓ΄pital's rule for 0/0 indeterminate forms

In this file, we prove several forms of "L'Hopital's rule" for computing 0/0
indeterminate forms. The proof of `has_deriv_at.lhopital_zero_right_on_Ioo`
is based on the one given in the corresponding
[Wikibooks](https://en.wikibooks.org/wiki/Calculus/L%27H%C3%B4pital%27s_Rule)
chapter, and all other statements are derived from this one by composing by
carefully chosen functions.

Note that the filter `f'/g'` tends to isn't required to be one of `𝓝 a`,
`at_top` or `at_bot`. In fact, we give a slightly stronger statement by
allowing it to be any filter on `ℝ`.

Each statement is available in a `has_deriv_at` form and a `deriv` form, which
is denoted by each statement being in either the `has_deriv_at` or the `deriv`
namespace.

## Tags

L'HΓ΄pital's rule, L'Hopital's rule
-/

open filter set
open_locale filter topological_space pointwise

variables {a b : ℝ} (hab : a < b) {l : filter ℝ} {f f' g g' : ℝ β†’ ℝ}

/-!
## Interval-based versions

We start by proving statements where all conditions (derivability, `g' β‰  0`) have
to be satisfied on an explicitly-provided interval.
-/

namespace has_deriv_at

include hab

theorem lhopital_zero_right_on_Ioo
  (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x)
  (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0)
  (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[>] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l :=
begin
  have sub : βˆ€ x ∈ Ioo a b, Ioo a x βŠ† Ioo a b := Ξ» x hx, Ioo_subset_Ioo (le_refl a) (le_of_lt hx.2),
  have hg : βˆ€ x ∈ (Ioo a b), g x β‰  0,
  { intros x hx h,
    have : tendsto g (𝓝[<] x) (𝓝 0),
    { rw [← h, ← nhds_within_Ioo_eq_nhds_within_Iio hx.1],
      exact ((hgg' x hx).continuous_at.continuous_within_at.mono $ sub x hx).tendsto },
    obtain ⟨y, hyx, hy⟩ : βˆƒ c ∈ Ioo a x, g' c = 0,
      from exists_has_deriv_at_eq_zero' hx.1 hga this (Ξ» y hy, hgg' y $ sub x hx hy),
    exact hg' y (sub x hx hyx) hy },
  have : βˆ€ x ∈ Ioo a b, βˆƒ c ∈ Ioo a x, (f x) * (g' c) = (g x) * (f' c),
  { intros x hx,
    rw [← sub_zero (f x), ← sub_zero (g x)],
    exact exists_ratio_has_deriv_at_eq_ratio_slope' g g' hx.1 f f'
      (Ξ» y hy, hgg' y $ sub x hx hy) (Ξ» y hy, hff' y $ sub x hx hy) hga hfa
      (tendsto_nhds_within_of_tendsto_nhds (hgg' x hx).continuous_at.tendsto)
      (tendsto_nhds_within_of_tendsto_nhds (hff' x hx).continuous_at.tendsto) },
  choose! c hc using this,
  have : βˆ€ x ∈ Ioo a b, ((Ξ» x', (f' x') / (g' x')) ∘ c) x = f x / g x,
  { intros x hx,
    rcases hc x hx with ⟨h₁, hβ‚‚βŸ©,
    field_simp [hg x hx, hg' (c x) ((sub x hx) h₁)],
    simp only [hβ‚‚],
    rwa mul_comm },
  have cmp : βˆ€ x ∈ Ioo a b, a < c x ∧ c x < x,
    from Ξ» x hx, (hc x hx).1,
  rw ← nhds_within_Ioo_eq_nhds_within_Ioi hab,
  apply tendsto_nhds_within_congr this,
  simp only,
  apply hdiv.comp,
  refine tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _
    (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds
      (tendsto_nhds_within_of_tendsto_nhds tendsto_id) _ _) _,
  all_goals
  { apply eventually_nhds_within_of_forall,
    intros x hx,
    have := cmp x hx,
    try {simp},
    linarith [this] }
end

theorem lhopital_zero_right_on_Ico
  (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x)
  (hcf : continuous_on f (Ico a b)) (hcg : continuous_on g (Ico a b))
  (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0)
  (hfa : f a = 0) (hga : g a = 0)
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (nhds_within a (Ioi a)) l) :
  tendsto (Ξ» x, (f x) / (g x)) (nhds_within a (Ioi a)) l :=
begin
  refine lhopital_zero_right_on_Ioo hab hff' hgg' hg' _ _ hdiv,
  { rw [← hfa, ← nhds_within_Ioo_eq_nhds_within_Ioi hab],
    exact ((hcf a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto },
  { rw [← hga, ← nhds_within_Ioo_eq_nhds_within_Ioi hab],
    exact ((hcg a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto },
end

theorem lhopital_zero_left_on_Ioo
  (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x)
  (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0)
  (hfb : tendsto f (nhds_within b (Iio b)) (𝓝 0)) (hgb : tendsto g (nhds_within b (Iio b)) (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (nhds_within b (Iio b)) l) :
  tendsto (Ξ» x, (f x) / (g x)) (nhds_within b (Iio b)) l :=
begin
  -- Here, we essentially compose by `has_neg.neg`. The following is mostly technical details.
  have hdnf : βˆ€ x ∈ -Ioo a b, has_deriv_at (f ∘ has_neg.neg) (f' (-x) * (-1)) x,
    from Ξ» x hx, comp x (hff' (-x) hx) (has_deriv_at_neg x),
  have hdng : βˆ€ x ∈ -Ioo a b, has_deriv_at (g ∘ has_neg.neg) (g' (-x) * (-1)) x,
    from Ξ» x hx, comp x (hgg' (-x) hx) (has_deriv_at_neg x),
  rw preimage_neg_Ioo at hdnf,
  rw preimage_neg_Ioo at hdng,
  have := lhopital_zero_right_on_Ioo (neg_lt_neg hab) hdnf hdng
    (by { intros x hx h,
          apply hg' _ (by {rw ← preimage_neg_Ioo at hx, exact hx}),
          rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h })
    (hfb.comp tendsto_neg_nhds_within_Ioi_neg)
    (hgb.comp tendsto_neg_nhds_within_Ioi_neg)
    (by { simp only [neg_div_neg_eq, mul_one, mul_neg],
          exact (tendsto_congr $ Ξ» x, rfl).mp (hdiv.comp tendsto_neg_nhds_within_Ioi_neg) }),
  have := this.comp tendsto_neg_nhds_within_Iio,
  unfold function.comp at this,
  simpa only [neg_neg]
end

theorem lhopital_zero_left_on_Ioc
  (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x)
  (hcf : continuous_on f (Ioc a b)) (hcg : continuous_on g (Ioc a b))
  (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0)
  (hfb : f b = 0) (hgb : g b = 0)
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (nhds_within b (Iio b)) l) :
  tendsto (Ξ» x, (f x) / (g x)) (nhds_within b (Iio b)) l :=
begin
  refine lhopital_zero_left_on_Ioo hab hff' hgg' hg' _ _ hdiv,
  { rw [← hfb, ← nhds_within_Ioo_eq_nhds_within_Iio hab],
    exact ((hcf b $ right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto },
  { rw [← hgb, ← nhds_within_Ioo_eq_nhds_within_Iio hab],
    exact ((hcg b $ right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto },
end

omit hab

theorem lhopital_zero_at_top_on_Ioi
  (hff' : βˆ€ x ∈ Ioi a, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioi a, has_deriv_at g (g' x) x)
  (hg' : βˆ€ x ∈ Ioi a, g' x β‰  0)
  (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_top l) :
  tendsto (Ξ» x, (f x) / (g x)) at_top l :=
begin
  obtain ⟨ a', haa', ha'⟩ : βˆƒ a', a < a' ∧ 0 < a' :=
    ⟨1 + max a 0, ⟨lt_of_le_of_lt (le_max_left a 0) (lt_one_add _),
                   lt_of_le_of_lt (le_max_right a 0) (lt_one_add _)⟩⟩,
  have fact1 : βˆ€ (x:ℝ), x ∈ Ioo 0 a'⁻¹ β†’ x β‰  0 := Ξ» _ hx, (ne_of_lt hx.1).symm,
  have fact2 : βˆ€ x ∈ Ioo 0 a'⁻¹, a < x⁻¹,
    from Ξ» _ hx, lt_trans haa' ((lt_inv ha' hx.1).mpr hx.2),
  have hdnf : βˆ€ x ∈ Ioo 0 a'⁻¹, has_deriv_at (f ∘ has_inv.inv) (f' (x⁻¹) * (-(x^2)⁻¹)) x,
    from λ x hx, comp x (hff' (x⁻¹) $ fact2 x hx) (has_deriv_at_inv $ fact1 x hx),
  have hdng : βˆ€ x ∈ Ioo 0 a'⁻¹, has_deriv_at (g ∘ has_inv.inv) (g' (x⁻¹) * (-(x^2)⁻¹)) x,
    from λ x hx, comp x (hgg' (x⁻¹) $ fact2 x hx) (has_deriv_at_inv $ fact1 x hx),
  have := lhopital_zero_right_on_Ioo (inv_pos.mpr ha') hdnf hdng
    (by { intros x hx,
          refine mul_ne_zero _ (neg_ne_zero.mpr $ inv_ne_zero $ pow_ne_zero _ $ fact1 x hx),
          exact hg' _ (fact2 x hx) })
    (hftop.comp tendsto_inv_zero_at_top)
    (hgtop.comp tendsto_inv_zero_at_top)
    (by { refine (tendsto_congr' _).mp (hdiv.comp tendsto_inv_zero_at_top),
          rw eventually_eq_iff_exists_mem,
          use [Ioi 0, self_mem_nhds_within],
          intros x hx,
          unfold function.comp,
          erw mul_div_mul_right,
          refine neg_ne_zero.mpr (inv_ne_zero $ pow_ne_zero _ $ ne_of_gt hx) }),
  have := this.comp tendsto_inv_at_top_zero',
  unfold function.comp at this,
  simpa only [inv_inv],
end

theorem lhopital_zero_at_bot_on_Iio
  (hff' : βˆ€ x ∈ Iio a, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Iio a, has_deriv_at g (g' x) x)
  (hg' : βˆ€ x ∈ Iio a, g' x β‰  0)
  (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_bot l) :
  tendsto (Ξ» x, (f x) / (g x)) at_bot l :=
begin
  -- Here, we essentially compose by `has_neg.neg`. The following is mostly technical details.
  have hdnf : βˆ€ x ∈ -Iio a, has_deriv_at (f ∘ has_neg.neg) (f' (-x) * (-1)) x,
    from Ξ» x hx, comp x (hff' (-x) hx) (has_deriv_at_neg x),
  have hdng : βˆ€ x ∈ -Iio a, has_deriv_at (g ∘ has_neg.neg) (g' (-x) * (-1)) x,
    from Ξ» x hx, comp x (hgg' (-x) hx) (has_deriv_at_neg x),
  rw preimage_neg_Iio at hdnf,
  rw preimage_neg_Iio at hdng,
  have := lhopital_zero_at_top_on_Ioi hdnf hdng
    (by { intros x hx h,
          apply hg' _ (by {rw ← preimage_neg_Iio at hx, exact hx}),
          rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h })
    (hfbot.comp tendsto_neg_at_top_at_bot)
    (hgbot.comp tendsto_neg_at_top_at_bot)
    (by { simp only [mul_one, mul_neg, neg_div_neg_eq],
          exact (tendsto_congr $ Ξ» x, rfl).mp (hdiv.comp tendsto_neg_at_top_at_bot) }),
  have := this.comp tendsto_neg_at_bot_at_top,
  unfold function.comp at this,
  simpa only [neg_neg],
end

end has_deriv_at

namespace deriv

include hab

theorem lhopital_zero_right_on_Ioo
  (hdf : differentiable_on ℝ f (Ioo a b)) (hg' : βˆ€ x ∈ Ioo a b, deriv g x β‰  0)
  (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[>] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l :=
begin
  have hdf : βˆ€ x ∈ Ioo a b, differentiable_at ℝ f x,
    from Ξ» x hx, (hdf x hx).differentiable_at (Ioo_mem_nhds hx.1 hx.2),
  have hdg : βˆ€ x ∈ Ioo a b, differentiable_at ℝ g x,
    from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)),
  exact has_deriv_at.lhopital_zero_right_on_Ioo hab (Ξ» x hx, (hdf x hx).has_deriv_at)
    (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hfa hga hdiv
end

theorem lhopital_zero_right_on_Ico
  (hdf : differentiable_on ℝ f (Ioo a b))
  (hcf : continuous_on f (Ico a b)) (hcg : continuous_on g (Ico a b))
  (hg' : βˆ€ x ∈ (Ioo a b), (deriv g) x β‰  0)
  (hfa : f a = 0) (hga : g a = 0)
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (nhds_within a (Ioi a)) l) :
  tendsto (Ξ» x, (f x) / (g x)) (nhds_within a (Ioi a)) l :=
begin
  refine lhopital_zero_right_on_Ioo hab hdf hg' _ _ hdiv,
  { rw [← hfa, ← nhds_within_Ioo_eq_nhds_within_Ioi hab],
    exact ((hcf a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto },
  { rw [← hga, ← nhds_within_Ioo_eq_nhds_within_Ioi hab],
    exact ((hcg a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto },
end

theorem lhopital_zero_left_on_Ioo
  (hdf : differentiable_on ℝ f (Ioo a b))
  (hg' : βˆ€ x ∈ (Ioo a b), (deriv g) x β‰  0)
  (hfb : tendsto f (nhds_within b (Iio b)) (𝓝 0)) (hgb : tendsto g (nhds_within b (Iio b)) (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (nhds_within b (Iio b)) l) :
  tendsto (Ξ» x, (f x) / (g x)) (nhds_within b (Iio b)) l :=
begin
  have hdf : βˆ€ x ∈ Ioo a b, differentiable_at ℝ f x,
    from Ξ» x hx, (hdf x hx).differentiable_at (Ioo_mem_nhds hx.1 hx.2),
  have hdg : βˆ€ x ∈ Ioo a b, differentiable_at ℝ g x,
    from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)),
  exact has_deriv_at.lhopital_zero_left_on_Ioo hab (Ξ» x hx, (hdf x hx).has_deriv_at)
    (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hfb hgb hdiv
end

omit hab

theorem lhopital_zero_at_top_on_Ioi
  (hdf : differentiable_on ℝ f (Ioi a))
  (hg' : βˆ€ x ∈ (Ioi a), (deriv g) x β‰  0)
  (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_top l) :
  tendsto (Ξ» x, (f x) / (g x)) at_top l :=
begin
  have hdf : βˆ€ x ∈ Ioi a, differentiable_at ℝ f x,
    from Ξ» x hx, (hdf x hx).differentiable_at (Ioi_mem_nhds hx),
  have hdg : βˆ€ x ∈ Ioi a, differentiable_at ℝ g x,
    from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)),
  exact has_deriv_at.lhopital_zero_at_top_on_Ioi (Ξ» x hx, (hdf x hx).has_deriv_at)
    (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hftop hgtop hdiv,
end

theorem lhopital_zero_at_bot_on_Iio
  (hdf : differentiable_on ℝ f (Iio a))
  (hg' : βˆ€ x ∈ (Iio a), (deriv g) x β‰  0)
  (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_bot l) :
  tendsto (Ξ» x, (f x) / (g x)) at_bot l :=
begin
  have hdf : βˆ€ x ∈ Iio a, differentiable_at ℝ f x,
    from Ξ» x hx, (hdf x hx).differentiable_at (Iio_mem_nhds hx),
  have hdg : βˆ€ x ∈ Iio a, differentiable_at ℝ g x,
    from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)),
  exact has_deriv_at.lhopital_zero_at_bot_on_Iio (Ξ» x hx, (hdf x hx).has_deriv_at)
    (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hfbot hgbot hdiv,
end

end deriv

/-!
## Generic versions

The following statements no longer any explicit interval, as they only require
conditions holding eventually.
-/

namespace has_deriv_at

/-- L'HΓ΄pital's rule for approaching a real from the right, `has_deriv_at` version -/
theorem lhopital_zero_nhds_right
  (hff' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at f (f' x) x)
  (hgg' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at g (g' x) x)
  (hg' : βˆ€αΆ  x in 𝓝[>] a, g' x β‰  0)
  (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[>] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l :=
begin
  rw eventually_iff_exists_mem at *,
  rcases hff' with ⟨s₁, hs₁, hff'⟩,
  rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩,
  rcases hg' with ⟨s₃, hs₃, hg'⟩,
  let s := s₁ ∩ sβ‚‚ ∩ s₃,
  have hs : s ∈ 𝓝[>] a := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃,
  rw mem_nhds_within_Ioi_iff_exists_Ioo_subset at hs,
  rcases hs with ⟨u, hau, hu⟩,
  refine lhopital_zero_right_on_Ioo hau _ _ _ hfa hga hdiv;
  intros x hx;
  apply_assumption;
  exact (hu hx).1.1 <|> exact (hu hx).1.2 <|> exact (hu hx).2
end

/-- L'HΓ΄pital's rule for approaching a real from the left, `has_deriv_at` version -/
theorem lhopital_zero_nhds_left
  (hff' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at f (f' x) x)
  (hgg' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at g (g' x) x)
  (hg' : βˆ€αΆ  x in 𝓝[<] a, g' x β‰  0)
  (hfa : tendsto f (𝓝[<] a) (𝓝 0)) (hga : tendsto g (𝓝[<] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[<] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[<] a) l :=
begin
  rw eventually_iff_exists_mem at *,
  rcases hff' with ⟨s₁, hs₁, hff'⟩,
  rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩,
  rcases hg' with ⟨s₃, hs₃, hg'⟩,
  let s := s₁ ∩ sβ‚‚ ∩ s₃,
  have hs : s ∈ 𝓝[<] a := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃,
  rw mem_nhds_within_Iio_iff_exists_Ioo_subset at hs,
  rcases hs with ⟨l, hal, hl⟩,
  refine lhopital_zero_left_on_Ioo hal _ _ _ hfa hga hdiv;
  intros x hx;
  apply_assumption;
  exact (hl hx).1.1 <|> exact (hl hx).1.2 <|> exact (hl hx).2
end

/-- L'HΓ΄pital's rule for approaching a real, `has_deriv_at` version. This
  does not require anything about the situation at `a` -/
theorem lhopital_zero_nhds'
  (hff' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, has_deriv_at f (f' x) x)
  (hgg' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, has_deriv_at g (g' x) x)
  (hg' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, g' x β‰  0)
  (hfa : tendsto f (𝓝[univ \ {a}] a) (𝓝 0)) (hga : tendsto g (𝓝[univ \ {a}] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[univ \ {a}] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[univ \ {a}] a) l :=
begin
  have : univ \ {a} = Iio a βˆͺ Ioi a,
  { ext, rw [mem_diff_singleton, eq_true_intro $ mem_univ x, true_and, ne_iff_lt_or_gt], refl },
  simp only [this, nhds_within_union, tendsto_sup, eventually_sup] at *,
  exact ⟨lhopital_zero_nhds_left hff'.1 hgg'.1 hg'.1 hfa.1 hga.1 hdiv.1,
          lhopital_zero_nhds_right hff'.2 hgg'.2 hg'.2 hfa.2 hga.2 hdiv.2⟩
end

/-- **L'HΓ΄pital's rule** for approaching a real, `has_deriv_at` version -/
theorem lhopital_zero_nhds
  (hff' : βˆ€αΆ  x in 𝓝 a, has_deriv_at f (f' x) x)
  (hgg' : βˆ€αΆ  x in 𝓝 a, has_deriv_at g (g' x) x)
  (hg' : βˆ€αΆ  x in 𝓝 a, g' x β‰  0)
  (hfa : tendsto f (𝓝 a) (𝓝 0)) (hga : tendsto g (𝓝 a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, f' x / g' x) (𝓝 a) l) :
  tendsto (Ξ» x, f x / g x) (𝓝[univ \ {a}] a) l :=
begin
  apply @lhopital_zero_nhds' _ _ _ f' _ g';
  apply eventually_nhds_within_of_eventually_nhds <|> apply tendsto_nhds_within_of_tendsto_nhds;
  assumption
end

/-- L'Hôpital's rule for approaching +∞, `has_deriv_at` version -/
theorem lhopital_zero_at_top
  (hff' : βˆ€αΆ  x in at_top, has_deriv_at f (f' x) x)
  (hgg' : βˆ€αΆ  x in at_top, has_deriv_at g (g' x) x)
  (hg' : βˆ€αΆ  x in at_top, g' x β‰  0)
  (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_top l) :
  tendsto (Ξ» x, (f x) / (g x)) at_top l :=
begin
  rw eventually_iff_exists_mem at *,
  rcases hff' with ⟨s₁, hs₁, hff'⟩,
  rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩,
  rcases hg' with ⟨s₃, hs₃, hg'⟩,
  let s := s₁ ∩ sβ‚‚ ∩ s₃,
  have hs : s ∈ at_top := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃,
  rw mem_at_top_sets at hs,
  rcases hs with ⟨l, hl⟩,
  have hl' : Ioi l βŠ† s := Ξ» x hx, hl x (le_of_lt hx),
  refine lhopital_zero_at_top_on_Ioi _ _ (Ξ» x hx, hg' x $ (hl' hx).2) hftop hgtop hdiv;
  intros x hx;
  apply_assumption;
  exact (hl' hx).1.1 <|> exact (hl' hx).1.2
end

/-- L'Hôpital's rule for approaching -∞, `has_deriv_at` version -/
theorem lhopital_zero_at_bot
  (hff' : βˆ€αΆ  x in at_bot, has_deriv_at f (f' x) x)
  (hgg' : βˆ€αΆ  x in at_bot, has_deriv_at g (g' x) x)
  (hg' : βˆ€αΆ  x in at_bot, g' x β‰  0)
  (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0))
  (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_bot l) :
  tendsto (Ξ» x, (f x) / (g x)) at_bot l :=
begin
  rw eventually_iff_exists_mem at *,
  rcases hff' with ⟨s₁, hs₁, hff'⟩,
  rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩,
  rcases hg' with ⟨s₃, hs₃, hg'⟩,
  let s := s₁ ∩ sβ‚‚ ∩ s₃,
  have hs : s ∈ at_bot := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃,
  rw mem_at_bot_sets at hs,
  rcases hs with ⟨l, hl⟩,
  have hl' : Iio l βŠ† s := Ξ» x hx, hl x (le_of_lt hx),
  refine lhopital_zero_at_bot_on_Iio _ _ (Ξ» x hx, hg' x $ (hl' hx).2) hfbot hgbot hdiv;
  intros x hx;
  apply_assumption;
  exact (hl' hx).1.1 <|> exact (hl' hx).1.2
end

end has_deriv_at

namespace deriv

/-- **L'HΓ΄pital's rule** for approaching a real from the right, `deriv` version -/
theorem lhopital_zero_nhds_right
  (hdf : βˆ€αΆ  x in 𝓝[>] a, differentiable_at ℝ f x)
  (hg' : βˆ€αΆ  x in 𝓝[>] a, deriv g x β‰  0)
  (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[>] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l :=
begin
  have hdg : βˆ€αΆ  x in 𝓝[>] a, differentiable_at ℝ g x,
    from hg'.mp (eventually_of_forall $
      Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))),
  have hdf' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at f (deriv f x) x,
    from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  have hdg' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at g (deriv g x) x,
    from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  exact has_deriv_at.lhopital_zero_nhds_right hdf' hdg' hg' hfa hga hdiv
end

/-- **L'HΓ΄pital's rule** for approaching a real from the left, `deriv` version -/
theorem lhopital_zero_nhds_left
  (hdf : βˆ€αΆ  x in 𝓝[<] a, differentiable_at ℝ f x)
  (hg' : βˆ€αΆ  x in 𝓝[<] a, deriv g x β‰  0)
  (hfa : tendsto f (𝓝[<] a) (𝓝 0)) (hga : tendsto g (𝓝[<] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[<] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[<] a) l :=
begin
  have hdg : βˆ€αΆ  x in 𝓝[<] a, differentiable_at ℝ g x,
    from hg'.mp (eventually_of_forall $
      Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))),
  have hdf' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at f (deriv f x) x,
    from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  have hdg' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at g (deriv g x) x,
    from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  exact has_deriv_at.lhopital_zero_nhds_left hdf' hdg' hg' hfa hga hdiv
end

/-- **L'HΓ΄pital's rule** for approaching a real, `deriv` version. This
  does not require anything about the situation at `a` -/
theorem lhopital_zero_nhds'
  (hdf : βˆ€αΆ  x in 𝓝[univ \ {a}] a, differentiable_at ℝ f x)
  (hg' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, deriv g x β‰  0)
  (hfa : tendsto f (𝓝[univ \ {a}] a) (𝓝 0)) (hga : tendsto g (𝓝[univ \ {a}] a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[univ \ {a}] a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[univ \ {a}] a) l :=
begin
  have : univ \ {a} = Iio a βˆͺ Ioi a,
  { ext, rw [mem_diff_singleton, eq_true_intro $ mem_univ x, true_and, ne_iff_lt_or_gt], refl },
  simp only [this, nhds_within_union, tendsto_sup, eventually_sup] at *,
  exact ⟨lhopital_zero_nhds_left hdf.1 hg'.1 hfa.1 hga.1 hdiv.1,
          lhopital_zero_nhds_right hdf.2 hg'.2 hfa.2 hga.2 hdiv.2⟩,
end

/-- **L'HΓ΄pital's rule** for approaching a real, `deriv` version -/
theorem lhopital_zero_nhds
  (hdf : βˆ€αΆ  x in 𝓝 a, differentiable_at ℝ f x)
  (hg' : βˆ€αΆ  x in 𝓝 a, deriv g x β‰  0)
  (hfa : tendsto f (𝓝 a) (𝓝 0)) (hga : tendsto g (𝓝 a) (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝 a) l) :
  tendsto (Ξ» x, (f x) / (g x)) (𝓝[univ \ {a}] a) l :=
begin
  apply lhopital_zero_nhds';
  apply eventually_nhds_within_of_eventually_nhds <|> apply tendsto_nhds_within_of_tendsto_nhds;
  assumption
end

/-- **L'Hôpital's rule** for approaching +∞, `deriv` version -/
theorem lhopital_zero_at_top
  (hdf : βˆ€αΆ  (x : ℝ) in at_top, differentiable_at ℝ f x)
  (hg' : βˆ€αΆ  (x : ℝ) in at_top, deriv g x β‰  0)
  (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_top l) :
  tendsto (Ξ» x, (f x) / (g x)) at_top l :=
begin
  have hdg : βˆ€αΆ  x in at_top, differentiable_at ℝ g x,
    from hg'.mp (eventually_of_forall $
      Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))),
  have hdf' : βˆ€αΆ  x in at_top, has_deriv_at f (deriv f x) x,
    from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  have hdg' : βˆ€αΆ  x in at_top, has_deriv_at g (deriv g x) x,
    from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  exact has_deriv_at.lhopital_zero_at_top hdf' hdg' hg' hftop hgtop hdiv
end

/-- **L'Hôpital's rule** for approaching -∞, `deriv` version -/
theorem lhopital_zero_at_bot
  (hdf : βˆ€αΆ  (x : ℝ) in at_bot, differentiable_at ℝ f x)
  (hg' : βˆ€αΆ  (x : ℝ) in at_bot, deriv g x β‰  0)
  (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0))
  (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_bot l) :
  tendsto (Ξ» x, (f x) / (g x)) at_bot l :=
begin
  have hdg : βˆ€αΆ  x in at_bot, differentiable_at ℝ g x,
    from hg'.mp (eventually_of_forall $
      Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))),
  have hdf' : βˆ€αΆ  x in at_bot, has_deriv_at f (deriv f x) x,
    from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  have hdg' : βˆ€αΆ  x in at_bot, has_deriv_at g (deriv g x) x,
    from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at),
  exact has_deriv_at.lhopital_zero_at_bot hdf' hdg' hg' hfbot hgbot hdiv
end

end deriv