<commit_before># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2004-2008 <NAME> <<EMAIL>>
# Copyright 2006-2008 <NAME> <<EMAIL>>
#
# This file is free software, which comes along with NIT.  This software is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
# is kept unaltered, and a notification of the changes is added.
# You  are  allowed  to  redistribute it and sell it, alone or is a part of
# another product.

# Most minimal classes and methods.
# This module is the root of the standard module hierarchy.
module kernel

import end # Mark this module is a top level one. (must be only one)

###############################################################################
# System Classes                                                              #
###############################################################################

# The root of the class hierarchy.
# Each class implicitly specialize Object.
#
# Currently, Object is also used to collect all top-level methods.
interface Object
	# Type of this instance, automatically specialized in every class
	#
	# A common use case of the virtual type `SELF` is to type an attribute and
	# store another instance of the same type as `self`. It can also be used as as
	# return type to a method producing a copy of `self` or returning an instance
	# expected to be the exact same type as self.
	#
	# This virtual type must be used with caution as it can hinder specialization.
	# In fact, it imposes strict restrictions on all sub-classes and their usage.
	# For example, using `SELF` as a return type of a method `foo`
	# forces all subclasses to ensure that `foo` returns the correct and updated
	# type.
	# A dangerous usage take the form of a method typed by `SELF` which creates
	# and returns a new instance.
	# If not correctly specialized, this method would break when invoked on a
	# sub-class.
	#
	# A general rule for safe usage of `SELF` is to ensure that inputs typed
	# `SELF` are stored in attributes typed `SELF` and returned by methods typed
	# `SELF`, pretty much the same things as you would do with parameter types.
	type SELF: Object

	# The unique object identifier in the class.
	# Unless specific code, you should not use this method.
	# The identifier is used internally to provide a hash value.
	fun object_id: Int is intern

	# Return true if `self` and `other` have the same dynamic type.
	# Unless specific code, you should not use this method.
	fun is_same_type(other: Object): Bool is intern

	# Return true if `self` and `other` are the same instance.
	# Unless specific code, you should use `==` instead.
	fun is_same_instance(other: nullable Object): Bool is intern

	# Have `self` and `other` the same value?
	#
	# The exact meaning of "same value" is let to the subclasses.
	# Implicitly, the default implementation, is `is_same_instance`
	fun ==(other: nullable Object): Bool do return self.is_same_instance(other)

	# Have `self` and `other` different values?
	#
	# != is equivalent with "not ==".
	fun !=(other: nullable Object): Bool do return not (self == other)

	# Display self on stdout (debug only).
	# This method MUST not be used by programs, it is here for debugging
	# only and can be removed without any notice
	fun output
	do
		'<'.output
		object_id.output
		'>'.output
	end

	# Display class name on stdout (debug only).
	# This method MUST not be used by programs, it is here for debugging
	# only and can be removed without any notice
	fun output_class_name is intern	

	# The hash code of the object.
	# Assuming that a == b -> a.hash == b.hash
	#
	# Without redefinition, it is based on the `object_id` of the instance.
	fun hash: Int do return object_id / 8
end

# The main class of the program.
# `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
# `sys` is used to invoke methods on the program on the system.
class Sys
	# Instructions outside classes implicitly redefine this method.
	fun main do end

	# The entry point for the execution of the whole program.
	# Its job is to call `main` but some modules may want to refine it
	# and inject specific work before or after the main part.
	fun run do main

	# Number of the last error
	fun errno: Int is extern "sys_errno"
end

# Quit the program with a specific return code
fun exit(exit_value: Int) is intern

# Return the global sys object, the only instance of the `Sys` class.
fun sys: Sys is intern


###############################################################################
# Abstract Classes                                                            #
###############################################################################

# The ancestor of class where objects are in a total order.
# In order to work, the method '<' has to be redefined.
interface Comparable
	# What `self` can be compared to?
	type OTHER: Comparable

	# Is `self` lesser than `other`?
	fun <(other: OTHER): Bool is abstract 

	# not `other` < `self`
	# Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
	fun <=(other: OTHER): Bool do return not other < self

	# not `self` < `other`
	# Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
	fun >=(other: OTHER): Bool do return not self < other

	# `other` < `self`
	fun >(other: OTHER): Bool do return other < self

	# -1 if <, +1 if > and 0 otherwise
	# Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
	fun <=>(other: OTHER): Int
	do
		if self < other then
			return -1
		else if other < self then
			return 1
		else
			return 0
		end
	end

	# c <= self <= d
	fun is_between(c: OTHER, d: OTHER): Bool
	do
		return c <= self and self <= d
	end

	# The maximum between `self` and `other` (prefers `self` if equals).
	fun max(other: OTHER): OTHER
	do
		if self < other then
			return other
		else
			return self
		end
	end

	# The minimum between `self` and `c` (prefer `self` if equals)
	fun min(c: OTHER): OTHER
	do
		if c < self then
			return c
		else
			return self
		end
	end
