1 people like it.

HList with Mapper and Folder

Extended from: http://www.fssnip.net/d2

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
type HList = interface end
and HNil = HNil with
    static member inline (|*|) (f, HNil) = f $ HNil
    interface HList
and HCons<'a, 'b when 'b :> HList> = HCons of 'a * 'b with
    static member inline (|*|) (f, HCons(x, xs)) = f $ HCons(x, xs) 
    interface HList 

let inline head (HCons(h,t)) = h
let inline tail (HCons(h,t)) = t

type Peano = interface end
and Zero = Zero with
    static member inline (|*|) (f, Zero) = f $ Zero
    interface Peano
and Succ<'a when 'a :> Peano> = Succ of 'a  with
    static member inline (|*|) (f, Succ(x)) = f $ Succ(x) 
    interface Peano 

type Bool = interface end
and True = True with
    interface Bool
and False = False with
    interface Bool

let inline (^+^) head tail = HCons(head, tail)

// Examples

type Append = Append with
    static member ($) (Append, HNil) = id
    static member inline ($) (Append, HCons(x, xs)) = fun list ->
        HCons (x, (Append |*| xs) list)

type ZeroMapper = ZeroMapper with
    static member ($) (ZeroMapper, HNil) = HNil
    static member inline ($) (ZeroMapper, HCons(x : string, xs)) = HCons("", ZeroMapper |*| xs)
    static member inline ($) (ZeroMapper, HCons(x : int, xs)) = HCons(0, ZeroMapper |*| xs)

type ZeroMap = ZeroMap with
    static member inline ($) (ZeroMap, s: string) = ""
    static member inline ($) (ZeroMap, i: int) = 0

type Mapper<'a> = Mapper of 'a with
    static member ($) (Mapper(M), HNil) = HNil
    static member inline ($) (Mapper(M), HCons(x, xs)) = HCons(M $ x, (Mapper(M) |*| (xs)))

type StrFolder = StrFolder with
    static member inline ($) (StrFolder, (s1: string, s2: string)) = s1 + s2 
    static member inline ($) (StrFolder, (s1: string, i2: int)) = s1 + (string i2)

type Folder<'a, 'v> = Folder of 'a * 'v with
    static member ($) (Folder(F, v), HNil) = v
    static member inline ($) (Folder(F, v), HCons(x, xs)) = Folder(F, F $ (v,x)) |*| xs

type Length = Length with
    static member ($) (Length, HNil) = Zero
    static member inline ($) (Length, HCons(x, xs)) = Succ (Length |*| xs) 

type Count = Count with
    static member ($) (Count, HNil) = 0
    static member inline ($) (Count, HCons(x, xs)) = 1 + (Count |*| xs) 

let first = 1 ^+^ '1' ^+^ HNil
let second =  "1" ^+^ true ^+^ HNil
let third = "one" ^+^ 123 ^+^ HNil


// result : HCons<int,HCons<char,HCons<string,HCons<bool,HNil>>>>
let result = (Append $ first) second // HCons (1,HCons ('1',HCons ("1",HCons (true,HNil))))

ZeroMapper $ third // HCons<string,HCons<int,HNil>> = HCons ("",HCons (0,HNil))
ZeroMapper $ second // type error, no char version of ZeroMapper defined

Mapper(ZeroMap) $ third //HCons<string,HCons<int,HNil>> = HCons ("",HCons (0,HNil))
Mapper(ZeroMap) $ second // Compiler error

Folder(StrFolder, "") $ third
Folder(StrFolder, "") $ second // Compiler error

tail third // HCons (123,HNil)
(tail >> tail) third // HNil
(tail >> tail >> tail) third // compiler error

// length : Succ<Succ<Succ<Succ<Zero>>>>
let length = Length $ result // Succ (Succ (Succ (Succ Zero)))
let count = Count $ result // 4
Multiple items
union case HNil.HNil: HNil

--------------------
type HNil =
  | HNil
  interface HList
  static member ( |*| ) : f:'a * HNil:HNil -> '_arg5 (requires member ( $ ))

Full name: Script.HNil
val f : 'a (requires member ( $ ))
type HList

Full name: Script.HList
Multiple items
union case HCons.HCons: 'a * 'b -> HCons<'a,'b>

--------------------
type HCons<'a,'b (requires 'b :> HList)> =
  | HCons of 'a * 'b
  interface HList
  static member ( |*| ) : f:'a0 * HCons<'b1,'c> -> '_arg8 (requires member ( $ ) and 'c :> HList)

Full name: Script.HCons<_,_>
val f : 'a (requires member ( $ ) and 'c :> HList)
val x : 'b
val xs : #HList
val head : HCons<'a,#HList> -> 'a

Full name: Script.head
val h : 'a
val t : #HList
val tail : HCons<'a,'b> -> 'b (requires 'b :> HList)

Full name: Script.tail
type Peano

Full name: Script.Peano
Multiple items
union case Zero.Zero: Zero

--------------------
type Zero =
  | Zero
  interface Peano
  static member ( |*| ) : f:'a * Zero:Zero -> '_arg5 (requires member ( $ ))

Full name: Script.Zero
Multiple items
union case Succ.Succ: 'a -> Succ<'a>

--------------------
type Succ<'a (requires 'a :> Peano)> =
  | Succ of 'a
  interface Peano
  static member ( |*| ) : f:'a0 * Succ<'b> -> '_arg8 (requires member ( $ ) and 'b :> Peano)

