Using Top.Fold to count fields of a record

From Impredicative Wiki
(Difference between revisions)
Jump to: navigation, search
(Demonstration of Top.fold usage to count record type fields)
m (Fixed missing pre tag)

Revision as of 10:04, 8 December 2010

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>
    <title>Count fields of a record type</title>
    <h1>Count fields of a record type</h1>
      We can use fold to count the fields in a type level record.
      Here is the value of q: {[q]}
Personal tools