end

# Discrete total orders.
interface Discrete
	super Comparable

	redef type OTHER: Discrete

	# The next element.
	fun successor(i: Int): OTHER is abstract

	# The previous element.
	fun predecessor(i: Int): OTHER is abstract

	# The distance between self and d.
	#
	#     assert 10.distance(15)	     ==  5
	#     assert 'Z'.distance('A')	     ==  25
	fun distance(d: OTHER): Int
	do
		var cursor: OTHER
		var stop: OTHER
		if self < d then
			cursor = self
			stop = d
		else if self > d then
			cursor = d
			stop = self
		else
			return 0
		end

		var nb = 0
		while cursor < stop do
			cursor = cursor.successor(1)
			nb += 1
		end
		return nb
	end
end

# Something that can be cloned
#
# This interface introduces the `clone` method used to duplicate an instance
# Its specific semantic is let to the subclasses.
interface Cloneable
	# Duplicate `self`
	#
	# The specific semantic of this method is let to the subclasses;
	# Especially, if (and how) attributes are cloned (depth vs. shallow).
	#
	# As a rule of thumb, the principle of least astonishment should
	# be used to guide the semantic.
	#
	# Note that as the returned clone depends on the semantic,
	# the `==` method, if redefined, should ensure the equality
	# between an object and its clone.
	fun clone: SELF is abstract
end

# A numeric value supporting mathematical operations
interface Numeric
	super Comparable
<commit_msg>lib/standard: fix a repeated error in the kernel module doc<commit_after># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2004-2008 <NAME> <<EMAIL>>
# Copyright 2006-2008 <NAME> <<EMAIL>>
#
# This file is free software, which comes along with NIT.  This software is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
# is kept unaltered, and a notification of the changes is added.
# You  are  allowed  to  redistribute it and sell it, alone or is a part of
# another product.

# Most minimal classes and methods.
# This module is the root of the standard module hierarchy.
module kernel

import end # Mark this module is a top level one. (must be only one)

###############################################################################
# System Classes                                                              #
###############################################################################

# The root of the class hierarchy.
# Each class implicitly specialize Object.
#
# Currently, Object is also used to collect all top-level methods.
interface Object
	# Type of this instance, automatically specialized in every class
	#
	# A common use case of the virtual type `SELF` is to type an attribute and
	# store another instance of the same type as `self`. It can also be used as as
	# return type to a method producing a copy of `self` or returning an instance
	# expected to be the exact same type as self.
	#
	# This virtual type must be used with caution as it can hinder specialization.
	# In fact, it imposes strict restrictions on all sub-classes and their usage.
	# For example, using `SELF` as a return type of a method `foo`
	# forces all subclasses to ensure that `foo` returns the correct and updated
	# type.
	# A dangerous usage take the form of a method typed by `SELF` which creates
	# and returns a new instance.
	# If not correctly specialized, this method would break when invoked on a
	# sub-class.
	#
	# A general rule for safe usage of `SELF` is to ensure that inputs typed
	# `SELF` are stored in attributes typed `SELF` and returned by methods typed
	# `SELF`, pretty much the same things as you would do with parameter types.
	type SELF: Object

	# The unique object identifier in the class.
	# Unless specific code, you should not use this method.
	# The identifier is used internally to provide a hash value.
	fun object_id: Int is intern

	# Return true if `self` and `other` have the same dynamic type.
	# Unless specific code, you should not use this method.
	fun is_same_type(other: Object): Bool is intern

	# Return true if `self` and `other` are the same instance.
	# Unless specific code, you should use `==` instead.
	fun is_same_instance(other: nullable Object): Bool is intern

	# Have `self` and `other` the same value?
	#
	# The exact meaning of "same value" is left to the subclasses.
	# Implicitly, the default implementation, is `is_same_instance`
	fun ==(other: nullable Object): Bool do return self.is_same_instance(other)

	# Have `self` and `other` different values?
	#
	# != is equivalent with "not ==".
	fun !=(other: nullable Object): Bool do return not (self == other)

	# Display self on stdout (debug only).
	# This method MUST not be used by programs, it is here for debugging
	# only and can be removed without any notice
	fun output
	do
		'<'.output
		object_id.output
		'>'.output
	end

	# Display class name on stdout (debug only).
	# This method MUST not be used by programs, it is here for debugging
	# only and can be removed without any notice
	fun output_class_name is intern

	# The hash code of the object.
	# Assuming that a == b -> a.hash == b.hash
	#
	# Without redefinition, it is based on the `object_id` of the instance.
	fun hash: Int do return object_id / 8
