Advertisement
xavierm02

Untitled

Jul 18th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.34 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.   Example: ('i, 'o, ('i, 'o) PMap.t) 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 recursive implementation
  24.   - 't: type (abstract type)
  25.   - 'd: datatype (concrete type)
  26.   - 'a: abstraction (abstraction of abstract type) (it's the same thing as the abstract type except that in C, 'c is replaced by unit
  27.                                 to allow differetiation function types treated as constants and function types being implemented)
  28.  
  29.   - 'c: constant type
  30.   - 'it: input type
  31.   - 'id: input datatype
  32.   - 'ia: input abstraction
  33.   - 'ot: output type
  34.   - 'od: output datatype
  35.   - 'oa: output abstraction
  36. *)
  37.  
  38. type ('t, 'd, 'a) fri =
  39.   | C: 'c -> ('c, 'c, unit) fri
  40.   | F: ('it, 'id, 'ia) fri * ('ot, 'od, 'oa) fri * ('id, 'od, 'd) fi -> ('it -> 'ot, 'd, 'ia -> 'oa) fri
  41.  
  42. (* Function
  43.   - 't type (abstract type)
  44.   - 'd datatype (concrete type)
  45.   - 'a abstract (abstraction of abstract type)
  46. *)
  47. type ('t, 'd, 'a) t =
  48.   {
  49.     data: 'd;
  50.     implementation: ('t, 'd, 'a) fri
  51.   }
  52.  
  53. (* That's where the 'a is supposed to help: I only need to match against F *)
  54. let get1 i (f : ('t, 'd, 'ai -> 'ao) t) =
  55.   match f.implementation with
  56.   | F (ii, oi, fi) -> failwith "TODO" (* This words *)
  57.  
  58. (* But when I ty to actually implement the function, I get a weird error *)
  59. let get2 i (f : ('t, 'd, 'ai -> 'ao) t) =
  60.   match f.implementation with
  61.   | F (ii, oi, fi) -> fi.get i f.data
  62.   (*Error: This expression has type id#0 but an expression was expected of type
  63.          id#0
  64.        The type constructor id#0 would escape its scope
  65.   *)
  66.  
  67. (* Google helped me find other people with the same problem and the answer was to add type arguments so I tried *)
  68. let get3 (type t2) (type d) (type ai) (type ao) i (f : (t2, d, ai -> ao) t) =
  69.   match f.implementation with
  70.   | F (ii, oi, fi) -> fi.get i f.data
  71.   (*Error: This expression has type id#0 but an expression was expected of type
  72.          id#0
  73.        The type constructor id#0 would escape its scope
  74.   *)
  75.  
  76. (* I also tried removing the type restriction and matching both cases but it doesn't work either *)
  77. let get4 i f  =
  78.   match f.implementation with
  79.   | C c -> Some c
  80.   | F (ii, oi, fi) -> fi.get i f.data
  81.   (* Error: This pattern matches values of type
  82.          ('a -> 'b, 'a -> 'b, ex#0 -> ex#1) fri
  83.        but a pattern was expected which matches values of type
  84.          ('a -> 'b, 'a -> 'b, unit) fri
  85.        Type ex#0 -> ex#1 is not compatible with type unit
  86.   *)
  87.  
  88. (* And removing the type restriction and not matching C *)
  89. let get5 i f =
  90.   match f.implementation with
  91.   | F (ii, oi, fi) -> fi.get i f.data
  92.   (* Error: This expression has type id#0 but an expression was expected of type
  93.          id#0
  94.        The type constructor id#0 would escape its scope
  95.   *)
  96.  
  97. (* And replacing by _ [http://caml.inria.fr/mantis/view.php?id=6264] *)
  98. let get6 i (f : (_, _, _ -> _) t) =
  99.   match f.implementation with
  100.   | F (ii, oi, fi) -> fi.get i f.data
  101.   (*Error: This expression has type id#0 but an expression was expected of type
  102.          id#0
  103.        The type constructor id#0 would escape its scope
  104.   *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement