Type alias for list of other types in Elm - elm

If I try to define new type alias, for example, in a following manner:
type alias ListOfInts = [Int]
I get following error:
I was partway through parsing a type alias, but I got stuck here:
11| type alias ListOfInts = [Int]
^
I was expecting to see a type next. Try putting Int or String for now?
Is there a way to define type alias for Lists in Elm?

Unlike some other languages such as Haskell, Elm does not use square brackets as special notation for its list type. Consequently, as the compiler tells you, it can't make sense of the [ in a context where it expects to see a type.
The type is instead written List Int, as you can find for example in the Elm guide.
So just change your code to this:
type alias ListOfInts = List Int

Related

What is right part in "query : SelectionSet Response RootQuery" type annotations?

I try to understand this line:
query : SelectionSet Response RootQuery
I understand this is a "Type Annotations" syntax, but I don't find documentation example or explanation about multiple "word" separated by whitespace.
I see these examples:
answer : Int
factorial : Int -> Int
distance : { x : Float, y : Float } -> Float
add : number -> number -> number (ref)
I found anywhere query: Int Int Int syntax, neither in Elm Syntax nor in Beginning Elm nor in Elm FAQ.
Do SelectionSet, Response, RootQuery are
functions arguments?
multi value function results?
Best regards,
Stéphane
Same question on Elm Discourse
A response in How do I read the type of a SelectionSet?
SelectionSet is a type with two type variables. Here's the definition:
type SelectionSet decodesTo scope
= SelectionSet (List RawField) (Decoder decodesTo)
In a type declaration like this, any lowercase name after the type is a type variable, which can be filled in by any type (except for a few special constrained type variables, number, appendable, comparable, and compappend). A simpler example would be Maybe, where you can have a Maybe Int or a Maybe String. Similarly, Dict takes two type variables (for the key and value) so you can have Dict String String or Dict Int MyCustomType (the key type of a Dict does need to be comparable).
So, in your scenario, Response corresponds to decodesTo and RootQuery corresponds to scope. The SelectionSet has a Decoder that decodes to a Response value, and it also carries around this scope type variable, which isn't used directly in the data that it holds. It's used as a piece of information at the type level, so that you (and the library) know that calling map (which has the type (a -> b) -> SelectionSet a scope -> SelectionSet b scope) will preserve that scope value; that is, it prevents mixing scopes.
The above is mostly general function syntax for functional languages such as Haskell, Elm, etc.
For example:
add : Int -> Int -> Int
add x y = x + y
The first line says that is function of two integer arguments with an integer return value. The second line implements the specification given in the first. One calls this function as follows:
> add 2 3
5
Note this example:
> inc = add 1
> inc 2
3
Here add 1 is a "partially applied function." It has type Int -> Int. In many languages, add 1 would not make sense. But since functions are first-class things in Elm, add 1 makes sense: it is a function. Partial application is also called "currying."

Creating F# discrimated union type in C#

I'm trying to add F# project to my C# solution. I created a F# project and wrote some F# code, now I'm trying to use it from my C# projects.
I successfully referenced F# project and can access it's types, but having issues with discriminated unions. For example I have following types defined in F#:
namespace Sample
type NotificationReceiverUser = NotificationReceiverUser of string
type NotificationReceiverGroup = NotificationReceiverGroup of string
type NotificationReceiver = NotificationReceiverUser | NotificatonReceiverGroup
I can create NotificationReceiverUser object directly as follows:
var receiver = NotificationReceiverUser.NewNotificationReceiverUser("abc");
,but I need NotificationReceiver object, and I'm not getting NotificationReceiver.NewNotificationReceiverUser or NotificationReceiver.NewNotificationReceiverGroup static methods. Looking at some other SO questions it looks like these methods should be available by default. Would appreciate any pointers on why they are missing for me.
What you're trying to do is not a "discriminated union". It's an indiscrimnated union. First you created two types, and then you're trying to say: "values of this third type may be either this or that". Some languages have indiscriminated unions (e.g. TypeScript), but F# does not.
In F#, you can't just say "either this or that, go figure it out". You need to give each case of the union a "tag". Something by which to recognize it. That's why it's called a "discriminated" union - because you can discriminate between the cases.
For example:
type T = A of string | B of int
Values of type T may be either string or int, and the way to know which one is to look at the "tags" assigned to them - A or B respectively.
The following, on the other hand, is illegal in F#:
type T = string | int
Coming back to your code, in order to "fix" it the mechanical way, all you need to do is add case discriminators:
type NotificationReceiverUser = NotificationReceiverUser of string
type NotificationReceiverGroup = NotificationReceiverGroup of string
type NotificationReceiver = A of NotificationReceiverUser | B of NotificatonReceiverGroup
But my intuition tells me that what you actually meant to do was this:
type NotificationReceiver =
| NotificationReceiverUser of string
| NotificatonReceiverGroup of string
Two cases of the same type (weird, but legal), still distinguished by tags.
With such definition, you would access it from C# thusly:
var receiver = NotificationReceiver.NewNotificationReceiverUser("abc");