end

# The main class of the program.
# `Sys` is a singleton class, its only instance is `sys` defined in `Object`.
# `sys` is used to invoke methods on the program on the system.
class Sys
	# Instructions outside classes implicitly redefine this method.
	fun main do end

	# The entry point for the execution of the whole program.
	# Its job is to call `main` but some modules may want to refine it
	# and inject specific work before or after the main part.
	fun run do main

	# Number of the last error
	fun errno: Int is extern "sys_errno"
end

# Quit the program with a specific return code
fun exit(exit_value: Int) is intern

# Return the global sys object, the only instance of the `Sys` class.
fun sys: Sys is intern


###############################################################################
# Abstract Classes                                                            #
###############################################################################

# The ancestor of class where objects are in a total order.
# In order to work, the method '<' has to be redefined.
interface Comparable
	# What `self` can be compared to?
	type OTHER: Comparable

	# Is `self` lesser than `other`?
	fun <(other: OTHER): Bool is abstract 

	# not `other` < `self`
	# Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
	fun <=(other: OTHER): Bool do return not other < self

	# not `self` < `other`
	# Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
	fun >=(other: OTHER): Bool do return not self < other

	# `other` < `self`
	fun >(other: OTHER): Bool do return other < self

	# -1 if <, +1 if > and 0 otherwise
	# Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
	fun <=>(other: OTHER): Int
	do
		if self < other then
			return -1
		else if other < self then
			return 1
		else
			return 0
		end
	end

	# c <= self <= d
	fun is_between(c: OTHER, d: OTHER): Bool
	do
		return c <= self and self <= d
	end

	# The maximum between `self` and `other` (prefers `self` if equals).
	fun max(other: OTHER): OTHER
	do
		if self < other then
			return other
		else
			return self
		end
	end

	# The minimum between `self` and `c` (prefer `self` if equals)
	fun min(c: OTHER): OTHER
	do
		if c < self then
			return c
		else
			return self
		end
	end
end

# Discrete total orders.
interface Discrete
	super Comparable

	redef type OTHER: Discrete

	# The next element.
	fun successor(i: Int): OTHER is abstract

	# The previous element.
	fun predecessor(i: Int): OTHER is abstract

	# The distance between self and d.
	#
	#     assert 10.distance(15)	     ==  5
	#     assert 'Z'.distance('A')	     ==  25
	fun distance(d: OTHER): Int
	do
		var cursor: OTHER
		var stop: OTHER
		if self < d then
			cursor = self
			stop = d
		else if self > d then
			cursor = d
			stop = self
		else
			return 0
		end

		var nb = 0
		while cursor < stop do
			cursor = cursor.successor(1)
			nb += 1
		end
		return nb
	end
end

# Something that can be cloned
#
# This interface introduces the `clone` method used to duplicate an instance
# Its specific semantic is left to the subclasses.
interface Cloneable
	# Duplicate `self`
	#
	# The specific semantic of this method is left to the subclasses;
	# Especially, if (and how) attributes are cloned (depth vs. shallow).
	#
	# As a rule of thumb, the principle of least astonishment should
	# be used to guide the semantic.
	#
	# Note that as the returned clone depends on the semantic,
	# the `==` method, if redefined, should ensure the equality
	# between an object and its clone.
	fun clone: SELF is abstract
end

# A numeric value supporting mathematical operations
interface Numeric
	super Comparable

{'fraction_non_alphanumeric': 0.1150861502038669, 'fraction_numerical': 0.0046034460081546755, 'mean_word_length': 3.395086705202312, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '320396', 'n_tokens_mistral': 4568, 'n_tokens_neox': 4277, 'n_words': 2195}