From 31e82db1a6d6d448fa783a9829afacf7af6a03ba Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Tue, 21 Oct 2008 19:56:20 -0400 Subject: Sum prose --- demo/link.ur | 2 +- demo/prose | 25 +++++++++++++++++++++++++ demo/sum.ur | 1 + src/elab_err.sig | 2 +- src/elab_err.sml | 6 +++--- src/elaborate.sml | 8 ++++---- 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/demo/link.ur b/demo/link.ur index 122b98f1..f9e33968 100644 --- a/demo/link.ur +++ b/demo/link.ur @@ -3,5 +3,5 @@ fun target () = return fun main () = return - Go there + Go there diff --git a/demo/prose b/demo/prose index a863f6c5..7f9f6bb0 100644 --- a/demo/prose +++ b/demo/prose @@ -71,3 +71,28 @@ sql.urp

Other functions demonstrate use of the dml function, for building a transaction from a SQL DML command. It is easy to insert antiquoted Ur code into queries and DML commands, and the type-checker catches mistakes in the types of the expressions that we insert.

+ +sum.urp + +

Metaprogramming is one of the most important facilities of Ur. This example shows how to write a function that is able to sum up the fields of records of integers, no matter which set of fields the particular record has.

+ +

Ur's support for analysis of types is based around extensible records, or row types. In the definition of the sum function, we see the type parameter fs assigned the kind {Unit}, which stands for records of types of kind Unit. The Unit kind has only one inhabitant, (). The kind Type is for "normal" types.

+ +

The unary $ operator is used to build a record Type from a {Type} (that is, the kind of records of types). The library function mapUT takes in a Type t and a {Unit} r, and it builds a {Type} as long as r, where every field is assigned value t.

+ +

Another library function foldUR is defined at the level of expressions, while mapUT is a type-level function. foldUR takes 6 arguments, some of them types and some values. Type arguments are distinguished by being written within brackets. The arguments to foldUR respectively tell us: + +

    +
  1. The type we will assign to each record field
  2. +
  3. The type of the final and all intermediate results of the fold, expressed as a function over the portion of the {Unit} that has been traversed so far
  4. +
  5. A function that updates the accumulator based on the current record field name, the rest of the input record type, the current record field value, and the current accumulator
  6. +
  7. The initial accumulator value
  8. +
  9. The input record type
  10. +
  11. The input record value
  12. +
+ +An unusual part of the third argument is the syntax [t1 ~ t2] within a multi-argument fn. This syntax denotes a proof that row types t1 and t2 have no field names in common. The proof is not named, because it is applied automatically as needed. Indeed, the proof appears unused in this case, though it is actually needed to ensure the validity of some inferred types, as well as to unify with the type of foldUR.

+ +

The general syntax for constant row types is [Name1 = t1, ..., NameN = tN], and there is a shorthand version [Name1, ..., NameN] for records of Units.

+ +

With sum defined, it is easy to make some sample calls. The form of the code for main does not make it apparent, but the compiler must "reverse engineer" the appropriate {Unit} from the {Type} available from the context at each call to sum.

diff --git a/demo/sum.ur b/demo/sum.ur index 22a21d8b..9391637a 100644 --- a/demo/sum.ur +++ b/demo/sum.ur @@ -4,6 +4,7 @@ fun sum (fs ::: {Unit}) (x : $(mapUT int fs)) = 0 [fs] x fun main () = return + {[sum {}]}
{[sum {A = 0, B = 1}]}
{[sum {C = 2, D = 3, E = 4}]}
diff --git a/src/elab_err.sig b/src/elab_err.sig index 1fd234b7..d757572f 100644 --- a/src/elab_err.sig +++ b/src/elab_err.sig @@ -49,7 +49,7 @@ signature ELAB_ERR = sig | COccursCheckFailed of Elab.con * Elab.con | CIncompatible of Elab.con * Elab.con | CExplicitness of Elab.con * Elab.con - | CKindof of Elab.kind * Elab.con + | CKindof of Elab.kind * Elab.con * string | CRecordFailure of Elab.con * Elab.con val cunifyError : ElabEnv.env -> cunify_error -> unit diff --git a/src/elab_err.sml b/src/elab_err.sml index 8131633c..e8d7ff68 100644 --- a/src/elab_err.sml +++ b/src/elab_err.sml @@ -109,7 +109,7 @@ datatype cunify_error = | COccursCheckFailed of con * con | CIncompatible of con * con | CExplicitness of con * con - | CKindof of kind * con + | CKindof of kind * con * string | CRecordFailure of con * con fun cunifyError env err = @@ -131,8 +131,8 @@ fun cunifyError env err = eprefaces "Differing constructor function explicitness" [("Con 1", p_con env c1), ("Con 2", p_con env c2)] - | CKindof (k, c) => - eprefaces "Unexpected kind for kindof calculation" + | CKindof (k, c, expected) => + eprefaces ("Unexpected kind for kindof calculation (expecting " ^ expected ^ ")") [("Kind", p_kind k), ("Con", p_con env c)] | CRecordFailure (c1, c2) => diff --git a/src/elaborate.sml b/src/elaborate.sml index aa48952c..6e23c760 100644 --- a/src/elaborate.sml +++ b/src/elaborate.sml @@ -481,7 +481,7 @@ (case hnormKind (kindof env c) of (L'.KArrow (_, k), _) => k | (L'.KError, _) => kerror - | k => raise CUnify' (CKindof (k, c))) + | k => raise CUnify' (CKindof (k, c, "arrow"))) | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc) | L'.CDisjoint (_, _, _, c) => kindof env c @@ -497,7 +497,7 @@ | L'.CProj (c, n) => (case hnormKind (kindof env c) of (L'.KTuple ks, _) => List.nth (ks, n - 1) - | k => raise CUnify' (CKindof (k, c))) + | k => raise CUnify' (CKindof (k, c, "tuple"))) | L'.CError => kerror | L'.CUnif (_, k, _, _) => k @@ -546,7 +546,7 @@ case hnormKind (kindof env c) of (L'.KRecord k, _) => k | (L'.KError, _) => kerror - | k => raise CUnify' (CKindof (k, c)) + | k => raise CUnify' (CKindof (k, c, "record")) val k1 = rkindof c1 val k2 = rkindof c2 @@ -2318,7 +2318,7 @@ fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) = val env = if n1 = n2 then env else - E.pushCNamedAs env x n1 (L'.KType, loc) + E.pushCNamedAs env x n1 k' (SOME (L'.CNamed n2, loc)) in SOME (env, denv) -- cgit v1.2.3