In mathematics, more precisely in formal language theory, the profinite words are a generalization of the notion of finite words into a complete topological space. This notion allows the use of topology to study languages and finite semigroups. For example, profinite words are used to give an alternative characterization of the algebraic notion of a variety of finite semigroups.
 Let A an alphabet. The set of profinite words over A consists of the completion of a metric space whose domain is the set 
  
    
      
        
          A
          
            ∗
          
        
      
    
    {\displaystyle A^{*}}
  
 of words over A. The distance used to define the metric is given using a notion of separation of words. Those notions are now defined.
 Let M and N be monoids, and let p and q be elements of the monoid M. Let φ be a morphism of monoids from M to N. It is said that the morphism φ separates p and q if 
  
    
      
        ϕ
        (
        p
        )
        ≠
        ϕ
        (
        q
        )
      
    
    {\displaystyle \phi (p)\neq \phi (q)}
  
. For example, the morphism 
  
    
      
        ϕ
        :
        
          A
          
            ∗
          
        
        →
        
          Z
        
        
          /
        
        2
        
          Z
        
        ,
        w
        ↦
        
          |
        
        w
        
          |
        
        (
        mod
        ⁡
        2
        )
      
    
    {\displaystyle \phi :A^{*}\to \mathbb {Z} /2\mathbb {Z} ,w\mapsto |w|(\operatorname {mod} 2)}
  
 sending a word to the parity of its length separates the words ababa and abaa. Indeed 
  
    
      
        ϕ
        (
        a
        b
        a
        b
        a
        )
        =
        1
        ≠
        0
        =
        ϕ
        (
        a
        b
        a
        a
        )
      
    
    {\displaystyle \phi (ababa)=1\neq 0=\phi (abaa)}
  
. 
 It is said that N separates p and q if there exists a morphism of monoids φ from M to N that separates p and q. Using the previous example, 
  
    
      
        
          Z
        
        
          /
        
        2
        
          Z
        
      
    
    {\displaystyle \mathbb {Z} /2\mathbb {Z} }
  
 separates ababa and abaa. More generally, 
  
    
      
        
          Z
        
        
          /
        
        n
        
          Z
        
      
    
    {\displaystyle \mathbb {Z} /n\mathbb {Z} }
  
 separates any words whose size are not congruent modulo n. In general, any two distinct words can be separated, using the monoid whose elements are the factors of p plus a fresh element 0. The morphism sends prefixes of p to themselves and everything else to 0.
 The distance between two distinct words p and q is defined as the inverse of the size of the smallest monoid N separating p and q. Thus, the distance of ababa and abaa is 
  
    
      
        
          
            1
            2
          
        
      
    
    {\displaystyle {\frac {1}{2}}}
  
. The distance of p to itself is defined as 0.
 This distance d is an ultrametric, that is, 
  
    
      
        d
        (
        x
        ,
        z
        )
        ≤
        max
        
          {
          
            d
            (
            x
            ,
            y
            )
            ,
            d
            (
            y
            ,
            z
            )
          
          }
        
      
    
    {\displaystyle d(x,z)\leq \max \left\{d(x,y),d(y,z)\right\}}
  
. Furthermore it satisfies 
  
    
      
        d
        (
        u
        w
        ,
        v
        w
        )
        ≤
        d
        (
        u
        ,
        v
        )
      
    
    {\displaystyle d(uw,vw)\leq d(u,v)}
  
 and 
  
    
      
        d
        (
        w
        u
        ,
        w
        v
        )
        ≤
        d
        (
        u
        ,
        v
        )
      
    
    {\displaystyle d(wu,wv)\leq d(u,v)}
  
.
Since any word p can be separated from any other word using a monoid with |p|+1 elements, where |p| is the length of p, it follows that the distance between p and any other word is at least 
  
    
      
        
          
            1
            
              
                |
              
              p
              
                |
              
            
          
        
      
    
    {\displaystyle {\frac {1}{|p|}}}
  
. Thus the topology defined by this metric is discrete.
 The profinite completion of 
  
    
      
        
          A
          
            ∗
          
        
      
    
    {\displaystyle A^{*}}
  
, denoted 
  
    
      
        
          
            
              
                A
                
                  ∗
                
              
              ^
            
          
        
      
    
    {\displaystyle {\widehat {A^{*}}}}
  
, is the completion of the set of finite words under the distance defined above. The completion preserves the monoid structure.
 The topology on 
  
    
      
        
          
            
              
                A
                
                  ∗
                
              
              ^
            
          
        
      
    
    {\displaystyle {\widehat {A^{*}}}}
  
 is compact.
 Any monoid morphism 
  
    
      
        ϕ
        :
        
          A
          
            ∗
          
        
        →
        M
      
    
    {\displaystyle \phi :A^{*}\to M}
  
