From 09e62111fd29ec376e3e13ca80cb8e88a47bdea7 Mon Sep 17 00:00:00 2001 From: bringert Date: Wed, 7 Dec 2005 10:03:18 +0000 Subject: Transfer reference: added list patterns, do notation. --- doc/transfer-reference.html | 128 ++++++++++++++++++++++++++++++-------------- 1 file changed, 89 insertions(+), 39 deletions(-) (limited to 'doc/transfer-reference.html') diff --git a/doc/transfer-reference.html b/doc/transfer-reference.html index 820f0f152..7670da8b8 100644 --- a/doc/transfer-reference.html +++ b/doc/transfer-reference.html @@ -7,7 +7,7 @@

Transfer language reference

Author: Björn Bringert <bringert@cs.chalmers.se>
-Last update: Wed Dec 7 10:45:42 2005 +Last update: Wed Dec 7 11:02:54 2005

@@ -29,23 +29,24 @@ Last update: Wed Dec 7 10:45:42 2005
  • Tuples
  • Lists -
  • Pattern matching +
  • Case expressions +
  • Patterns -
  • Metavariables -
  • Overloaded functions / Type classes -
  • Operators -
  • Compositional functions -
  • do notation +
  • Metavariables +
  • Overloaded functions / Type classes +
  • Operators +
  • Compositional functions +
  • do notation

    @@ -140,11 +141,14 @@ equations. The first equation whose patterns match the function arguments is used when the function is called. Pattern equations are on the form:

    -  f p1 ... p1n = exp
    +  f p11 ... p1m = exp
       ...
    -  f qn1 ... qnm = exp
    +  f pn1 ... pnm = exp
     

    +

    +where p11 to pnm are patterns, see Patterns. +

    Data type declarations

    @@ -238,7 +242,7 @@ in the type. Such dependent function types are written:

    Here, x1 can be used in T2 to Tn, x1 can be used -in T2 to Tn +in T2 to Tn.

    Basic types

    @@ -355,7 +359,7 @@ be used instead of Cons. These are just syntactic sugar for express using Nil and Cons, with the type arguments hidden.

    -

    Pattern matching

    +

    Case expressions

    Pattern matching is done in pattern equations and by using the case construct: @@ -368,6 +372,7 @@ Pattern matching is done in pattern equations and by using the

    +where p1 to pn are patterns, see Patterns. guard1 to guardn are boolean expressions. Case arms can also be written without guards, such as:

    @@ -382,10 +387,9 @@ This is the same as writing: pk | True -> rhsk

    -

    -The syntax of patterns are decribed below. -

    - + +

    Patterns

    +

    Constructor patterns

    Constructor patterns are written as: @@ -400,7 +404,7 @@ If the value to be matched is the constructor C applied to arguments v1 to vn, then v1 to vn will be matched against p1 to pn.

    - +

    Variable patterns

    A variable pattern is a single identifier: @@ -413,7 +417,7 @@ A variable pattern is a single identifier: A variable pattern matches any value, and binds the variable name to the value. A variable may not occur more than once in a pattern.

    - +

    Wildcard patterns

    Wildcard patterns are written as with a single underscore: @@ -425,7 +429,7 @@ Wildcard patterns are written as with a single underscore:

    Wildcard patterns match all values and bind no variables.

    - +

    Record patterns

    Record patterns match record values: @@ -442,7 +446,7 @@ fields l1 to ln, and their values match p1 - +

    Disjunctive patterns

    It is possible to write a pattern on the form: @@ -455,9 +459,39 @@ It is possible to write a pattern on the form: A value will match this pattern if it matches any of the patterns p1 to pn. FIXME: talk about how this is expanded

    - -

    List patterns

    +

    List patterns

    +

    +When pattern matching in lists, there are two special constructs. +A whole list can be matched be a list of patterns: +

    +
    +  [p1, ... , pn]
    +
    +

    +

    +This pattern will match lists of length n, such that each element +in the list matches the corresponding pattern. The empty list pattern: +

    +
    +  []
    +
    +

    +

    +is a special case of this. It matches the empty list, oddly enough. +

    +

    +Non-empty lists can also be matched with ::-patterns: +

    +
    +  p1::p2
    +
    +

    +

    +This pattern matches a non-empty lists such that the first element of +the list matches p1 and the rest of the list matches p2. +

    +

    Tuple patterns

    Tuples patterns on the form: @@ -469,17 +503,17 @@ Tuples patterns on the form:

    are syntactic sugar for record patterns, in the same way as tuple expressions.

    - +

    String literal patterns

    String literals can be used as patterns.

    - +

    Integer literal patterns

    Integer literals can be used as patterns.

    - +

    Metavariables

    Metavariable are written as questions marks: @@ -494,17 +528,33 @@ A metavariable is a way to the the Transfer type checker that: I can't be bothered to tell you".

    -Metavariables can be used to avoid having to give type variables -and dictionaries explicitly. +Metavariables can be used to avoid having to give type +and dictionary arguments explicitly.

    - -

    Overloaded functions / Type classes

    -

    Operators

    +

    Overloaded functions / Type classes

    -

    Compositional functions

    +

    Operators

    +

    Compositional functions

    +

    do notation

    +

    +Sequences of operations in the Monad type class can be written +using do-notation, like in Haskell: +

    +
    +  do x <- f
    +     y <- g x
    +     h y
    +
    +

    +

    +is equivalent to: +

    +
    +  f >>= \x -> g x >>= \y -> h y
    +
    -- cgit v1.2.3