Predef.icl 6.54 KB
Newer Older
Camil Staps's avatar
Camil Staps committed
1
implementation module Builtin.Predef
2

3
import StdArray
Camil Staps's avatar
Camil Staps committed
4 5
import StdBool
import StdEnum
6
import StdInt
Camil Staps's avatar
Camil Staps committed
7
import StdList
Camil Staps's avatar
Camil Staps committed
8
import StdMisc
9 10 11
import StdOverloaded
import StdString

12
from Data.Func import $
13
import Data.List
14 15 16
import Data.Maybe
import Text

Camil Staps's avatar
Camil Staps committed
17 18
import Clean.Doc
import Clean.Types
19

Camil Staps's avatar
Camil Staps committed
20 21
import Cloogle.API
import Cloogle.DB
22

23 24 25
CLR :: !String -> CleanLangReportLocation
CLR sec =
	{ clr_version = "3.0"
Camil Staps's avatar
Camil Staps committed
26 27 28
	, clr_section = sec
	}

29 30 31
builtin_functions :: [FunctionEntry]
builtin_functions =
	[ { zero
32
	  & fe_loc=Builtin "if" [CLR "3.4.2"]
33
	  , fe_type=Just $ Func [Type "Bool" [], Var "a", Var "a"] (Var "a") (TypeContext [])
34 35
	  }
	, { zero
36
	  & fe_loc=Builtin "dynamic" [CLR "8.1"]
37
	  , fe_type=Just $ Func [Var "a"] (Type "Dynamic" []) (TypeContext [Instance "TC" [Var "a"]])
38 39
	  }
	]
40

41 42
builtin_classes :: [ClassEntry]
builtin_classes =
43
	[ { ce_loc=Builtin "TC" [CLR "8.1.4"]
44
	  , ce_vars=["v"]
Camil Staps's avatar
Camil Staps committed
45
	  , ce_is_meta=False
46
	  , ce_context=TypeContext []
47
	  , ce_documentation=Nothing
48 49 50 51
	  , ce_members={}
	  , ce_instances={}
	  , ce_derivations={}
	  , ce_usages={}
52 53
	  }
	]
54

55 56 57
builtin_types :: [TypeDefEntry]
builtin_types =
	[ { deft
58
	  & tde_loc=Builtin "Bool" [CLR "4.1"]
59 60 61 62 63 64
	  , tde_typedef.td_name = "Bool"
	  , tde_typedef.td_rhs  = TDRCons False
	    [ { defc & cons_name="False" }
	    , { defc & cons_name="True" }
	    ]
	  }
65 66 67 68 69
	, { deft & tde_loc=Builtin "Int"     [CLR "4.1"], tde_typedef.td_name = "Int"}
	, { deft & tde_loc=Builtin "Real"    [CLR "4.1"], tde_typedef.td_name = "Real"}
	, { deft & tde_loc=Builtin "Char"    [CLR "4.1"], tde_typedef.td_name = "Char"}
	, { deft & tde_loc=Builtin "Dynamic" [CLR "8"],   tde_typedef.td_name = "Dynamic"}
	, { deft & tde_loc=Builtin "File"    [CLR "4.7"], tde_typedef.td_name = "File"}
70
	, { deft
71
	  & tde_loc=Builtin "String" [CLR "4.7"]
72 73 74
	  , tde_typedef.td_name = "String"
	  , tde_typedef.td_rhs = TDRSynonym (Type "_#Array" [Type "Char" []]) }
	, { deft
75
	  & tde_loc=Builtin "World" [CLR "4.7"], tde_typedef.td_name = "World"
76 77 78 79 80 81 82
	  , tde_typedef.td_uniq = True
	  , tde_doc = Just
	    { TypeDoc | gDefault{|*|}
	    & description = Just "An object of this type is automatically created when the program is started, if needed. It makes efficient interfacing with the outside world possible. Its value is always `65536`."
	    }
	  }
	, { deft
83
	  & tde_loc=Builtin "->" [CLR "4.6"]
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	  , tde_typedef.td_name = "(->)"
	  , tde_typedef.td_args = [Var "a", Var "b"]
	  , tde_doc = Just
	    { TypeDoc | gDefault{|*|}
	    & description = Just "The arrow type is used to denote functions.\n\nOften, function types can be written in an uncurried fashion, e.g. `a b -> c` is the same as `a -> b -> c`."
	    , vars = ["The argument type", "The result type"]
	    }
	  }
	, { deft
	  & tde_loc=Builtin "()" []
	  , tde_typedef.td_name="_Unit"
	  , tde_doc = Just
	    { TypeDoc | gDefault{|*|}
	    & description = Just "The void / unit type."
	    }
	  , tde_typedef.td_rhs = TDRCons False [{defc & cons_name="()"}]
	  }
	:  lists
	++ arrays
	++ tuples
	]
105
where
106 107 108 109 110 111 112 113 114
	deft =
		{ tde_loc=zero
		, tde_typedef=
			{ td_name=""
			, td_uniq=False
			, td_args=[]
			, td_rhs=TDRAbstract Nothing
			}
		, tde_doc=Nothing
115 116 117
		, tde_instances={}
		, tde_derivations={}
		, tde_usages={}
118 119 120 121 122
		}
	defc =
		{ cons_name=""
		, cons_args=[]
		, cons_exi_vars=[]
123
		, cons_context=TypeContext []
124 125
		, cons_priority=Nothing
		}
