Using Top.Fold to count fields of a record

From Impredicative Wiki
Revision as of 10:04, 8 December 2010 by Karn Kallio (Talk | contribs)

Jump to: navigation, search

This code example shows a use of Top.Fold to count the fields of a type level record.

con rr :: {Type} = [One = int, Two = string, Three = bool, Four = list float] 

val q : int = @@fold [fn (rx :: {Type}) => (int)]
                (fn [nm :: Name] [t :: Type] [r :: {Type}] [[nm] ~ r] acc => 1 + acc)
                (0) [rr] (_ : folder rr)


fun main () = return <xml>
  <head>
    <title>Count fields of a record type</title>
  </head>
  <body>
    <h1>Count fields of a record type</h1>
    <p>
      We can use fold to count the fields in a type level record.
    </p>
    <p>
      Here is the value of q: {[q]}
    </p>
  </body>
</xml>
Personal tools