Support for Java reflective arrays.
The type JArray X corresponds to Java's J[] for any type X where J is the corresponding java type of X, but note that Java does not really have generic arrays.
We can use arrays of non-primitive types generically inside Frege, but native code that expects or returns arrays will not be generic.
The native interface will take every occurrence of
JArray X
or
Mutable s (JArray X)
in native declarations to denote the corresponding Java array type. But when the type argument is variable, it will be just Object. This corresponds to the usage in the array reflection API.
Multi-dimensional arrays are not very well supported out of the box. If one needs more than 1 dimensions, the type will get quite complex, as of course arrays are mutable and so one will have multiple levels of JArray nested in Mutable nested in JArray and so forth. Moreover, multi-dimensional arrays cannot be generic at all.
Note that there are really two different APIs:
The former ones are usable only in polymorphic functions where the type argument for JArray is a variable and we don't (want to) have ArrayElement constraints. They are not good for interfacing native methods that take or return arrays of a certain type. Run time type errors are possible because native methods could put anything there. However, when used in Frege only, the typing is safe.
The latter ones are truly type safe, because their Frege type corresponds to the expected Java compile time type, which is also the actual run time type.
Here is a cheat sheet for the different array get and set methods:
Array type Argument/ Description Result
setAt Mutable (JArray s X) Maybe X set null or data element setElemAt Mutable (JArray s X) X set data element getAt Mutable (JArray s X) Maybe X get null or data element getElemAt Mutable (JArray s X) X get data element (unsafe) itemAt JArray s X Maybe X get null or data element (pure) elemAt JArray s X X get data element (pure, unsafe)
"unsafe" in this context applies only to non-primitive types and means that the function will fail with a NullPointerException if the value accessed is a Java null.
Like JArray.genericItemAt but the result is not wrapped in Maybe.
The user is expected to prove that the element cannot be null or else risk a NullPointerException.
Can not be used with arrays of primitive values.
Equivalent of fold for mutable arrays.
Can not be used with arrays of primitive values.
Create a mutable array from a finite index/value list.
Indexes not mentioned in the list remain null for non primitive array elements and 0 otherwise.
Can not be used with arrays of primitive values.
Create a mutable array from a finite list.
Can not be used with arrays of primitive values.
Get the array element at a certain index of a mutable array and return it in the ST monad.
This will throw an IndexOutOfBoundsException if the index is lower than 0 or greater or equal to the length of the array.
Because in general, array elements may be null, the return value is wrapped in Maybe, as usual.
Can not be used with arrays of primitive values.
Get the array element at a certain index of a mutable array and return it in the ST monad.
This will throw an IndexOutOfBoundsException if the index is lower than 0 or greater or equal to the length of the array.
Unlike with JArray.getAt the element must not be null.
The user is expected to prove that the element cannot be null or else risk a NullPointerException.
Can not be used with arrays of primitive values.
Get the array element at a given index. This will throw an IndexOutOfBoundsException if the index is lower than 0 or greater or equal to the length of the array.
Because in general, array elements may be null, the return value is wrapped in Maybe, as usual.
Can not be used with arrays of primitive values.
Modify a mutable array by applying a function to all its elements.
Can not be used with arrays of primitive values.
Set the element at a certain index of a mutable array to a value that is wrapped in Maybe. This won't work for primitive element types.
This will throw an IndexOutOfBoundsException if the index is lower than 0 or greater or equal to the length of the array.
To set the corresponding array element to null, pass Maybe.Nothing, otherwise pass a Maybe.Just value.
Can not be used with arrays of primitive values.
Set the element at a certain index of a mutable array.
This will throw an IndexOutOfBoundsException if the index is lower than 0 or greater or equal to the length of the array.
Can not be used with arrays of primitive values.
Return the length of a mutable array in the ST monad.
Tell the length of an immutable Java array.
Because the length of an array cannot change, it is safe to use this function with Mutable.readonly.
create a one dimensional Java array
Unload an immutable array to a list.
The non-null elements become Maybe.Just values, the nulls translate to Maybe.Nothing
Create an immutable generic array from a finite index/value list.
Uses JArray.genericFromIndexList and freezes the resulting array.
(This is used in the parsers generated with YYGen)
Left fold an immutable array
Type class for basic JArray operations. The element type must be an instance of this class to support arrays of that type.
ArrayElement is derivable.
The operations are mostly overloaded on return type and provide the appropriate java.lang.Class object when needed.
This supports one dimensional arrays, though more dimensions would be possible with some extra effort.
Note that JArray cannot be an instance of ArrayElement itself, because it has no fixed java.lang.Class instance.
Int, Char, Bool, Float, Double, Long, StringJ, ->, (,), (,,), Integer, []
Create an immutable JArray from a finite index/value list. See ArrayElement.arrayFromIndexListST
Create a mutable array from a finite index/value list.
Indexes not mentioned in the list remain null for non primitive array elements and 0 otherwise.
Create an immutable array from a finite list whose elements are 'ArrayElement`
Uses JArray.fromList and freezes the resulting array.
Create a mutable array from a finite list.
Create an immutable JArray from a finite list of Maybe values.
Create a mutable array from a finite list of Maybe values.
The size of an JArray
Get non-null element at index from immutable array, see JArray.elemAt
Get item at index from mutable array, see JArray.getAt
Get non null item at index from mutable array, see JArray.getElemAt
Get item at index from immutable array, see JArray.itemAt
Unload JArray to a list, lazily
Unload JArray to a maybe list, lazily
Modify item at index in mutable array with result of function application.
Modify non null item at index in mutable array with result of function application.
Create a one dimensional array with elements of the instantiated type.
Set item or null at index in mutable array, see JArray.setAt
Set item at index in mutable array. see JArray.setElemAt
Create a mutable array of a given size and compute the values of its elements by some function. The function gets the current index and the already computed values in the form of an immutable array, where it can access elements with a smaller index than the current one.
The restriction to smaller indexes is because array elements are strict in Frege. For example, we can't store unevaluated values in an String[] array, because the Java type of unevaluated values is not String.
To create an array of 1000 Fibonacci numbers, one could write:
cache fib 1000 where fib 0 _ = 1n fib 1 _ = 1n fib n a = a.[n-1] + a.[n-2]
Memoize a number of results from a function that maps Int to the array element.
Uses ArrayElement.cache and makes it immutable
Map a function over the elements of an immutable array, and collect the results in another immutable array.
Uses ArrayElement.mapArrayST and makes result read-only.
Type class for array elements of primitive type.
Not thought for public use, as all instances are pre-defined.
The default implementation of PrimitiveArrayElement.setAt does not support passing Maybe.Nothing, because there can be no null in primitive arrays.
Int, Char, Bool, Float, Double, Long
Default implementation suitable for primitive types, wraps result with Maybe.Just
Default implementation suitable for primitive types, wraps result with Maybe.Just
Default implementation suitable for primitive types.
It is an error to put Maybe.Nothing in a primitive array.
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from ArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from ArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from ArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from ArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from ArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from ArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from ArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from ArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from ArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from ArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from ArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from ArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from ArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from ArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from ArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from ArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from ArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from ArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from Eq.!=
Unload an immutable array to a list
The resulting list consists of all the non null elements of the array argument.
This will work for arrays of reference type only!
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from PrimitiveArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from PrimitiveArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from PrimitiveArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from PrimitiveArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from PrimitiveArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from PrimitiveArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from PrimitiveArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from PrimitiveArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from PrimitiveArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from PrimitiveArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from PrimitiveArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from PrimitiveArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from PrimitiveArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from PrimitiveArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from PrimitiveArrayElement.setAt
inherited from ArrayElement.setElemAt
inherited from ArrayElement.arrayFromIndexList
inherited from ArrayElement.arrayFromIndexListST
inherited from ArrayElement.arrayFromList
inherited from ArrayElement.arrayFromListST
inherited from ArrayElement.arrayFromMaybeList
inherited from ArrayElement.arrayFromMaybeListST
inherited from ArrayElement.arrayLength
inherited from ArrayElement.elemAt
inherited from PrimitiveArrayElement.getAt
inherited from ArrayElement.getElemAt
inherited from PrimitiveArrayElement.itemAt
inherited from ArrayElement.listFromArray
inherited from ArrayElement.maybeListFromArray
inherited from ArrayElement.modifyAt
inherited from ArrayElement.modifyElemAt
inherited from ArrayElement.newArray
inherited from PrimitiveArrayElement.setAt
inherited from ArrayElement.setElemAt
ArrayElement_String.modifyAt, ArrayElement_String.modifyElemAt
PrimitiveArrayElement_Bool.modifyAt, PrimitiveArrayElement_Bool.modifyElemAt
PrimitiveArrayElement_Char.modifyAt, PrimitiveArrayElement_Char.modifyElemAt
PrimitiveArrayElement_Double.modifyAt, PrimitiveArrayElement_Double.modifyElemAt
PrimitiveArrayElement_Float.modifyAt, PrimitiveArrayElement_Float.modifyElemAt
PrimitiveArrayElement_Int.modifyAt, PrimitiveArrayElement_Int.modifyElemAt
ArrayElement_Integer.modifyAt, ArrayElement_Integer.modifyElemAt
PrimitiveArrayElement_Long.modifyAt, PrimitiveArrayElement_Long.modifyElemAt