126

127 128
	lists = [make_list kind spine \\ kind <- [[], ['#'], ['!'], ['|']], spine <- [[], ['!']] | kind <> ['|'] || spine <> ['!']]
	where
129 130
		make_list :: [Char] [Char] -> TypeDefEntry
		make_list k s =
131
			{ deft
132
			& tde_loc = Builtin higherorder [CLR "4.2"]
133
			, tde_typedef.td_name = toString (['_':k] ++ ['List'] ++ s)
134
			, tde_typedef.td_args = [Var "a"]
135 136 137 138 139
			, tde_doc = Just
				{ TypeDoc | gDefault{|*|}
				& description = Just $ "A" + kind + spine + " list.\n\n" + description
				, vars = ["The type of the list elements."]
				}
140
			}
141 142 143 144 145 146 147 148 149 150 151
		where
			higherorder = toString (['[':k] ++ s` ++ [']'])
				with s` = if (s == ['!'] && k == []) [' !'] s
			lista       = toString (['[':k] ++ ['a':s] ++ [']'])
			listints    = toString (['[':k] ++ ['1,1,2,3,5':s] ++ [']'])
			listany     = toString (['[':k] ++ ['\\','w':s] ++ [']'])
			kind = case k of
				[]    ->  " normal"
				['#'] -> "n unboxed"
				['!'] ->  " head strict"
				['|'] -> "n overloaded"
Camil Staps's avatar
Camil Staps committed
152
				_     -> abort "error in make_list\n"
153 154 155
			spine = case s of
				[]    -> ""
				['!'] -> " spine strict"
Camil Staps's avatar
Camil Staps committed
156
				_     -> abort "error in make_list\n"
157

Camil Staps's avatar
Camil Staps committed
158
			description = "These types of list are available:\n\n" +
159 160 161 162 163 164 165 166
				"- {{`[a]`}}, a normal list\n" +
				"- {{`[#a]`}}, an unboxed head-strict list (elements are stored directly, without pointers)\n" +
				"- {{`[!a]`}}, a head-strict list (the first element is in root normal form)\n" +
				"- {{`[a!]`}}, a spine-strict list (the last element is known)\n" +
				"- {{`[#a!]`}}, an unboxed spine-strict list\n" +
				"- {{`[!a!]`}}, a head-strict spine-strict list\n" +
				"- {{`[|a]`}}, an overloaded list (one of the types above)"

Camil Staps's avatar
Camil Staps committed
167
	arrays = [make_array kind \\ kind <- [[], ['!'], ['#'], ['32#']]]
168
	where
169 170
		make_array :: [Char] -> TypeDefEntry
		make_array k =
171
			{ deft
172
			& tde_loc = Builtin typec [CLR "4.4"]
173
			, tde_typedef.td_name = toString (['_':k] ++ ['Array'])
174
			, tde_typedef.td_args = [Var "a"]
175 176 177 178 179
			, tde_doc = Just
				{ TypeDoc | gDefault{|*|}
				& description = Just $ "An array contains a finite number of elements of the same type. Access time is constant.\n\n" + description
				, vars = ["The type of the array elements."]
				}
180
			}
181 182 183
		where
			typec = toString (['{':k]++['}'])

Camil Staps's avatar
Camil Staps committed
184
			description = "These types of array are available:\n\n" +
185
				"- `{a}`, a normal array\n" +
Camil Staps's avatar
Camil Staps committed
186 187 188
				"- `{!a}`, a strict array (the elements are in root normal form)\n" +
				"- `{#a}`, an unboxed array (elements are stored directly, without pointers)\n" +
				"- `{32#a}`, a packed array (only `{32#Int}` and `{32#Real}`, for 32-bit integer and real arrays)"
189 190 191

	tuples = [make_tuple n \\ n <- [2..32]]
	where
192 193
		make_tuple :: Int -> TypeDefEntry
		make_tuple n =
194
			{ deft
195
			& tde_loc = Builtin typec [CLR "4.3"]
196
			, tde_typedef.td_name = "_Tuple" <+ n
197
			, tde_typedef.td_args = [Var $ toString [v:repeatn (n / 26) '`'] \\ v <- cycle ['a'..'z'] & n <- [0..n-1]]
198 199 200 201 202 203
			, tde_doc = Just
				{ TypeDoc | gDefault{|*|}
				& description = Just $ article + " " + ary + "ary tuple.\n\n" +
					"Tuples allow bundling a finite number of expressions of different types into one object without defining a new data type.\n\n" +
					"Clean supports tuples of arity 2 to 32."
				}
204
			}
205 206 207 208 209 210 211 212 213 214
		where
			typec = toString ['(':repeatn (n-1) ',' ++ [')']]
			ary = case n of
				2 -> "bin"
				3 -> "tern"
				n -> n <+ "-"
			article = case n of
				11 -> "An"
				18 -> "An"
				_  -> "A"