on master help sign in
Docs for new record syntax
commit0afd6f259408544550c271eb29121a66cc576f3c authorDavid Raymond Christiansen <david@davidchristiansen.dk> dateTue 5 May 2015, 22:28 (12 years ago) parent37ebed3
4 files changed+59−28
docs/reference/documenting.rst+5−5 docs/reference/ffi.rst+6−5 docs/tutorial/typesfuns.rst+46−17 src/Idris/Elab/Record.hs+2−1
docs/reference/documenting.rst+5−5
@@ -127,9 +127,9 @@ italics correctly). A comprehensive set of examples is given below.
127127 ||| @ rest the remaining work
128128 Later : (rest : Partial a) -> Partial a
129129
130− ||| We can document records just like normal data
131− record Yummy : Type where
132−
130+ ||| We can document records, including their fields and constructors
131+ record Yummy where
133132 ||| Make a yummy
134− ||| @ food what to eat
135− MkYummy : (food : String) -> Yummy
133+ constructor MkYummy
134+ ||| What to eat
135+ food : String
docs/reference/ffi.rst+6−5
@@ -70,21 +70,22 @@ call:
7070
7171.. code-block:: idris
7272
73− record FFI : Type where
74− MkFFI : (ffi_types : Type -> Type) ->
75− (ffi_fn : Type) -> FFI
73+ record FFI where
74+ constructor MkFFI
75+ ffi_types : Type -> Type
76+ ffi_fn : Type
7677
7778For C, this is:
7879
7980.. code-block:: idris
8081
81− -- Supported C integer types
82+ ||| Supported C integer types
8283 data C_IntTypes : Type -> Type where
8384 C_IntChar : C_IntTypes Char
8485 C_IntNative : C_IntTypes Int
8586 ... -- more integer types
8687
87− -- Supported C foreign types
88+ ||| Supported C foreign types
8889 data C_Types : Type -> Type where
8990 C_Str : C_Types String
9091 C_Float : C_Types Float
docs/tutorial/typesfuns.rst+46−17
@@ -979,18 +979,19 @@ example, we can represent a person's name and age in a record:
979979
980980.. code-block:: idris
981981
982− record Person : Type where
983− MkPerson : (name : String) ->
984− (age : Int) -> Person
982+ record Person where
983+ constructor MkPerson
984+ name : String
985+ age : Int
985986
986987 fred : Person
987988 fred = MkPerson "Fred" 30
988989
989−Record declarations are like ``data`` declarations, except that they
990−are introduced by the ``record`` keyword, and can only have one
991−constructor. The names of the binders in the constructor type
992−(``name`` and ``age``) here are the field names, which we can use to
993−access the field values:
990+Records can have *parameters*, which are listed between the record
991+name and the ``where`` keyword, and *fields*, which are in an indented
992+block following the `where` keyword (here, ``name`` and ``age``). The
993+constructor name is provided after the ``constructor`` keyword. The
994+field names can be used to access the field values:
994995
995996::
996997
@@ -1002,7 +1003,8 @@ access the field values:
10021003 name : Person -> String
10031004
10041005We can also use the field names to update a record (or, more
1005−precisely, produce a new record with the given fields updated).
1006+precisely, produce a copy of the record with the given fields
1007+updated):
10061008
10071009.. code-block:: bash
10081010
@@ -1016,15 +1018,14 @@ updates the given fields in a record.
10161018
10171019Records, and fields within records, can have dependent types. Updates
10181020are allowed to change the type of a field, provided that the result is
1019−well-typed, and the result does not affect the type of the record as a
1020−whole. For example:
1021+well-typed.
10211022
10221023.. code-block:: idris
10231024
1024− record Class : Type where
1025− ClassInfo : (students : Vect n Person) ->
1026− (className : String) ->
1027− Class
1025+ record Class where
1026+ constructor ClassInfo
1027+ students : Vect n Person
1028+ className : String
10281029
10291030It is safe to update the ``students`` field to a vector of a different
10301031length because it will not affect the type of the record:
@@ -1037,8 +1038,7 @@ length because it will not affect the type of the record:
10371038::
10381039
10391040 *record> addStudent fred (ClassInfo [] "CS")
1040− ClassInfo (prelude.vect.:: (MkPerson "Fred" 30) (prelude.vect.Nil)) "CS"
1041− : Class
1041+ ClassInfo [MkPerson "Fred" 30] "CS" : Class
10421042
10431043Nested record update
10441044--------------------
@@ -1060,3 +1060,32 @@ can also be accessed with the following syntax:
10601060.. code-block:: idris
10611061
10621062 record { a->b->c } x
1063+
1064+Parameters and Fields
1065+---------------------
1066+
1067+Records can have *parameters*, which are not subject to field
1068+updates. The parameters appear as arguments to the resulting type, and
1069+are written following the record type name. For example, a pair type
1070+could be defined as follows:
1071+
1072+.. code-block:: idris
1073+
1074+ record Prod a b where
1075+ constructor Times
1076+ fst : a
1077+ snd : b
1078+
1079+The parameters to a record type need not be types. For example, we can
1080+restrict the size of classes using a ``Nat`` parameter to the
1081+``Class`` record:
1082+
1083+.. code-block:: idris
1084+
1085+ record SizedClass (size : Nat) where
1086+ constructor SizedClassInfo
1087+ students : Vect size Person
1088+ className : String
1089+
1090+Note that it is no longer possible to write ``addStudent`` for this
1091+type, as that would change the size of the class.
src/Idris/Elab/Record.hs+2−1
@@ -32,8 +32,9 @@ import Data.Maybe
3232import Data.List
3333import Control.Monad
3434
35+-- | Elaborate a record declaration
3536elabRecord :: ElabInfo ->
36− (Docstring (Either Err PTerm)) ->
37+ (Docstring (Either Err PTerm)) -> -- ^ The documentation for the whole declaration
3738 SyntaxInfo ->
3839 FC ->
3940 DataOpts ->