type FruitSalad = {
Apples: AppleType
Bananas: BananaType
Cherries: CherryType
}
type AppleType =
| GoldenDelicious
| Fuji
| GrannySmith
type AppleType GoldenDelicious | AppleType Fuji | AppleType GrannySmith;
Well, you wouldn’t really use a lot of these static data types in a dynamically-typed language, but they might occasionally be useful, so here goes.
First, you’d need to declare the symbolic fruit names, so that they’re not mistaken as variables in the type declaration. Let’s say:
nonfix GoldenDelicious Fuji GrannySmith Chiquita Piemont;
type AppleType GoldenDelicious | AppleType Fuji | AppleType GrannySmith;
type BananaType Chiquita;
type CherryType Piemont;
Second, an idiomatic representation of the FruitSalad record type would simply use a constructor symbol with arguments:
type FruitSalad (FruitSalad _::AppleType _::BananaType _::CherryType);
But then you have to specify the arguments in the right order:
> typep FruitSalad (FruitSalad Fuji Chiquita Piemont);
1
> typep FruitSalad (FruitSalad Fuji Piemont Chiquita);
0
You can use Pure’s record type instead, but you’ll have to go to some lengths to define the type and make it work exactly like the F# type:
type FruitSalad x = recordp x && all (member x) [Apples, Bananas, Cherries] &&
#keys x == 3 && typep AppleType (x!Apples) && typep BananaType (x!Bananas) &&
typep CherryType (x!Cherries);
(Yes, that’s tedious and error-prone. However, one could write a little convenience function that would take care of all the mindless drudgery, like enum does for enumeration types.)
That will allow you to specify the different fields in any order:
> typep FruitSalad {Apples=>Fuji, Bananas=>Chiquita, Cherries=>Piemont};
1
> typep FruitSalad {Apples=>Fuji, Cherries=>Piemont, Bananas=>Chiquita};
1
Writing an entire Pure program in that style won’t be much fun, though. If you need these rigid types, you’re much better off writing your program in F#. If you need to check your data for validity, a corresponding predicate would work just fine in Pure. Basically, that’s what Pure’s types are, anyway.
Pure’s types are not really a data structuring device (who needs all that fluff if you can just write down any kind of structured data as a term?), but a means to do data abstraction. The basics of that are sketched out in the Type Tags section of the Pure manual (see the definition of the point
type there). The Type Rules section has another example on Peano arithmetic.
Albert
--
You received this message because you are subscribed to the Google Groups "pure-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pure-lang+unsubscribe@googlegroups.com.
To post to this group, send email to pure...@googlegroups.com.
Visit this group at https://groups.google.com/group/pure-lang.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "pure-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pure-lang+unsubscribe@googlegroups.com.
To post to this group, send email to pure...@googlegroups.com.
Visit this group at https://groups.google.com/group/pure-lang.
For more options, visit https://groups.google.com/d/optout.