PostgreSQL create type PL/pgSQL and cstring

I wanna format some fields in the output of my PostgreSQL 9.1 database. I thought of creating a type, so I could do the formatting in the output function, and checking for inconsistencies in the input function. I decided to use the procedural language PL/pgSQL. But I'm getting some errors:
CREATE OR REPLACE FUNCTION "CPF_in"(cstring)
"PL/pgSQL functions cannot accept type cstring"
(But that's how it is in the manual.) I can put "character varying" instead of cstring, or even leave the () empty. But when I'm going to create the desired type:
CREATE TYPE Tcpf (
INPUT = CPF_in(character varying),
OUTPUT = CPF_out
);
I got an error:
ERROR: syntax error at or near ")"
LINE 2: INPUT = CPF_in(character varying),
and if I try
CREATE TYPE Tcpf (
INPUT = CPF_in(),
OUTPUT = CPF_out
);
I get
ERROR: syntax error at or near ")"
LINE 2: INPUT = CPF_in(),
How is this supposed to be done? The manual only say cstring...
The cstring pseudo-type is used for programming in a low-level language like C, not in PL/pgSQL. You have to use a low-level language like C if you're creating a new base type.
You must register two or more functions (using CREATE FUNCTION) before
defining the type. The support functions input_function and
output_function are required . . . .
Generally these functions have to be coded in C or another low-level
language.
A simpler way to control the output format is to use a view. If your formatting is complex, write a function, and call that function from a view. You can revoke permissions on the base table if you need to force every client to use your formatting. You might need to create triggers to make your view fully updatable.
For controlling input, you can use a function. (CREATE FUNCTION...) You can write functions in PL/pgSQL. Again, consider revoking permissions on the table.

DB2 SQL array in attribute-defs of user defined type

I have a question concerning user defined types in DB2(v. 9.7.0.441). I want to create a type which has an attribute-array of another user defined type. Let me show you what I mean by a brief (fictional) example:
This is the UDT I want to use in another type
CREATE TYPE sport AS
(
Sport VARCHAR(10)
) MODE DB2SQL;
This is the UDT which should use the one above
CREATE TYPE person AS
(
plays sport ARRAY[3] // 'REF(sport)' or 'plays VARCHAR(10) ARRAY[3]' don't work either
) MODE DB2SQL;
DB2 just says that the token ARRAY[3] is unexpected.
Any hint what could be wrong here? By now it would be enough to have an CHAR Array in a UDT...
Thanks in advance
Ok ok,
according to db2's CREATE TYPE (array) statement "an array type can only be used as the data type of:
A local variable in a compound SQL (compiled) statement
A parameter of an SQL routine
A parameter of a Java procedure (ordinary arrays only)
The returns type of an SQL function
A global variable
and
A variable or parameter defined with an array type can only be used in compound SQL (compiled) statements
"
So its just not possible to use an array type inside a user defined type :-/
I would assume something like the following...
CREATE TYPE sport AS(Sport VARCHAR(10)) MODE DB2SQL;
CREATE TYPE PersonT AS(plays SportT ARRAY[3]) MODE DB2SQL;
CREATE TYPE SportArrayT AS SportT ARRAY[3];
CREATE TYPE PersonT AS (plays SportT SportArrayT) MODE DB2SQL;
However, the 3rd statement fails. Obviously an array of a user-defined type (or REF(SportT)) is not allowed :-(
Have a look at the doc for CREATE TYPE (array).
I'm not really sure what you're trying to do but the examples they give are like this:
CREATE TYPE CAPITALSARRAY AS VARCHAR(30) ARRAY[VARCHAR(20)]

type +'a t in Ocaml Map Library?

I'm working with Ocaml's built-in Map library for a problem set, and I'm having trouble accessing the datatype of a map itself. This is supposed to be the third implementation of a dictionary (the first two being a list and an unbalanced binary search tree), and part of the functor I have to implement is "type dict", which is the datatype of the actual dictionary. For list, type dict was (D.key * D.value) list; for the tree, type dict was Empty | Branch((D.key * D.value), dict, dict). The Ocaml documentation says:
type +'a t
The type of maps from type key to type 'a.
This seems like what I need, but I can't seem to use it correctly. M is my Map.Make module, by the way. I've tried
type dict = M.t
type dict = M.+D.value t
type dict = M.+
But I keep getting error messages. Can anyone help? Thanks so much!
+ is a variance annotation, it is not part of the name. The syntax of parametrized type is param type or (param, param, ...) type in OCaml : int list, (int, string) Hashbl.t. What you want here is D.value M.t.
You can find out yourself by asking the ocaml compiler: ocamlc -i file.ml
To create a map via Map.Make from the standard ocaml library, this file would look like this (for a map from int to 'a):
module Intmap = Map.Make (struct
type t = int
let compare = Pervasives.compare
end)
The ocaml compiler will give you something like this (when calling ocamlc -i mymap.ml):
module Intmap :
sig
type key = int
type +'a t
val empty : 'a t
...
end