Advertisement
xavierm02

Untitled

Jul 18th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.08 KB | None | 0 0
  1. open Batteries
  2. open Map
  3.  
  4. (* Function implementation
  5.   - 'i: input
  6.   - 'o: output
  7.   - 'd: datatype
  8.  
  9.   - 'r: result
  10.  
  11.   'i ~> 'o := ('i, 'o, 'd) fi
  12. *)
  13.  
  14. type ('i, 'o, 'd) fi =
  15.   {
  16.     make: unit -> 'd;
  17.     set: 'i -> 'o -> 'd -> 'd;
  18.     unset: 'i -> 'd -> 'd;
  19.     get: 'i -> 'd -> 'o option;
  20.     foldi: 'r. ('i -> 'o -> 'r -> 'r) -> 'd -> 'r -> 'r
  21.   }
  22.  
  23. (* Function implementation by PMap
  24.   'i ~> 'o := ('i, 'o, ('i, 'o) PMap)
  25. *)
  26.  
  27. let pmap_implementation =
  28.   let make' () =
  29.     PMap.empty
  30.   in
  31.   let set' input output data =
  32.     PMap.add input output data
  33.   in
  34.   let unset' input data =
  35.     PMap.remove input data
  36.   in
  37.   let get' input data =
  38.     try
  39.       Some (PMap.find input data)
  40.     with
  41.     | Not_found -> None
  42.   in
  43.   let foldi' f data x =
  44.     PMap.foldi f data x
  45.   in
  46.   {
  47.     make = make';
  48.     set = set';
  49.     unset = unset';
  50.     get = get';
  51.     foldi = foldi'
  52.   }
  53.  
  54. (* Function recursive implementation
  55.   - 't: type (abstract type)
  56.   - 'd: datatype (concrete type)
  57.   - 'a: abstraction (abstraction of abstract type) (it's the same thing as the abstract type except that in C, 'c is replaced by unit
  58.                                 to allow differetiation function types treated as constants and function types being implemented)
  59.  
  60.   - 'c: constant type
  61.   - 'it: input type
  62.   - 'id: input datatype
  63.   - 'ia: input abstraction
  64.   - 'ot: output type
  65.   - 'od: output datatype
  66.   - 'oa: output abstraction
  67. *)
  68.  
  69. type ('t, 'd, 'a, 'plop) fri =
  70.   | C: 'c -> ('c, 'c, unit, 'c) fri
  71.   | F: ('it, 'id, 'ia, 'plopi) fri * ('ot, 'od, 'oa, 'plopo) fri * ('id, 'od, 'd) fi -> ('it -> 'ot, 'd, 'ia -> 'oa, 'id -> 'od) fri
  72.  
  73. (* Function
  74.   - 't type (abstract type)
  75.   - 'd datatype (concrete type)
  76.   - 'a abstract (abstraction of abstract type)
  77. *)
  78. type ('t, 'd, 'a, 'plop) t =
  79.   {
  80.     data: 'd;
  81.     implementation: ('t, 'd, 'a, 'plop) fri
  82.   }
  83.  
  84. (* That's where the 'a is supposed to help: I only need to match against F *)
  85. let get1 (i : 'id) (f : ('t, 'd, 'ai -> 'ao, 'id -> 'od) t) =
  86.   match f.implementation with
  87.   | F (ii, oi, fi) -> fi.get i f.data (* This works *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement