30 | %language ElabReflection
34 | ---------------------------------
35 | --- SPECIALISATION ERROR TYPE ---
36 | ---------------------------------
38 | ||| Specialisation error
39 | export
41 | ||| Failed to extract polymorphic type name from task
43 | ||| Unused variable
45 | ||| Partial specification
47 | ||| Internal error
49 | ||| Lambda has unnamed arguments
51 | ||| Polymorphic type has unnamed arguments
53 | ||| Failed to get TypeInfo
54 | |||
55 | ||| Can occur either due to trying to specialise a non-type invocation
56 | ||| or due to not having the necessary TypeInfo in the NamesInfoInTypes instance
59 | %hint
60 | export
64 | -------------------------------
65 | --- SPECIALISAION TASK TYPE ---
66 | -------------------------------
68 | ||| Specialisation task
71 | ||| Full unification task
75 | ||| Unification task type
78 | ||| Namespace in which specialiseData was called
80 | ||| Name of specialised type
82 | ||| Invocation of polymorphic type extracted from unification task
84 | ||| Polymorphic type's TypeInfo
86 | ||| Proof that all the constructors of the polymorphic type are named
99 | ]
101 | ||| Unification results for the whole type
105 | ------------------------
106 | --- HELPER FUNCTIONS ---
107 | ------------------------
121 | export
125 | export
129 | ||| Prepend namespace into which everything is generated to name
139 | ||| Given a sequence of arguments, return list of argument name-BindVar pairs
143 | ||| Given a list of arguments and a list of their aliases, apply aliases to then
157 | Element
164 | ||| Given a list of arguments, generate a list of aliases for them
165 | transformArgNames' : (f : Name -> Name) -> (as : List Arg) -> (0 _ : All IsNamedArg as) => List (Name, Name)
170 | ||| Given a list of arguments, generate a list of aliased arguments
171 | ||| and a list of aliases
181 | ||| Given a list of aliased argument pairs, generate a list of equality type
182 | ||| for each pair
190 | ||| Given a list of aliased argument pairs [(a, b), ...], generate a series of
191 | ||| named applications: (... {a=a} {b=a})
200 | ||| Make an argument omega implicit if it is explicit
204 | ||| Make an argument omega implicit
208 | ||| Make a type argument zero-count
212 | ||| A tuple value of multiple repeating expressions
218 | ||| Map all unmapped variables from the list to their aliases
222 | ||| Proof that hideExplicitArg doesn't affect namedness of arguments
232 | ||| Proof that makeImplicit doesn't affect namedness of arguments
238 | makeImplicitPreservesNames (x :: xs) @{_ :: _} with (x) -- This `with` match is a workaround for coverage checking bug
242 | ||| Make all explicit arguments in list implicit
243 | hideExplicitArgs : (xs : List Arg) -> (0 _ : All IsNamedArg xs) => Subset (List Arg) (All IsNamedArg)
246 | ||| Make all arguments in list implicit
247 | makeArgsImplicit : (xs : List Arg) -> (0 _ : All IsNamedArg xs) => Subset (List Arg) (All IsNamedArg)
250 | ||| Create a binding application of aliased arguments
251 | ||| binding everything to `(_)
257 | ||| Create a non-binding application of aliased arguments
262 | ---------------------
263 | --- TASK ANALYSIS ---
264 | ---------------------
266 | ||| Given a list of arguments and a sorted set of names,
267 | ||| assert that every argument's name is in that set
286 | ||| Get all the information needed for specialisation from task
298 | -- Check for unused arguments
300 | -- Extract name of polymorphic type
303 | -- Prove that all spec lambda arguments are named
306 | -- Create aliases for spec lambda's arguments and perform substitution
310 | -- Check for partial application in spec
313 | -- Prove that all spec lambda type's arguments are named
316 | -- Apply aliasing to spec lambda type's info
318 | -- Get current namespace
320 | -- Get polymorphic type's info
323 | -- Prove all its arguments/constructors/constructor arguments are named
337 | }
339 | ||| Generate an AnyApp for given Arg, with the argument value either
340 | ||| retrieved from the map if present or generated with `fallback`
346 | AnyApp
350 | ||| Generate a List AnyApp for given argument List,
351 | ||| with arguments retrieved from the map if present or generated with `fallback`
363 | ||| Returns a full application of the given type constructor
364 | ||| with argument values sourced from `argValues`
365 | ||| or generated with `fallback` if not present
366 | export
372 | TTImp
377 | ||| Returns a full application of the given constructor
378 | ||| with argument values sourced from `argValues`
379 | ||| or generated with `fallback` if not present
380 | export
386 | TTImp
396 | ---------------------------
397 | --- CONSTRUCTOR MAPPING ---
398 | ---------------------------
399 | ||| Run monadic operation on all constructors of specialised type
409 | ||| Map over all constructors for which unification succeeded
425 | ||| Run monadic operation on all pairs of specified and polymorphic constructors
444 | where
457 | -------------------------------
458 | --- CONSTRUCTOR UNIFICATION ---
459 | -------------------------------
461 | ||| Run unification for a given polymorphic constructor
474 | logPoint DetailedDebug "specialiseData.unifyCon" [t.polyTy, con] "Unifier task: \{show uniTask}"
476 | logPoint DetailedDebug "specialiseData.unifyCon" [t.polyTy, con] "Unifier output: \{show uniRes}"
479 | ---------------------------------
480 | --- SPECIFIED TYPE GENERATION ---
481 | ---------------------------------
483 | ||| Generate argument of a specified constructor
492 | let piInfo = if fromLambda && (fvData.piInfo == ExplicitArg) then ImplicitArg else fvData.piInfo
495 | ||| Generate a specialised constructor
508 | MkCon
514 | ||| Generate a specialised type
519 | MkTypeInfo
525 | ------------------------------------
526 | --- POLY TO POLY CAST DERIVATION ---
527 | ------------------------------------
529 | ||| Generate IPi with implicit type arguments and given return
534 | ||| Generate specialised to polymorphic type conversion function signature
539 | ||| Generate specialised to polymorphic type conversion function clause
540 | ||| for given constructor
550 | Clause
558 | ||| Generate specialised to polymorphic type conversion function declarations
569 | ]
571 | ||| Generate specialised to polymorphic cast signature
576 | ||| Generate specialised to polymorphic cast declarations
581 | ]
583 | -----------------------------------
584 | --- MULTIINJECTIVITY DERIVATION ---
585 | -----------------------------------
587 | ||| Derive multiinjectivity for a polymorphic constructor that has a
588 | ||| specialised equivalent
619 | ]
621 | ||| Derive multiinjectivity for all polymorphic constructors that have
622 | ||| a specialised equivalent
628 | ----------------------------------
629 | --- MULTICONGRUENCY DERIVATION ---
630 | ----------------------------------
632 | ||| Derive multicongruency for a specialised constructor
633 | |||
634 | ||| mCongN : forall argsN, argsN'; conN argsN === conN argsN'
661 | ]
663 | ||| Derive multicongruency for all specialised constructors
669 | -----------------------------------
670 | --- CAST INJECTIVITY DERIVATION ---
671 | -----------------------------------
673 | ||| Make a clause for the cast injectivity proof
685 | Clause
705 | ||| Derive cast injectivity proof
725 | ~=~
739 | ]
741 | -------------------------------------
742 | --- DECIDABLE EQUALITY DERIVATION ---
743 | -------------------------------------
745 | ||| Decidable equality signatures
750 | piAll
755 | ]
757 | ||| Decidable equality clause
765 | ||| Derive decidable equality
778 | ]
780 | -----------------------
781 | --- SHOW DERIVATION ---
782 | -----------------------
784 | ||| Derive Show implementation via cast
793 | forallMTArgs
797 | forallMTArgs
804 | ]
806 | ---------------------
807 | --- EQ DERIVATION ---
808 | ---------------------
810 | ||| Derive Eq implementation via cast
820 | forallMTArgs
824 | forallMTArgs
830 | ]
832 | ------------------------------------
833 | --- POLY TO POLY CAST DERIVATION ---
834 | ------------------------------------
836 | ||| Generate specialised to polymorphic type conversion function signature
841 | TTImp
845 | ||| Generate specialised to polymorphic type conversion function clause
846 | ||| for given constructor
856 | Clause
863 | ||| Generate specialised to polymorphic type conversion function declarations
874 | ]
876 | ||| Generate specialised to polymorphic cast signature
881 | ||| Generate specialised to polymorphic cast declarations
886 | ]
888 | -----------------------------
889 | --- FROMSTRING DERIVATION ---
890 | -----------------------------
900 | forallMTArgs
908 | ]
910 | ----------------------
911 | --- NUM DERIVATION ---
912 | ----------------------
923 | forallMTArgs
928 | forallMTArgs
933 | forallMTArgs
941 | `(MkNum
945 | ]
946 | ]
948 | ------------------------------------
949 | --- SPECIALISED TYPE DECLARATION ---
950 | ------------------------------------
952 | ||| Generate declarations for given task, unification results, and specialised type
953 | specDecls : MonadLog m => UniResults -> (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => m $ List Decl
1002 | ]
1015 | -------------------------------------
1016 | --- SPECIALISATION TASK INTERFACE ---
1017 | -------------------------------------
1019 | ||| Valid task lambda interface
1020 | |||
1021 | ||| Auto-implemented by any Type or any function that returns Type.
1022 | export
1025 | export
1028 | export
1031 | export
1034 | export
1037 | export
1040 | export
1043 | export
1046 | ---------------------------
1047 | --- DATA SPECIALISATION ---
1048 | ---------------------------
1050 | ||| Perform a specialisation for a given type name, kind and content expressions
1051 | |||
1052 | ||| In order to generate a specialised type declaration equivalent to the following type alias:
1053 | ||| ```
1054 | ||| VF : Nat -> Type
1055 | ||| VF n = Fin n
1056 | ||| ```
1057 | ||| ...you may use this function as follows:
1058 | ||| ```
1059 | ||| specialiseDataRaw `{VF} `(Nat -> Type) `(\n => Fin n)
1060 | ||| ```
1061 | export
1083 | ||| Perform a specialisation for a given type name and content lambda
1084 | |||
1085 | ||| In order to generate a specialised type declaration equivalent to the following type alias:
1086 | ||| ```
1087 | ||| VF : Nat -> Type
1088 | ||| VF n = Fin n
1089 | ||| ```
1090 | ||| ...you may use this function as follows:
1091 | ||| ```
1092 | ||| specialiseData `{VF} $ \n => Fin n
1093 | ||| ```
1094 | export
1107 | -- Quote spec lambda type
1109 | -- Quote spec lambda
1114 | ||| Perform a specialisation for a given type name and content lambda,
1115 | ||| returning a list of declarations and failing on error
1116 | |||
1117 | ||| In order to generate a specialised type declaration equivalent to the following type alias:
1118 | ||| ```
1119 | ||| VF : Nat -> Type
1120 | ||| VF n = Fin n
1121 | ||| ```
1122 | ||| ...you may use this function as follows:
1123 | ||| ```
1124 | ||| specialiseData'' `{VF} $ \n => Fin n
1125 | ||| ```
1126 | export
1144 | ||| Perform a specialisation for a given type name and content lambda,
1145 | ||| declaring the results and failing on error
1146 | |||
1147 | ||| In order to declare a specialised type declaration equivalent to the following type alias:
1148 | ||| ```
1149 | ||| VF : Nat -> Type
1150 | ||| VF n = Fin n
1151 | ||| ```
1152 | ||| ...you may use this function as follows:
1153 | ||| ```
1154 | ||| %runElab specialiseData' `{VF} $ \n => Fin n
1155 | ||| ```
1156 | export