reserve a,b,p,r,r1,r2,s,s1,s2,x0,x for Real;
reserve f,g for PartFunc of REAL,REAL;
reserve X,Y for set;

theorem Th21:
  for x0 be Real, f st x0 in dom f holds f
  is_upper_semicontinuous_in x0 & f is_lower_semicontinuous_in x0 iff f
  is_continuous_in x0
proof
  let x0 be Real, f such that
A1: x0 in dom f;
A2: f is_continuous_in x0 implies f is_upper_semicontinuous_in x0 & f
  is_lower_semicontinuous_in x0
  proof
    assume
A3: f is_continuous_in x0;
A4: for r st 0<r ex s st 0<s & for x st x in dom f & |.x-x0.|<s holds f.
    x0-f.x<r
    proof
      let r;
      assume 0<r;
      then consider s be Real such that
A5:   0<s and
A6:   for x be Real st x in dom f & |.x-x0.|<s holds |.f.x-
      f.x0 .|<r by A3,FCONT_1:3;
      take s;
      for x st x in dom f & |.x-x0.|<s holds f.x0-f.x<r
      proof
        let x;
        assume x in dom f & |.x-x0.|<s;
        then
A7:     |.f.x-f.x0.|<r by A6;
        f.x-f.x0>=-|.f.x-f.x0.| by ABSVALUE:4;
        then -(f.x-f.x0)<=|.f.x-f.x0.| by XREAL_1:26;
        hence thesis by A7,XXREAL_0:2;
      end;
      hence thesis by A5;
    end;
    for r st 0<r ex s st 0<s & for x st x in dom f & |.x-x0.|<s holds f.
    x-f.x0<r
    proof
      let r;
      assume 0<r;
      then consider s be Real such that
A8:   0<s and
A9:   for x be Real st x in dom f & |.x-x0.|<s holds |.f.x-
      f.x0 .|<r by A3,FCONT_1:3;
      take s;
      for x st x in dom f & |.x-x0.|<s holds f.x-f.x0<r
      proof
        let x;
        assume x in dom f & |.x-x0.|<s;
        then
A10:    |.f.x-f.x0.|<r by A9;
        f.x-f.x0<=|.f.x-f.x0.| by ABSVALUE:4;
        hence thesis by A10,XXREAL_0:2;
      end;
      hence thesis by A8;
    end;
    hence thesis by A1,A4;
  end;
  f is_upper_semicontinuous_in x0 & f is_lower_semicontinuous_in x0
  implies f is_continuous_in x0
  proof
    assume that
A11: f is_upper_semicontinuous_in x0 and
A12: f is_lower_semicontinuous_in x0;
    for r be Real st 0<r ex s be Real st 0<s & for x be Real
 st x in dom f & |.x-x0.|<s holds |.f.x-f.x0.|<r
    proof
      let r be Real such that
A13:  0<r;
      consider s1 such that
A14:  0<s1 and
A15:  for x st x in dom f & |.x-x0.|<s1 holds f.x-f.x0<r by A12,A13;
      consider s2 such that
A16:  0<s2 and
A17:  for x st x in dom f & |.x-x0.|<s2 holds f.x0-f.x<r by A11,A13;
      set s = min(s1,s2);
A18:  for x be Real st x in dom f & |.x-x0.|<s holds |.f.x-f.x0.|<r
      proof
        let x be Real such that
A19:    x in dom f and
A20:    |.x-x0.|<s;
        s <= s2 by XXREAL_0:17;
        then |.x-x0.|<s2 by A20,XXREAL_0:2;
        then
A21:    f.x0-f.x<r by A17,A19;
        s <= s1 by XXREAL_0:17;
        then |.x-x0.|<s1 by A20,XXREAL_0:2;
        then
A22:    f.x-f.x0<r by A15,A19;
A23:    |.f.x-f.x0.|<>r
        proof
          assume
A24:      |.f.x-f.x0.|=r;
          now
            per cases;
            suppose
              f.x-f.x0>=0;
              hence contradiction by A22,A24,ABSVALUE:def 1;
            end;
            suppose
              not f.x-f.x0>=0;
              then |.f.x-f.x0.|=-(f.x-f.x0) by ABSVALUE:def 1;
              hence contradiction by A21,A24;
            end;
          end;
          hence thesis;
        end;
        -(f.x0-f.x)>-r by A21,XREAL_1:24;
        then |.f.x-f.x0.|<=r by A22,ABSVALUE:5;
        hence thesis by A23,XXREAL_0:1;
      end;
      take s;
      s > 0
      proof
        now
          per cases;
          suppose
            s1<=s2;
            hence thesis by A14,XXREAL_0:def 9;
          end;
          suppose
            not s1<=s2;
            hence thesis by A16,XXREAL_0:def 9;
          end;
        end;
        hence thesis;
      end;
      hence thesis by A18;
    end;
    hence thesis by FCONT_1:3;
  end;
  hence thesis by A2;
end;