Full name: Script.Succ<_>
val f : 'a (requires member ( $ ) and 'b :> Peano)
val x : #Peano
type Bool

Full name: Script.Bool
Multiple items
union case True.True: True

--------------------
type True =
  | True
  interface Bool

Full name: Script.True
type False =
  | False
  interface Bool

Full name: Script.False
Multiple items
union case False.False: False

--------------------
type False =
  | False
  interface Bool

Full name: Script.False
val head : 'a
val tail : #HList
Multiple items
union case Append.Append: Append

--------------------
type Append =
  | Append
  static member ( $ ) : Append:Append * HNil:HNil -> ('a -> 'a)
  static member ( $ ) : Append:Append * HCons<'a,'b> -> ('c -> HCons<'a,'d>) (requires 'b :> HList and member ( |*| ) and 'd :> HList)

Full name: Script.Append
val id : x:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.id
val x : 'a
val xs : 'b (requires 'b :> HList and member ( |*| ) and 'd :> HList)
Multiple items
val list : 'c

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Multiple items
union case ZeroMapper.ZeroMapper: ZeroMapper

--------------------
type ZeroMapper =
  | ZeroMapper
  static member ( $ ) : ZeroMapper:ZeroMapper * HNil:HNil -> HNil
  static member ( $ ) : ZeroMapper:ZeroMapper * HCons<string,'b> -> HCons<string,'_arg9> (requires 'b :> HList and member ( |*| ) and '_arg9 :> HList)
  static member ( $ ) : ZeroMapper:ZeroMapper * HCons<int,'a> -> HCons<int,'_arg12> (requires 'a :> HList and member ( |*| ) and '_arg12 :> HList)

Full name: Script.ZeroMapper
val x : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val xs : 'b (requires 'b :> HList and member ( |*| ) and '_arg9 :> HList)
val x : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val xs : 'a (requires 'a :> HList and member ( |*| ) and '_arg12 :> HList)
Multiple items
union case ZeroMap.ZeroMap: ZeroMap

--------------------
type ZeroMap =
  | ZeroMap
  static member ( $ ) : ZeroMap:ZeroMap * s:string -> string
  static member ( $ ) : ZeroMap:ZeroMap * i:int -> int

Full name: Script.ZeroMap
val s : string
val i : int
Multiple items
union case Mapper.Mapper: 'a -> Mapper<'a>

--------------------
type Mapper<'a> =
  | Mapper of 'a
  static member ( $ ) : Mapper<'a0> * HNil:HNil -> HNil
  static member ( $ ) : Mapper<'a0> * HCons<'b,'c> -> HCons<'_arg7,'_arg10> (requires member ( $ ) and 'c :> HList and member ( |*| ) and '_arg10 :> HList)

Full name: Script.Mapper<_>
val M : 'a
val M : 'a (requires member ( $ ))
val x : 'b (requires member ( $ ))
val xs : 'c (requires 'c :> HList and member ( |*| ) and member ( $ ) and '_arg10 :> HList)
Multiple items
union case StrFolder.StrFolder: StrFolder

--------------------
type StrFolder =
  | StrFolder
  static member ( $ ) : StrFolder:StrFolder * (string * string) -> string
  static member ( $ ) : StrFolder:StrFolder * (string * int) -> string

Full name: Script.StrFolder
val s1 : string
val s2 : string
val i2 : int
Multiple items
union case Folder.Folder: 'a * 'v -> Folder<'a,'v>

--------------------
type Folder<'a,'v> =
  | Folder of 'a * 'v
  static member ( $ ) : Folder<'a0,'b> * HNil:HNil -> 'b
  static member ( $ ) : Folder<'a0,'b> * HCons<'c,'d> -> '_arg7 (requires member ( $ ) and 'd :> HList and member ( |*| ))

Full name: Script.Folder<_,_>
val F : 'a
val v : 'b
val F : 'a (requires member ( $ ))
val x : 'c
val xs : 'd (requires 'd :> HList and member ( |*| ) and member ( $ ))
Multiple items
union case Length.Length: Length

--------------------
type Length =
  | Length
  static member ( $ ) : Length:Length * HNil:HNil -> Zero
  static member ( $ ) : Length:Length * HCons<'a,'b> -> Succ<'_arg7> (requires 'b :> HList and member ( |*| ) and '_arg7 :> Peano)

Full name: Script.Length
val xs : 'b (requires 'b :> HList and member ( |*| ) and '_arg7 :> Peano)
Multiple items
union case Count.Count: Count

--------------------
type Count =
  | Count
  static member ( $ ) : Count:Count * HNil:HNil -> int
  static member ( $ ) : Count:Count * HCons<'a,'b> -> int (requires 'b :> HList and member ( |*| ))

Full name: Script.Count
val xs : 'b (requires 'b :> HList and member ( |*| ))
val first : HCons<int,HCons<char,HNil>>

Full name: Script.first
val second : HCons<string,HCons<bool,HNil>>

Full name: Script.second
val third : HCons<string,HCons<int,HNil>>

Full name: Script.third
val result : HCons<int,HCons<char,HCons<string,HCons<bool,HNil>>>>

Full name: Script.result
val length : Succ<Succ<Succ<Succ<Zero>>>>

Full name: Script.length
val count : int

Full name: Script.count

More information

Link:http://fssnip.net/pu
Posted:9 years ago
Author:Rick Minerich
Tags: hlist , types