, with M finite can be extended uniquely into a monoid morphism 
  
    
      
        
          
            
              ϕ
              ^
            
          
        
        :
        
          
            
              
                A
                
                  ∗
                
              
              ^
            
          
        
        →
        M
      
    
    {\displaystyle {\widehat {\phi }}:{\widehat {A^{*}}}\to M}
  
, and this morphism is uniformly continuous (using any metric on 
  
    
      
        M
      
    
    {\displaystyle M}
  
 compatible with the discrete topology).  Furthermore, 
  
    
      
        
          
            
              
                A
                
                  ∗
                
              
              ^
            
          
        
      
    
    {\displaystyle {\widehat {A^{*}}}}
  
 is the least topological space with this property.
 A profinite word is an element of 
  
    
      
        
          
            
              
                A
                
                  ∗
                
              
              ^
            
          
        
      
    
    {\displaystyle {\widehat {A^{*}}}}
  
. And a profinite language is a set of profinite words. Every finite word is a profinite word. A few examples of profinite words that are not finite are now given.
 For m any word, let 
  
    
      
        
          m
          
            ω
          
        
      
    
    {\displaystyle m^{\omega }}
  
 denote 
  
    
      
        
          lim
          
            i
            →
            ∞
          
        
        
          m
          
            i
            !
          
        
      
    
    {\displaystyle \lim _{i\to \infty }m^{i!}}
  
, which exists because 
  
    
      
        
          m
          
            i
            !
          
        
      
    
    {\displaystyle m^{i!}}
  
 is a Cauchy sequence. Intuitively, to separate 
  
    
      
        
          m
          
            i
            !
          
        
      
    
    {\displaystyle m^{i!}}
  
 and 
  
    
      
        
          m
          
            
              i
              ′
            
            !
          
        
      
    
    {\displaystyle m^{i'!}}
  
, a monoid should count at least up to 
  
    
      
        min
        (
        i
        ,
        
          i
          ′
        
        )
      
    
    {\displaystyle \min(i,i')}
  
, and hence requires at least 
  
    
      
        min
        (
        i
        ,
        
          i
          ′
        
        )
      
    
    {\displaystyle \min(i,i')}
  
 elements. Since 
  
    
      
        
          m
          
            i
            !
          
        
      
    
    {\displaystyle m^{i!}}
  
 is a Cauchy sequence, 
  
    
      
        
          m
          
            ω
          
        
      
    
    {\displaystyle m^{\omega }}
  
 is indeed a profinite word.
 Furthermore, the word 
  
    
      
        
          m
          
            ω
          
        
      
    
    {\displaystyle m^{\omega }}
  
 is idempotent. This is due to the fact that, for any morphism 
  
    
      
        ϕ
        :
        
          A
          
            ∗
          
        
        →
        N
      
    
    {\displaystyle \phi :A^{*}\to N}
  
 with N finite, 
  
    
      
        ϕ
        (
        
          m
          
            i
            !
          
        
        )
        =
        ϕ
        (
        m
        
          )
          
            i
            !
          
        
      
    
    {\displaystyle \phi (m^{i!})=\phi (m)^{i!}}
  
. Since N is finite, for i great enough, 
  
    
      
        ϕ
        (
        m
        
          )
          
            i
            !
          
        
      
    
    {\displaystyle \phi (m)^{i!}}
  
 is idempotent, and the sequence is constant. 
 Similarly, 
  
    
      
        
          m
          
            ω
            +
            1
          
        
      
    
    {\displaystyle m^{\omega +1}}
  
 and 
  
    
      
        
          m
          
            ω
            −
            1
          
        
      
    
    {\displaystyle m^{\omega -1}}
  
 are defined as 
  
    
      
        
          lim
          
            n
            →
            ∞
          
        
        
          m
          
            n
            !
            +
            1
          
        
      
    
    {\displaystyle \lim _{n\to \infty }m^{n!+1}}
  
 and 
  
    
      
        
          lim
          
            n
            →
            ∞
          
        
        
          m
          
            n
            !
            −
            1
          
        
      
    
    {\displaystyle \lim _{n\to \infty }m^{n!-1}}
  
 respectively.
 The notion of profinite languages allows one to relate notions of semigroup theory to notions of topology. More precisely, given  P a profinite language, the following statements are equivalent:
 Similar statements also hold for languages P of finite words.  The following conditions are equivalent.
 Those characterisations are due to the more general fact that, taking the closure of a language of finite words, and restricting a profinite language to finite words are inverse operations, when they are applied to recognisable languages.
 Pin, Jean-Éric (2016-11-30). Mathematical Foundations of Automata Theory (PDF). pp. 130–139.
 Almeida, Jorge (1994). Finite semigroups and universal algebra. River Edge, NJ: World Scientific Publishing Co. Inc. ISBN 981-02-1895-8.