Predef.icl 6.79 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

Camil Staps's avatar
Camil Staps committed
23 24 25 26 27 28 29 30 31
CLR :: Int String String -> CleanLangReportLocation
CLR f sec h =
	{ clr_version = v
	, clr_file    = "CleanRep." + v + "_" <+ f <+ ".htm"
	, clr_section = sec
	, clr_heading = h
	}
where v = "2.2"

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

44 45 46 47
builtin_classes :: [ClassEntry]
builtin_classes =
	[ { ce_loc=Builtin "TC" [CLR 10 "8.1.4" "_Toc311798080"]
	  , ce_vars=["v"]
Camil Staps's avatar
Camil Staps committed
48
	  , ce_is_meta=False
49
	  , ce_context=TypeContext []
50
	  , ce_documentation=Nothing
51 52 53 54
	  , ce_members={}
	  , ce_instances={}
	  , ce_derivations={}
	  , ce_usages={}
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
builtin_types :: [TypeDefEntry]
builtin_types =
	[ { deft
	  & tde_loc=Builtin "Bool" [CLR 6 "4.1" "_Toc311798017"]
	  , tde_typedef.td_name = "Bool"
	  , tde_typedef.td_rhs  = TDRCons False
	    [ { defc & cons_name="False" }
	    , { defc & cons_name="True" }
	    ]
	  }
	, { deft & tde_loc=Builtin "Int"     [CLR 6 "4.1" "_Toc311798017"], tde_typedef.td_name = "Int"}
	, { deft & tde_loc=Builtin "Real"    [CLR 6 "4.1" "_Toc311798017"], tde_typedef.td_name = "Real"}
	, { deft & tde_loc=Builtin "Char"    [CLR 6 "4.1" "_Toc311798017"], tde_typedef.td_name = "Char"}
	, { deft & tde_loc=Builtin "Dynamic" [CLR 10 "8"  "_Toc311798077"], tde_typedef.td_name = "Dynamic"}
	, { deft & tde_loc=Builtin "File"    [CLR 6 "4.7" "_Toc311798037"], tde_typedef.td_name = "File"}
	, { deft
	  & tde_loc=Builtin "String" [CLR 6 "4.7" "_Toc311798037"]
	  , tde_typedef.td_name = "String"
	  , tde_typedef.td_rhs = TDRSynonym (Type "_#Array" [Type "Char" []]) }
	, { deft
	  & tde_loc=Builtin "World" [CLR 6 "4.7" "_Toc311798037"], tde_typedef.td_name = "World"
	  , 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
	  & tde_loc=Builtin "->" [CLR 6 "4.6" "_Toc311798036"]
	  , 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
	]
108
where
109 110 111 112 113 114 115 116 117
	deft =
		{ tde_loc=zero
		, tde_typedef=
			{ td_name=""
			, td_uniq=False
			, td_args=[]
			, td_rhs=TDRAbstract Nothing
			}
		, tde_doc=Nothing
118 119 120
		, tde_instances={}
		, tde_derivations={}
		, tde_usages={}
121 122 123 124 125
		}
	defc =
		{ cons_name=""
		, cons_args=[]
		, cons_exi_vars=[]
126
		, cons_context=TypeContext []
127 128
		, cons_priority=Nothing
		}
129

130 131
	lists = [make_list kind spine \\ kind <- [[], ['#'], ['!'], ['|']], spine <- [[], ['!']] | kind <> ['|'] || spine <> ['!']]
	where
132 133
		make_list :: [Char] [Char] -> TypeDefEntry
		make_list k s =
134
			{ deft
135 136
			& tde_loc = Builtin higherorder [CLR 6 "4.2" "_Toc311798019"]
			, tde_typedef.td_name = toString (['_':k] ++ ['List'] ++ s)
137
			, tde_typedef.td_args = [Var "a"]
138 139 140 141 142
			, tde_doc = Just
				{ TypeDoc | gDefault{|*|}
				& description = Just $ "A" + kind + spine + " list.\n\n" + description
				, vars = ["The type of the list elements."]
				}
143
			}
144 145 146 147 148 149 150 151 152 153 154
		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
155
				_     -> abort "error in make_list\n"
156 157 158
			spine = case s of
				[]    -> ""
				['!'] -> " spine strict"
Camil Staps's avatar
Camil Staps committed
159
				_     -> abort "error in make_list\n"
160

Camil Staps's avatar
Camil Staps committed
161
			description = "These types of list are available:\n\n" +
162 163 164 165 166 167 168 169
				"- {{`[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)"

170 171
	arrays = [make_array kind \\ kind <- [[], ['!'], ['#']]]
	where
172 173
		make_array :: [Char] -> TypeDefEntry
		make_array k =
174
			{ deft
175 176
			& tde_loc = Builtin typec [CLR 6 "4.4" "_Toc311798029"]
			, tde_typedef.td_name = toString (['_':k] ++ ['Array'])
177
			, tde_typedef.td_args = [Var "a"]
178 179 180 181 182
			, 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."]
				}
183
			}
184 185 186
		where
			typec = toString (['{':k]++['}'])

Camil Staps's avatar
Camil Staps committed
187
			description = "These types of array are available:\n\n" +
188 189 190 191 192 193
				"- `{a}`, a normal array\n" +
				"- `{#a}`, an unboxed strict array (elements are stored directly, without pointers)\n" +
				"- `{!a}`, a strict array (the elements are in root normal form)"

	tuples = [make_tuple n \\ n <- [2..32]]
	where
194 195
		make_tuple :: Int -> TypeDefEntry
		make_tuple n =
196
			{ deft
197 198
			& tde_loc = Builtin typec [CLR 6 "4.3" "_Toc311798026"]
			, tde_typedef.td_name = "_Tuple" <+ n
199
			, tde_typedef.td_args = [Var $ toString [v:repeatn (n / 26) '`'] \\ v <- cycle ['a'..'z'] & n <- [0..n-1]]
200 201 202 203 204 205
			, 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."
				}
206
			}
207 208 209 210 211 212 213 214 215 216
		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"